




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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
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
4、; 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: interface fo
5、r 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.h"class mp3 public:mp3();virtual mp3(); hwnd m_hwnd; /¼
6、;Ǽµ±Ç°´°¿ÚµÄ¾ä±údword deviceid;/Ö¸¶¨²¥·ÅÒôÀÖµÄÉ豸id mci_open_parms mciopenparms; /Ö¸¶¨´ò
7、1;ªÒôÀÖÎļþµÄ²ÎÊý 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
8、中添加如下代码/ mp3.cpp: implementation of the mp3 class./#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()vo
9、id mp3:load(hwnd hwnd,cstring strfilepath)m_hwnd=hwnd;mcisendcommand(deviceid,mci_close,0,0); /¼ÓÔØÎļþǰÏÈÇå³ýÉÏ´ÎÉèÖÃmciopenparms.lpstrelementname=strfilepath;/½«Ò
10、ôÀÖÎļþ·¾¶´«¸øÉ豸dword dwreturn;if (dwreturn=mcisendcommand(null,mci_open,mci_open_element|mci_wait,(dword)(lpvoid)&mciopenparms)/Èç¹û´ò¿ªÊ§°
11、20;£¬½«³ö´íÐÅÏ¢´æÔÚbuffer²¢ÏÔʾ³ö´í¾¯¸æchar buffer256; mcigeterrorstring(dwreturn,buffer,256);messagebox(hwnd,buffer,"³ö´í
12、90;¯¸æ",mb_iconhand|mb_iconerror|mb_iconstop);deviceid=mciopenparms.wdeviceid; /¶àýÌåÉ豸ÀàÐͱàºÅdword mp3:getinformation(dword item)/mci½Ó¿Ú¶ÔÏó
13、1;Ä״̬mci_status_parms mcistatusparms;/´ý»ñÈ¡µÄÏîÄ¿mcistatusparms.dwitem=item;mcistatusparms.dwreturn=0;/Ïë¶àýÌåÉ豸·¢ËÍÖ¸&
14、#193;»ñÈ¡µ±Ç°µÄ״̬²ÎÊýmcisendcommand(deviceid,mci_status,mci_status_item,(dword)&mcistatusparms);return mcistatusparms.dwreturn; void mp3:play()mci_play_parms mciplayparms;mciplayparms.dw
15、callback=(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
16、,mci_resume,0,0);void mp3:stop()mcisendcommand(deviceid,mci_stop,0,0);mcisendcommand(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_i
17、nt = 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;dlg.cpp中头文件如下:#include "stdafx.h"#include "mp3²¥·ÅÆ÷.h"#include "mp3²¥
18、183;ÅÆ÷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
19、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.domoda
20、l()=idok)cstring strfilepath=dlg.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:%0
21、2d",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);getdlgitem(idc_stop)->enablewindow(true);getdlgitem(idc_del)->enablewindow(true); m_list.insertst
22、ring(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;second=0;dc.setbkcolor(rgb(124,252,0);/设置放置计数器区域的外观dc.settextcolor(rgb(255,25
23、5,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(); sett
24、imer(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 (strt
25、emp.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(idc_pause,"暂停");killtimer(0);/取消计数器的显示cstring mtime;cclientdc dc(this);hou
26、r=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);getdlgite
27、m(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_err)m_list.deletestring(index);v
28、oid 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(this);dc.setbkcolor(rgb(124,252,0
29、);/设置放置计数器区域的外观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; cdfrom=mci_make_m
30、sf(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);/设置放置计数器区域的外观dc.settextcol
31、or(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);mp3.
32、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:onrelease
33、dcaptureslider1(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;mc
34、isetvolumn.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可执行文件,为了去掉那个不好看的
35、图标,我们可以进入res文件夹,把原来的图标删掉,不过,你要放入一个cio格式的图片作为图标,cio格式网上有很多转换的,删掉原图标后,程序会自动生成一个你放进去的图标。接下来演示打包:工程,设置,或者按alt+f7,如图设置组建,批组建,如图创建完成后,你就会发现在你的工程文件中多了一个release文件夹,打开找到exe,这个文件就是打包好的,随便放到哪里都可以执行,至此,工作基本完成了,最后附上源代码,仅供参考dlg.cpp文件/ mp3播放器dlg.cpp : implementation file/#include "stdafx.h"#include "
36、;mp3播放器.h"#include "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
37、 app aboutclass caboutdlg : public cdialogpublic:caboutdlg();/ dialog data/afx_data(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/ implement
38、ationprotected:/afx_msg(caboutdlg)/afx_msgdeclare_message_map();caboutdlg:caboutdlg() : 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)
39、/afx_msg_map(caboutdlg)/ no message handlers/afx_msg_mapend_message_map()/ cmp3dlg 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()-
40、>loadicon(idi_icon1);hour=0;minute=0;second=0;void cmp3dlg:dodataexchange(cdataexchange* 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)/
41、afx_msg_map(cmp3dlg)on_wm_syscommand()on_wm_paint()on_wm_querydragicon()on_bn_clicked(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_not
42、ify(nm_releasedcapture, idc_slider1, onreleasedcaptureslider1)on_wm_timer()on_bn_clicked(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./ id
43、m_aboutbox must be in the system command range.assert(idm_aboutbox & 0xfff0) = idm_aboutbox);assert(idm_aboutbox < 0xf000);cmenu* psysmenu = getsystemmenu(false);if (psysmenu != null)cstring straboutmenu;straboutmenu.loadstring(ids_aboutbox);if (!straboutmenu.isempty()psysmenu->appendmenu(
44、mf_separator);psysmenu->appendmenu(mf_string, idm_aboutbox, straboutmenu);/ set the icon for this dialog. the framework does this automatically/ when the application's main window is not a dialogseticon(m_hicon, true);/ set big iconseticon(m_hicon, false); / set small iconsetwindowtext("
45、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
46、: add extra 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 dia
47、log, you will need 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(),
48、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 = (rect.height() - cyicon + 1) / 2;/ draw the icondc.drawicon(x, y, m_hicon);else/cdialog:onpaint();
49、cpaintdc dc(this); 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.wi
50、dth(),rect.height(),&dcmem,0,0,bitmap.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
51、 code herecdialog:oncancel();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:onfileopen() char filefiler="
52、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(
53、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);/显示时间进度dc.textout(280,128,mtime);mp3 mp3;mp3.load(this->m_hwnd,strfi
54、lepath);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);void cmp3dlg:onopen
55、() 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:%02d",hour,minute,second);/显示时间进度dc.textout(280,128,mtime);if(i
56、ndex=-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() / todo: add your control notification handler code herecstring strtemp;mp3 mp3;getdlgitemtext(idc_pause,strtemp);/获取按钮状态if (strtemp.compare("暂停")=0)mp3.pause();setdlgitemtext(idc_pause,"继续");killtim
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 血液系统疾病的护理
- 节能电池考试题及答案
- 江苏保安考试题及答案
- 沙漠压力测试题及答案
- EMC布线考试题及答案
- 林草局遴选试题及答案
- 弱电安防面试题及答案
- 2025年隔膜电解装置项目申请报告模板
- 低碳城市规划与城市绿色建筑运营维护实践案例分析报告
- 2025年在线教育平台用户满意度提升策略与商业模式创新报告
- 2023年黑龙江省文化和旅游系统事业单位人员招聘笔试模拟试题及答案解析
- 2023年江西新余市数字产业投资发展有限公司招聘笔试题库含答案解析
- LY/T 3323-2022草原生态修复技术规程
- 部编版六年级语文下册课件第1课《北京的春节》《腊八粥》
- 涂装工模拟练习题含答案
- 2023-2024学年河南省永城市小学数学二年级下册期末评估测试题
- 乳腺疾病的超声诊断 (超声科)
- 服务精神:马里奥特之路
- 《建筑施工安全检查标准》JGJ59-2011图解
- 华为大学人才培养与发展实践
- 医疗垃圾废物处理课件
评论
0/150
提交评论