手把手教您用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);//devicecontextforpaintingSendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);//CentericoninclientrectangleintcxIcon=GetSystemMetrics(SM_CXICON);intcyIcon=GetSystemMetrics(SM_CYICON);CRectrect;GetClientRect(&rect);intx=(rect.Width()-cxIcon+1)/2;inty=(rect.Height()-cyIcon+1)/2;//Drawtheicondc.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_VER1000#pragmaonce#endif//_MSC_VER1000#includeMmsystem.hclassMp3{public:Mp3();virtual~Mp3();HWNDm_hWnd;//¼Ç¼µ±Ç°´°¿ÚµÄ¾ä±úDWORDDeviceID;//Ö¸¶¨²¥·ÅÒôÀÖµÄÉ豸IDMCI_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.////////////////////////////////////////////////////////////////////////#includestdafx.h#includeMp3²¥·ÅÆ÷.h#includeMp3.h#ifdef_DEBUG#undefTHIS_FILEstaticcharTHIS_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//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32m_hIcon=AfxGetApp()-LoadIcon(IDI_ICON1);hour=0;minute=0;second=0;}dlg.cpp中头文件如下:#includestdafx.h#includeMp3²¥·ÅÆ÷.h#includeMp3²¥·ÅÆ÷Dlg.h#includeMmsystem.h#includeDigitalv.h#includeMp3.h//ÒôÁ¿¿ØÖÆÓõ½#pragmacomment(lib,Winmm.lib)#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_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();CStr

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功