手把手教您用MFC做MP3音乐播放器_第1页
手把手教您用MFC做MP3音乐播放器_第2页
手把手教您用MFC做MP3音乐播放器_第3页
手把手教您用MFC做MP3音乐播放器_第4页
手把手教您用MFC做MP3音乐播放器_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、打开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:OnPaint() if (IsIconic()CP

2、aintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y =

3、 (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.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_BITMAP6是你的位图地址BITMAP bitmap; bmpBackgro

4、und.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: interface for the Mp3 class./#if !de

5、fined(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 DeviceID;/ID MCI_OPEN_PARMS mciopenparms; / v

6、oid 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./#include stdafx.h#include Mp3.h#include Mp3

7、.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.lpstrElementName=Strfilepath;/DWORD dwReturn;if (dwReturn=

8、mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)(LPVOID)&mciopenparms)/bufferchar buffer256; mciGetErrorString(dwReturn,buffer,256);MessageBox(hwnd,buffer,MB_ICONHAND|MB_ICONERROR|MB_ICONSTOP);DeviceID=mciopenparms.wDeviceID; /DWORD Mp3:getinformation(DWORD item)/MCIMCI_STATUS_PARMS mc

9、istatusparms;/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.dwCallback=(DWORD)m_hWnd;mciplayparms.dwFrom=0; /mciSendCommand(Devic

10、eID,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);mciSendCommand(DeviceID,MCI_CLOSE,0,0);在dlg.cpp文件的public中添加一行代码:int hour,

11、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 subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDI_ICON1);hour=0;mi

12、nute=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 THIS_FILE = _FILE_;#endif在对话框中双击添加添加onfileopen函数,代码如下void CMp3Dlg:Onfileope

13、n() 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.GetPathName();CString strfilename=dlg.GetFileName();SetDlgItemText(IDC_filena

14、me,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);/显示时间进度dc.TextOut(280,128,mtime);Mp3 mp3;mp3.Load(this-m_hWnd,strfilepath);GetDlgItem(IDC_ope

15、n)-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);双击播放,进入代码,添加如下void CMp3Dlg:Onopen() CString strfilename;int i

16、ndex=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:%02d,hour,minute,second);/显示时间进度dc.TextOut(280,128,mtime);if(index=-1)MessageBox(请添加音乐);return;m_list.

17、GetText(index,strfilename);SetDlgItemText(IDC_filename,strfilename); mp3.Stop();mp3.Load(this-m_hWnd,strfilename);mp3.Play(); SetTimer(0,1000,NULL);同理,暂停,停止,删除,退出代码如下void CMp3Dlg:Onpause() / TODO: Add your control notification handler code hereCString strtemp;Mp3 mp3;GetDlgItemText(IDC_pause,strtemp

18、);/获取按钮状态if (strtemp.Compare(暂停)=0)mp3.Pause();SetDlgItemText(IDC_pause,继续);KillTimer(0);/取消计数器的显示if (strtemp.Compare(继续)=0)mp3.resum();SetTimer(0,1000,NULL);SetDlgItemText(IDC_pause,暂停);void CMp3Dlg:Onstop() / TODO: Add your control notification handler code hereMp3 mp3;mp3.Stop();SetDlgItemText(ID

19、C_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);/设置数字显示的颜色mtime.Format(%02d:%02d:%02d,hour,minute,second);/显示时间进度dc.TextOut(280,128,mtime);GetDlgItem(IDC_open)-EnableWindow(FALSE);G

20、etDlgItem(IDC_pause)-EnableWindow(FALSE);GetDlgItem(IDC_stop)-EnableWindow(FALSE); GetDlgItem(IDC_del)-EnableWindow(FALSE);void CMp3Dlg:Ondel() UpdateData(TRUE);Mp3 mp3;int index=m_list.GetCurSel();mp3.Stop();SetDlgItemText(IDC_filename,);KillTimer(0);hour=0;minute=0;second=0;/歌曲时间置0if (index!=CB_ER

21、R)m_list.DeleteString(index);void CMp3Dlg:Onexit() / TODO: Add your control notification handler code hereCDialog:OnCancel();ctrl+w打开类向导,如图,添加ontimer函数代码如下:void CMp3Dlg:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call defaultCString mtime;Mp3 mp3;second+;CClientDC dc(thi

22、s);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); DWORD cdf=mp3.getinformation(MCI_STATUS_POSITION);DWORD cdfrom;

23、 cdfrom=MCI_MAKE_MSF(MCI_MSF_MINUTE(cdf),MCI_MSF_SECOND(cdf),MCI_MSF_FRAME(cdf);/获取当前播放文件的信息UpdateData(false);CDialog:OnTimer(nIDEvent);ctrl+w打开类向导添加函数如下void CMp3Dlg:OnDblclkList() /在列表中选中,双击左键播放音乐CString mtime;Mp3 mp3;CClientDC dc(this);hour=0;minute=0;second=0;dc.SetBkColor(RGB(124,252,0);/设置放置计数器

24、区域的外观dc.SetTextColor(RGB(255,255,203);/设置数字显示的颜色mtime.Format(%02d:%02d:%02d,hour,minute,second);/显示时间进度dc.TextOut(280,128,mtime);CString strfilename;int index=m_list.GetCurSel();m_list.GetText(index,strfilename);SetDlgItemText(IDC_filename,strfilename);mp3.Stop();mp3.Load(this-m_hWnd,strfilename);mp

25、3.Play(); SetTimer(0,1000,NULL);打开类向导,添加函数如下void CMp3Dlg:OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) / TODO: Add your control notification handler code hereUpdateData(true);m_int=m_slider.GetPos()/10;Setvolumn(m_slider.GetPos();UpdateData(false);*pResult = 0;打开类向导,添加函数如下void CMp3Dlg:OnRelea

26、sedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) / TODO: Add your control notification handler code hereSetvolumn(m_slider.GetPos();*pResult = 0;添加声音设置函数如下DWORD CMp3Dlg:Setvolumn(DWORD vol)MCI_DGV_SETAUDIO_PARMS mcisetvolumn;mcisetvolumn.dwCallback=NULL;mcisetvolumn.dwItem=MCI_DGV_SETAUDIO_VOLUME;

27、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可执行文件,为了去掉那个不好看的图标

28、,我们可以进入res文件夹,把原来的图标删掉,不过,你要放入一个cio格式的图片作为图标,cio格式网上有很多转换的,删掉原图标后,程序会自动生成一个你放进去的图标。接下来演示打包:工程,设置,或者按alt+f7,如图设置组建,批组建,如图创建完成后,你就会发现在你的工程文件中多了一个release文件夹,打开找到exe,这个文件就是打包好的,随便放到哪里都可以执行,至此,工作基本完成了,最后附上源代码,仅供参考dlg.cpp文件/ Mp3播放器Dlg.cpp : implementation file/#include stdafx.h#include Mp3播放器.h#include Mp

29、3播放器Dlg.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 THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CA

30、boutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg()

31、: CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMp3Dlg dia

32、logCMp3Dlg: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 subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDI_ICON1);hour=0;minute=0;second=0;void CMp3Dlg:DoDataExchange(CDataExchange

33、* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMp3Dlg)DDX_Control(pDX, IDC_LIST, m_list);DDX_Control(pDX, IDC_SLIDER1, m_slider);DDX_Text(pDX, IDC_vol, m_int);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMp3Dlg, CDialog)/AFX_MSG_MAP(CMp3Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(ID_e

34、xit, Onexit)ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)ON_BN_CLICKED(IDC_fileopen, Onfileopen)ON_BN_CLICKED(IDC_open, Onopen)ON_BN_CLICKED(IDC_pause, Onpause)ON_BN_CLICKED(IDC_stop, Onstop)ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnReleasedcaptureSlider1)ON_WM_TIMER()ON_BN_CLICKED(I

35、DC_del, Ondel)ON_LBN_DBLCLK(IDC_LIST, OnDblclkList)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMp3Dlg message handlersBOOL CMp3Dlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(ID

36、M_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE); / Set small iconSetWind

37、owText(MP3播放器);MoveWindow(250,150,580,500); /显示时间控制m_slider.SetRange(0,1000); /移动范围m_slider.SetPos(500);/滑块指针的初始位置GetDlgItem(IDC_open)-EnableWindow(FALSE);GetDlgItem(IDC_pause)-EnableWindow(FALSE);GetDlgItem(IDC_stop)-EnableWindow(FALSE); GetDlgItem(IDC_del)-EnableWindow(FALSE);/ TODO: Add extra ini

38、tialization herereturn TRUE; / return TRUE unless you set the focus to a controlvoid CMp3Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need

39、the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CMp3Dlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in

40、 client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);else/CDialog:OnPaint();CPaintDC dc(this); CRec

41、t rect; GetClientRect(&rect); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap bmpBackground; bmpBackground.LoadBitmap(IDB_BITMAP6); BITMAP bitmap; bmpBackground.GetBitmap(&bitmap); CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground); dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitmap.b

42、mWidth,bitmap.bmHeight,SRCCOPY); / The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CMp3Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CMp3Dlg:Onexit() / TODO: Add your control notification handler code hereCDialog:OnCancel();void CMp3Dlg:O

43、nCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) / TODO: Add your control notification handler code hereUpdateData(true);m_int=m_slider.GetPos()/10;Setvolumn(m_slider.GetPos();UpdateData(false);*pResult = 0;void CMp3Dlg:Onfileopen() char filefiler=mp3文件(*.mp3)|*.mp3| wma文件(*.wma)|*.wma| wav文件(*.w

44、av)|*.wav|;CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING,filefiler);if (dlg.DoModal()=IDOK)CString strfilepath=dlg.GetPathName();CString strfilename=dlg.GetFileName();SetDlgItemText(IDC_filename,strfilename);CString mtime;CClientDC dc(this);hour=0;minute=0;sec

45、ond=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);Mp3 mp3;mp3.Load(this-m_hWnd,strfilepath);GetDlgItem(IDC_open)-EnableWindow(TRUE);GetDlgItem(IDC_pause)-EnableWindow(TRUE);GetDl

46、gItem(IDC_stop)-EnableWindow(TRUE);GetDlgItem(IDC_del)-EnableWindow(TRUE); m_list.InsertString(m_list.GetCount(),strfilename);/获取文件名 m_list.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;s

47、econd=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();mp

48、3.Load(this-m_hWnd,strfilename);mp3.Play(); SetTimer(0,1000,NULL);void CMp3Dlg:Onpause() / TODO: Add your control notification handler code hereCString 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_pause,暂停);void CMp3Dlg:Onstop() / TODO: Add your control notification handler code hereMp3

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论