当前位置:文档之家› 手把手教您用MFC做MP3音乐播放器

手把手教您用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::OnPaint(){if (IsIconic()){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 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;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 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.h"class Mp3{public:Mp3();virtual ~Mp3();HWND m_hWnd; //¼Ç¼µ±Ç°´°¿ÚµÄ¾ä±úDWORD DeviceID;//Ö¸¶¨²¥·ÅÒôÀÖµÄÉ豸IDMCI_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.////////////////////////////////////////////////////////////////////////#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.lpstrElementName=Strfilepath;//½«ÒôÀÖÎļþ·¾¶´«¸øÉ豸DWORD dwReturn;if(dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)(LPV OID)&mciopenparms)){//Èç¹û´ò¿ªÊ§°Ü£¬½«³ö´íÐÅÏ¢´æÔÚbuffer²¢ÏÔʾ³ö´í¾¯¸æchar buffer[256];mciGetErrorString(dwReturn,buffer,256);MessageBox(hwnd,buffer,"³ö´í¾¯¸æ",MB_ICONHAND|MB_ICONERROR|MB_ICONSTOP);}DeviceID=mciopenparms.wDeviceID; //¶àýÌåÉ豸ÀàÐͱàºÅ}DWORD Mp3::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.dwCallback=(DWORD)m_hWnd;mciplayparms.dwFrom=0; //ÿ´Î´ÓÍ·²¥·ÅmciSendCommand(DeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mcipla yparms);}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,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;minute=0;second=0;}dlg.cpp中头文件如下:#include "stdafx.h"#include "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在对话框中双击添加添加onfileopen函数,代码如下void CMp3Dlg::Onfileopen(){char filefiler[]="mp3文件(*.mp3)|*.mp3|""wma文件(*.wma)|*.wma|""wav文件(*.wav)|*.wav|";CFileDialogdlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING,filefile r);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;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_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;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(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(){// TODO: Add your control notification handler code here CString strtemp;Mp3 mp3;GetDlgItemText(IDC_pause,strtemp);//获取按钮状态if (pare("暂停")==0){mp3.Pause();SetDlgItemText(IDC_pause,"继续");KillTimer(0);//取消计数器的显示}if (pare("继续")==0){mp3.resum();SetTimer(0,1000,NULL);SetDlgItemText(IDC_pause,"暂停");}}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));//设置数字显示的颜色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 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);}}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(this);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;cdfrom=MCI_MAKE_MSF(MCI_MSF_MINUTE(cdf),MCI_MSF_SECOND(cdf),MCI_MSF_FRAM E(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.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);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::OnReleasedcaptureSlider1(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;mcisetvolumn.dwValue=vol;MCI_OPEN_PARMS mciopenparms;DWORD DeviceID;DeviceID=mciopenparms.wDeviceID;mciSendCommand(DeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAU DIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn);// return mcisetvolumn.dwValue;return 0;}到此已经基本完成了,我们可以试听一下接下来我们可以到包成exe可执行文件,为了去掉那个不好看的图标,我们可以进入res文件夹,把原来的图标删掉,不过,你要放入一个cio格式的图片作为图标,cio格式网上有很多转换的,删掉原图标后,程序会自动生成一个你放进去的图标。

相关主题