1、用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函数下添加代码void CMp3Dlg:OnPain
2、t() if (IsIconic() CPaintDC dc(this); / device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); / Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width
3、() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; / Draw the icon dc.DrawIcon(x, y, m_hIcon); else /CDialog:OnPaint(); CPaintDC dc(this); CRect rect; GetClientRect(&rect); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap bmpBackground; bmpBackground.LoadBitmap(IDB_BITMAP6); /IDB_BITMA
4、P6是你的位图地址 BITMAP bitmap; 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: i
5、nterface for the Mp3 class./#if !defined(AFX_MP3_H_20D325E5_A96A_43FE_A485_92F57C68DD0D_INCLUDED_)#define AFX_MP3_H_20D325E5_A96A_43FE_A485_92F57C68DD0D_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#include Mmsystem.hclass Mp3 public: Mp3(); virtual Mp3(); HWND m_hWnd; / DWORD DeviceI
6、D;/ID MCI_OPEN_PARMS mciopenparms; / void Load(HWND hwnd,CString Strfilepath); DWORD getinformation(DWORD item); void Play(); void Pause(); void resum(); void Stop();#endif / !defined(AFX_MP3_H_20D325E5_A96A_43FE_A485_92F57C68DD0D_INCLUDED_)在mp3.cpp中添加如下代码/ Mp3.cpp: implementation of the Mp3 class./
7、#include stdafx.h#include Mp3.h#include Mp3.h#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE=_FILE_;#define new DEBUG_NEW#endif/ Construction/Destruction/Mp3:Mp3()Mp3:Mp3()void Mp3:Load(HWND hwnd,CString Strfilepath) m_hWnd=hwnd; mciSendCommand(DeviceID,MCI_CLOSE,0,0); / mciopenparms.lpstrElement
8、Name=Strfilepath;/ DWORD dwReturn; if (dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)(LPVOID)&mciopenparms) /buffer char buffer256; mciGetErrorString(dwReturn,buffer,256); MessageBox(hwnd,buffer,MB_ICONHAND|MB_ICONERROR|MB_ICONSTOP); DeviceID=mciopenparms.wDeviceID; /DWORD M
9、p3:getinformation(DWORD item) /MCI MCI_STATUS_PARMS mcistatusparms; / mcistatusparms.dwItem=item; mcistatusparms.dwReturn=0; / mciSendCommand(DeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)&mcistatusparms); return mcistatusparms.dwReturn; void Mp3:Play() MCI_PLAY_PARMS mciplayparms; mciplayparms.dwCallb
10、ack=(DWORD)m_hWnd; mciplayparms.dwFrom=0; / mciSendCommand(DeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciplayparms);void Mp3:Pause() mciSendCommand(DeviceID,MCI_PAUSE,0,0);void Mp3:resum() mciSendCommand(DeviceID,MCI_RESUME,0,0);void Mp3:Stop() mciSendCommand(DeviceID,MCI_STOP,0,0); mciSe
11、ndCommand(DeviceID,MCI_CLOSE,0,0);在dlg.cpp文件的public中添加一行代码:int hour,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 / Note that LoadIcon does not require a subsequen
12、t DestroyIcon in Win32 m_hIcon = AfxGetApp()-LoadIcon(IDI_ICON1); hour=0;minute=0;second=0;dlg.cpp中头文件如下:#include stdafx.h#include Mp3.h#include Mp3Dlg.h#include Mmsystem.h#include Digitalv.h #include Mp3.h /#pragma comment(lib,Winmm.lib)#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char
13、THIS_FILE = _FILE_;#endif在对话框中双击添加添加onfileopen函数,代码如下void CMp3Dlg:Onfileopen() char filefiler=mp3文件(*.mp3)|*.mp3| wma文件(*.wma)|*.wma| wav文件(*.wav)|*.wav|; CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING,filefiler);if (dlg.DoModal()=IDOK) CString strfilepath=dlg.
14、GetPathName(); CString strfilename=dlg.GetFileName(); SetDlgItemText(IDC_filename,strfilename); CString mtime; CClientDC dc(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);/显示时间进度
15、dc.TextOut(280,128,mtime); Mp3 mp3; 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.
16、SetCurSel(m_list.GetCount()-1);双击播放,进入代码,添加如下void CMp3Dlg:Onopen() CString strfilename; int index=m_list.GetCurSel(); CString mtime; CClientDC dc(this); Mp3 mp3; hour=0;minute=0;second=0; dc.SetBkColor(RGB(124,252,0);/设置放置计数器区域的外观 dc.SetTextColor(RGB(255,255,203);/设置数字显示的颜色 mtime.Format(%02d:%02d:%0
17、2d,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);同理,暂停,停止,删除,退出代码如下void CMp3Dlg:Onpause() / T
18、ODO: Add your control notification handler code here CString strtemp; Mp3 mp3; 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_paus
19、e,暂停); void CMp3Dlg:Onstop() / TODO: Add your control notification handler code here Mp3 mp3; mp3.Stop(); SetDlgItemText(IDC_pause,暂停); KillTimer(0);/取消计数器的显示 CString mtime; CClientDC dc(this); hour=0;minute=0;second=0; dc.SetBkColor(RGB(124,252,0);/设置放置计数器区域的外观 dc.SetTextColor(RGB(255,255,203);/设置数
20、字显示的颜色 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);void CMp3Dlg:Ondel() UpdateData(TRUE); Mp3
21、mp3; int index=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); void CMp3Dlg:Onexit() / TODO: Add your control notification handler code here CDialog:OnCancel(); ctrl+w打开类向导,如图,添加ontimer函数代码如下
22、:void CMp3Dlg:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call default CString mtime; Mp3 mp3; second+; CClientDC dc(this); dc.SetBkColor(RGB(124,252,0);/设置放置计数器区域的外观 dc.SetTextColor(RGB(255,255,203);/设置数字显示的颜色 if(second=60)/设置钟表的显示 minute+;second=0; if(minute=60) hour+;
23、minute=0; mtime.Format(%02d:%02d:%02d,hour,minute,second);/显示时间进度 dc.TextOut(280,128,mtime); DWORD cdf=mp3.getinformation(MCI_STATUS_POSITION); DWORD cdfrom; cdfrom=MCI_MAKE_MSF(MCI_MSF_MINUTE(cdf),MCI_MSF_SECOND(cdf),MCI_MSF_FRAME(cdf);/获取当前播放文件的信息 UpdateData(false); CDialog:OnTimer(nIDEvent);ctrl+
24、w打开类向导添加函数如下void CMp3Dlg:OnDblclkList() /在列表中选中,双击左键播放音乐 CString mtime; Mp3 mp3; CClientDC dc(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); CSt
25、ring strfilename; int index=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); 打开类向导,添加函数如下void CMp3Dlg:OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) / TODO: Add you
26、r control notification handler code here UpdateData(true); m_int=m_slider.GetPos()/10; Setvolumn(m_slider.GetPos(); UpdateData(false); *pResult = 0;打开类向导,添加函数如下void CMp3Dlg:OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) / TODO: Add your control notification handler code here Setvolumn(m_s
27、lider.GetPos(); *pResult = 0;添加声音设置函数如下DWORD CMp3Dlg:Setvolumn(DWORD vol) MCI_DGV_SETAUDIO_PARMS mcisetvolumn; mcisetvolumn.dwCallback=NULL; mcisetvolumn.dwItem=MCI_DGV_SETAUDIO_VOLUME; mcisetvolumn.dwValue=vol; MCI_OPEN_PARMS mciopenparms; DWORD DeviceID; DeviceID=mciopenparms.wDeviceID; mciSendCommand(DeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAUDIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn); / return mcisetvolumn.dwValue; return 0;到此已经基本完成了,我们可以试听一下接下来我们可以到包成exe可执行文件,为了去掉那个不好看的图标,我们可以进入res文件夹,把原来的图标删掉,不过,你要放入一个cio格式的图片作为图标,cio
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1