用MFC做MP3音乐播放器超实用.docx

上传人:b****6 文档编号:7124805 上传时间:2023-01-20 格式:DOCX 页数:36 大小:485.10KB
下载 相关 举报
用MFC做MP3音乐播放器超实用.docx_第1页
第1页 / 共36页
用MFC做MP3音乐播放器超实用.docx_第2页
第2页 / 共36页
用MFC做MP3音乐播放器超实用.docx_第3页
第3页 / 共36页
用MFC做MP3音乐播放器超实用.docx_第4页
第4页 / 共36页
用MFC做MP3音乐播放器超实用.docx_第5页
第5页 / 共36页
点击查看更多>>
下载资源
资源描述

用MFC做MP3音乐播放器超实用.docx

《用MFC做MP3音乐播放器超实用.docx》由会员分享,可在线阅读,更多相关《用MFC做MP3音乐播放器超实用.docx(36页珍藏版)》请在冰豆网上搜索。

用MFC做MP3音乐播放器超实用.docx

用MFC做MP3音乐播放器超实用

第一步:

打开vc6.0,建立如图所示mfc工程文件

选择基于对话框的确定

删除所有空间,建立如图所示对话框

属性如下:

播放IDC_open;

添加IDC_fileopen;

暂停IDC_pause;

删除IDC_del;

停止IDC_stop;

退出IDC_exit;

音乐名编辑框IDC_filename;

音量控制滑块IDC_SLIDER1;

音量控制编辑框IDC_vol;

建立类向导对应如下:

在工程文件,右键,插入,bitmap位图

引入你想插入的背景图,必须是bmp格式的

进入你的dlg.cpp文件

在onpaint函数下添加代码

voidCMp3Dlg:

:

OnPaint()

{

if(IsIconic())

{

CPaintDCdc(this);//devicecontextforpainting

SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);

//Centericoninclientrectangle

intcxIcon=GetSystemMetrics(SM_CXICON);

intcyIcon=GetSystemMetrics(SM_CYICON);

CRectrect;

GetClientRect(&rect);

intx=(rect.Width()-cxIcon+1)/2;

inty=(rect.Height()-cyIcon+1)/2;

//Drawtheicon

dc.DrawIcon(x,y,m_hIcon);

}

else

{

//CDialog:

:

OnPaint();

CPaintDCdc(this);

CRectrect;

GetClientRect(&rect);

CDCdcMem;

dcMem.CreateCompatibleDC(&dc);

CBitmapbmpBackground;

bmpBackground.LoadBitmap(IDB_BITMAP6);/IDB_BITMAP6是你的位图地址

BITMAPbitmap;

bmpBackground.GetBitmap(&bitmap);

CBitmap*pbmpOld=dcMem.SelectObject(&bmpBackground);

dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);

}

}

编译运行,你就会看到背景有图片了。

插入-类,找到geneticclass,类名mp3.cpp

你会发现在头文件中多了一个mp3.h文件

在mp3.h文件中添加代码如下

//Mp3.h:

interfacefortheMp3class.

//

//////////////////////////////////////////////////////////////////////

#if!

defined(AFX_MP3_H__20D325E5_A96A_43FE_A485_92F57C68DD0D__INCLUDED_)

#defineAFX_MP3_H__20D325E5_A96A_43FE_A485_92F57C68DD0D__INCLUDED_

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

#include"Mmsystem.h"

classMp3

{

public:

Mp3();

virtual~Mp3();

HWNDm_hWnd;//¼Ç¼µ±Ç°´°¿ÚµÄ¾ä±ú

DWORDDeviceID;//Ö¸¶¨²¥·ÅÒôÀÖµÄÉ豸ID

MCI_OPEN_PARMSmciopenparms;//Ö¸¶¨´ò¿ªÒôÀÖÎļþµÄ²ÎÊý

voidLoad(HWNDhwnd,CStringStrfilepath);

DWORDgetinformation(DWORDitem);

voidPlay();

voidPause();

voidresum();

voidStop();

};

#endif//!

defined(AFX_MP3_H__20D325E5_A96A_43FE_A485_92F57C68DD0D__INCLUDED_)

在mp3.cpp中添加如下代码

//Mp3.cpp:

implementationoftheMp3class.

//

//////////////////////////////////////////////////////////////////////

#include"stdafx.h"

#include"Mp3²¥·ÅÆ÷.h"

#include"Mp3.h"

#ifdef_DEBUG

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#definenewDEBUG_NEW

#endif

//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////

Mp3:

:

Mp3()

{

}

Mp3:

:

~Mp3()

{

}

voidMp3:

:

Load(HWNDhwnd,CStringStrfilepath)

{

m_hWnd=hwnd;

mciSendCommand(DeviceID,MCI_CLOSE,0,0);//¼ÓÔØÎļþÇ°ÏÈÇå³ýÉÏ´ÎÉèÖÃ

mciopenparms.lpstrElementName=Strfilepath;//½«ÒôÀÖÎļþ·¾¶´«¸øÉ豸

DWORDdwReturn;

if(dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)(LPVOID)&mciopenparms))

{

//Èç¹û´ò¿ªÊ§°Ü£¬½«³ö´íÐÅÏ¢´æÔÚbuffer²¢ÏÔʾ³ö´í¾¯¸æ

charbuffer[256];

mciGetErrorString(dwReturn,buffer,256);

MessageBox(hwnd,buffer,"³ö´í¾¯¸æ",MB_ICONHAND|MB_ICONERROR|MB_ICONSTOP);

}

DeviceID=mciopenparms.wDeviceID;//¶àýÌåÉ豸ÀàÐͱàºÅ

}

DWORDMp3:

:

getinformation(DWORDitem)

{

//MCI½Ó¿Ú¶ÔÏóµÄ״̬

MCI_STATUS_PARMSmcistatusparms;

//´ý»ñÈ¡µÄÏîÄ¿

mcistatusparms.dwItem=item;

mcistatusparms.dwReturn=0;

//Ïë¶àýÌåÉ豸·¢ËÍÖ¸Á»ñÈ¡µ±Ç°µÄ״̬²ÎÊý

mciSendCommand(DeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)&mcistatusparms);

returnmcistatusparms.dwReturn;

}

voidMp3:

:

Play()

{

MCI_PLAY_PARMSmciplayparms;

mciplayparms.dwCallback=(DWORD)m_hWnd;

mciplayparms.dwFrom=0;//ÿ´Î´ÓÍ·²¥·Å

mciSendCommand(DeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciplayparms);

}

voidMp3:

:

Pause()

{

mciSendCommand(DeviceID,MCI_PAUSE,0,0);

}

voidMp3:

:

resum()

{

mciSendCommand(DeviceID,MCI_RESUME,0,0);

}

voidMp3:

:

Stop()

{

mciSendCommand(DeviceID,MCI_STOP,0,0);

mciSendCommand(DeviceID,MCI_CLOSE,0,0);

}

在dlg.cpp文件的public中添加一行代码:

inthour,minute,second;

在CMp3Dlg:

:

CMp3Dlg(CWnd*pParent/*=NULL*/)中添加如下

CMp3Dlg:

:

CMp3Dlg(CWnd*pParent/*=NULL*/)

:

CDialog(CMp3Dlg:

:

IDD,pParent)

{

//{{AFX_DATA_INIT(CMp3Dlg)

m_int=0;

//}}AFX_DATA_INIT

//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32

m_hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);

hour=0;minute=0;second=0;

}

dlg.cpp中头文件如下:

#include"stdafx.h"

#include"Mp3²¥·ÅÆ÷.h"

#include"Mp3²¥·ÅÆ÷Dlg.h"

#include"Mmsystem.h"

#include"Digitalv.h"

#include"Mp3.h"//ÒôÁ¿¿ØÖÆÓõ½

#pragmacomment(lib,"Winmm.lib")

#ifdef_DEBUG

#definenewDEBUG_NEW

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#endif

在对话框中双击添加添加onfileopen函数,代码如下

voidCMp3Dlg:

:

Onfileopen()

{

charfilefiler[]="mp3文件(*.mp3)|*.mp3|"

"wma文件(*.wma)|*.wma|"

"wav文件(*.wav)|*.wav|";

CFileDialogdlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING,filefiler);

if(dlg.DoModal()==IDOK)

{

CStringstrfilepath=dlg.GetPathName();

CStringstrfilename=dlg.GetFileName();

SetDlgItemText(IDC_filename,strfilename);

CStringmtime;

CClientDCdc(this);

hour=0;minute=0;second=0;

dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观

dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色

mtime.Format("%02d:

%02d:

%02d",hour,minute,second);//显示时间进度

dc.TextOut(280,128,mtime);

Mp3mp3;

mp3.Load(this->m_hWnd,strfilepath);

GetDlgItem(IDC_open)->EnableWindow(TRUE);

GetDlgItem(IDC_pause)->EnableWindow(TRUE);

GetDlgItem(IDC_stop)->EnableWindow(TRUE);

GetDlgItem(IDC_del)->EnableWindow(TRUE);

m_list.InsertString(m_list.GetCount(),strfilename);//获取文件名

m_list.SetCurSel(m_list.GetCount()-1);

}

双击播放,进入代码,添加如下

voidCMp3Dlg:

:

Onopen()

{

CStringstrfilename;

intindex=m_list.GetCurSel();

CStringmtime;

CClientDCdc(this);

Mp3mp3;

hour=0;minute=0;second=0;

dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观

dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色

mtime.Format("%02d:

%02d:

%02d",hour,minute,second);//显示时间进度

dc.TextOut(280,128,mtime);

if(index==-1)

{

MessageBox("请添加音乐");

return;

}

m_list.GetText(index,strfilename);

SetDlgItemText(IDC_filename,strfilename);

mp3.Stop();

mp3.Load(this->m_hWnd,strfilename);

mp3.Play();

SetTimer(0,1000,NULL);

}

同理,暂停,停止,删除,退出代码如下

voidCMp3Dlg:

:

Onpause()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CStringstrtemp;

Mp3mp3;

GetDlgItemText(IDC_pause,strtemp);//获取按钮状态

if(strtemp.Compare("暂停")==0)

{

mp3.Pause();

SetDlgItemText(IDC_pause,"继续");

KillTimer(0);//取消计数器的显示

}

if(strtemp.Compare("继续")==0)

{

mp3.resum();

SetTimer(0,1000,NULL);

SetDlgItemText(IDC_pause,"暂停");

}

}

voidCMp3Dlg:

:

Onstop()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

Mp3mp3;

mp3.Stop();

SetDlgItemText(IDC_pause,"暂停");

KillTimer(0);//取消计数器的显示

CStringmtime;

CClientDCdc(this);

hour=0;minute=0;second=0;

dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观

dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色

mtime.Format("%02d:

%02d:

%02d",hour,minute,second);//显示时间进度

dc.TextOut(280,128,mtime);

GetDlgItem(IDC_open)->EnableWindow(FALSE);

GetDlgItem(IDC_pause)->EnableWindow(FALSE);

GetDlgItem(IDC_stop)->EnableWindow(FALSE);

GetDlgItem(IDC_del)->EnableWindow(FALSE);

}

voidCMp3Dlg:

:

Ondel()

{

UpdateData(TRUE);

Mp3mp3;

intindex=m_list.GetCurSel();

mp3.Stop();

SetDlgItemText(IDC_filename,"");

KillTimer(0);

hour=0;minute=0;second=0;//歌曲时间置0

if(index!

=CB_ERR)

{

m_list.DeleteString(index);

}

}

voidCMp3Dlg:

:

Onexit()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CDialog:

:

OnCancel();

}

ctrl+w打开类向导,如图,添加ontimer函数

代码如下:

voidCMp3Dlg:

:

OnTimer(UINTnIDEvent)

{

//TODO:

Addyourmessagehandlercodehereand/orcalldefault

CStringmtime;

Mp3mp3;

second++;

CClientDCdc(this);

dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观

dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色

if(second==60)//设置钟表的显示

{

minute++;second=0;

}

if(minute==60)

{

hour++;minute=0;

}

mtime.Format("%02d:

%02d:

%02d",hour,minute,second);//显示时间进度

dc.TextOut(280,128,mtime);

DWORDcdf=mp3.getinformation(MCI_STATUS_POSITION);

DWORDcdfrom;

cdfrom=MCI_MAKE_MSF(MCI_MSF_MINUTE(cdf),MCI_MSF_SECOND(cdf),MCI_MSF_FRAME(cdf));//获取当前播放文件的信息

UpdateData(false);

CDialog:

:

OnTimer(nIDEvent);

}

ctrl+w打开类向导添加函数如下

voidCMp3Dlg:

:

OnDblclkList()//在列表中选中,双击左键播放音乐

{

CStringmtime;

Mp3mp3;

CClientDCdc(this);

hour=0;minute=0;second=0;

dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观

dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色

mtime.Format("%02d:

%02d:

%02d",hour,minute,second);//显示时间进度

dc.TextOut(280,128,mtime);

CStringstrfilename;

intindex=m_list.GetCurSel();

m_list.GetText(index,strfilename);

SetDlgItemText(IDC_filename,strfilename);

mp3.Stop();

mp3.Load(this->m_hWnd,strfilename);

mp3.Play();

SetTimer(0,1000,NULL);

}

 

打开类向导,添加函数如下

voidCMp3Dlg:

:

OnCustomdrawSlider1(NMHDR*pNMHDR,LRESULT*pResult)

{

//TODO:

Addyourcontrolnotificationhandlercodehere

UpdateData(true);

m_int=m_slider.GetPos()/10;

Setvolumn(m_slider.GetPos());

UpdateData(false);

*pResult=0;

}

打开类向导,添加函数如下

voidCMp3Dlg:

:

OnReleasedcaptureSlider1(NMHDR*pNMHDR,LRESULT*pResult)

{

//TODO:

Addyourcontrolnotificationhandlercodehere

Setvolumn(m_slider.GetPos());

*pResult=0;

}

添加声音设置函数如下

DWORDCMp3Dlg:

:

Setvolumn(DWORDvol)

{

MCI_DGV_SETAUDIO_PARMSmcisetvolumn;

mcisetvolumn.dwCallback=NULL;

mcisetvolumn.dwItem=MCI_DGV_SETAUDIO_VOLUME;

mcisetvolumn.dwValue=vol;

MCI_OPEN_PARMSmciopenparms;

DWORDDeviceID;

DeviceID=mciopenparms.wDeviceID;

mciSendCommand(DeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAUDIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn);

//returnmcisetvolumn.dwValue;

return0;

}

 

到此已经基本完成了,我们可以试听一下

接下来我们可以到包成exe可执行文件,为了去掉那个不好看的图标,我们可以进入res文件夹,把原来的图标删掉,不过,你要放入一个cio格式的图片作为图标,cio

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1