用MFC做MP3音樂播放器超實用_第1頁
用MFC做MP3音樂播放器超實用_第2頁
用MFC做MP3音樂播放器超實用_第3頁
用MFC做MP3音樂播放器超實用_第4頁
用MFC做MP3音樂播放器超實用_第5頁
已閱讀5頁,還剩24頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

2、()cpaintdc 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

3、 y = (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; bmpbac

4、kground.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你會發(fā)現在頭文件中多了一個mp3.h文件在mp3.h文件中添加代碼如下/ mp3.h: interface for the mp3 class./#if

5、 !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 deviceid;/id mci_open_parms mciopenparms;

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

7、 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.lpstrelementname=strfilepath;/dword dwreturn;if (dwret

8、urn=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_parm

9、s 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.dwcallback=(dword)m_hwnd;mciplayparms.dwfrom=0; /mcisendcommand(d

10、eviceid,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 h

11、our,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=

12、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 this_file = _file_;#endif在對話框中雙擊添加添加onfileopen函數,代碼如下void cmp3dlg:onfil

13、eopen() 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_fi

14、lename,strfilename);cstring mtime;cclientdc dc(this);hour=0;minute=0;second=0;dc.setbkcolor(rgb(124,252,0);/設置放置計數器區(qū)域的外觀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

15、_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);雙擊播放,進入代碼,添加如下void cmp3dlg:onopen() cstring strfilename;i

16、nt index=m_list.getcursel();cstring mtime;cclientdc dc(this);mp3 mp3;hour=0;minute=0;second=0;dc.setbkcolor(rgb(124,252,0);/設置放置計數器區(qū)域的外觀dc.settextcolor(rgb(255,255,203);/設置數字顯示的顏色mtime.format(%02d:%02d:%02d,hour,minute,second);/顯示時間進度dc.textout(280,128,mtime);if(index=-1)messagebox(請?zhí)砑右魳?;return;m_l

17、ist.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,str

18、temp);/獲取按鈕狀態(tài)if (strtemp.compare(暫停)=0)mp3.pause();setdlgitemtext(idc_pause,繼續(xù));killtimer(0);/取消計數器的顯示if (strtemp.compare(繼續(xù))=0)mp3.resum();settimer(0,1000,null);setdlgitemtext(idc_pause,暫停);void cmp3dlg:onstop() / todo: add your control notification handler code heremp3 mp3;mp3.stop();setdlgitemtex

19、t(idc_pause,暫停);killtimer(0);/取消計數器的顯示cstring mtime;cclientdc dc(this);hour=0;minute=0;second=0;dc.setbkcolor(rgb(124,252,0);/設置放置計數器區(qū)域的外觀dc.settextcolor(rgb(255,255,203);/設置數字顯示的顏色mtime.format(%02d:%02d:%02d,hour,minute,second);/顯示時間進度dc.textout(280,128,mtime);getdlgitem(idc_open)-enablewindow(fals

20、e);getdlgitem(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!=c

21、b_err)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

22、(this);dc.setbkcolor(rgb(124,252,0);/設置放置計數器區(qū)域的外觀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 cdf

23、rom; 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、置計數器區(qū)域的外觀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

25、);mp3.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:onr

26、eleasedcaptureslider1(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_vol

27、ume;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可執(zhí)行文件,為了去掉那個不好

28、看的圖標,我們可以進入res文件夾,把原來的圖標刪掉,不過,你要放入一個cio格式的圖片作為圖標,cio格式網上有很多轉換的,刪掉原圖標后,程序會自動生成一個你放進去的圖標。接下來演示打包:工程,設置,或者按alt+f7,如圖設置組建,批組建,如圖創(chuàng)建完成后,你就會發(fā)現在你的工程文件中多了一個release文件夾,打開找到exe,這個文件就是打包好的,隨便放到哪里都可以執(zhí)行,至此,工作基本完成了,最后附上源代碼,僅供參考dlg.cpp文件/ mp3播放器dlg.cpp : implementation file/#include stdafx.h#include mp3播放器.h#includ

29、e mp3播放器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_dat

30、a(caboutdlg)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:caboutdl

31、g() : 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

32、 dialogcmp3dlg: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(cdataexch

33、ange* 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(

34、id_exit, 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_click

35、ed(idc_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);asser

36、t(idm_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 iconset

37、windowtext(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

38、 initialization 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 n

39、eed 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 ico

40、n 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 = (rect.height() - cyicon + 1) / 2;/ draw the icondc.drawicon(x, y, m_hicon);else/cdialog:onpaint();cpaintdc dc(this);

41、crect 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,bitm

42、ap.bmwidth,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 cmp3d

43、lg: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:onfileopen() char filefiler=mp3文件(*.mp3)|*.mp3| wma文件(*.wma)|*.wma| wav文件

44、(*.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_filename,strfilename);cstring mtime;cclientdc dc(this);hour=0;minute=0

45、;second=0;dc.setbkcolor(rgb(124,252,0);/設置放置計數器區(qū)域的外觀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);g

46、etdlgitem(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

47、=0;second=0;dc.setbkcolor(rgb(124,252,0);/設置放置計數器區(qū)域的外觀dc.settextcolor(rgb(255,255,203);/設置數字顯示的顏色mtime.format(%02d:%02d:%02d,hour,minute,second);/顯示時間進度dc.textout(280,128,mtime);if(index=-1)messagebox(請?zhí)砑右魳?;return;m_list.gettext(index,strfilename);setdlgitemtext(idc_filename,strfilename); mp3.stop(

48、);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);/獲取按鈕狀態(tài)if (strtemp.compare(暫停)=0)mp3.pause();setdlgitemtext(idc_pause,繼續(xù));killtimer(0);/取消計數器的顯示if (strtemp.compare(繼續(xù))=0)mp3.resum();settimer(0,1000,null);setdlgitemtext(idc_pause,暫停);void cmp3dlg:onstop() / todo: add your control notification handler code here

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論