文件名称:MFC制作的MP3
文件大小:2.75MB
文件格式:ZIP
更新时间:2018-01-02 16:40:26
MFC
使用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 painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int 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 icon dc.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;//Ö¸¶¨²¥•ÅÒôÀÖµÄÉ豸ID MCI_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_FILE static 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)(LPVOID)&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)&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,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 Win32 m_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_FILE static char 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.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("d:d:d",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("d:d:d",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 (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 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("d:d:d",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;//歌曲时间置0 if (index!=CB_ERR) { m_list.DeleteString(index); } } void CMp3Dlg::Onexit() { // TODO: Add your control notification handler code here CDialog::OnCancel(); } ctrl+w打开类向导,如图,添加ontimer函数 代码如下: void CMp3Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default CString 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("d:d:d",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_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.SetTextColor(RGB(255,255,203));//设置数字显示的颜色 mtime.Format("d:d:d",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 here UpdateData(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 here Setvolumn(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_SETAUDIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn); // return mcisetvolumn.dwValue; return 0; } 到此已经基本完成了,我们可以试听一下 接下来我们可以到包成exe可执行文件,为了去掉那个不好看的图标,我们可以进入res文件夹,把原来的图标删掉,不过,你要放入一个cio格式的图片作为图标,cio格式网上有很多转换的,删掉原图标后,程序会自动生成一个你放进去的图标。接下来演示打包: 工程,设置,或者按alt+f7,如图设置 组建,批组建,如图 创建完成后,你就会发现在你的工程文件中多了一个release文件夹,打开找到exe,这个文件就是打包好的,随便放到哪里都可以执行,至此,工作基本完成了,最后附上源代码,仅供参考 dlg.cpp文件 // Mp3播放器Dlg.cpp : implementation file // #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_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: 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 // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMp3Dlg dialog 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 Win32 m_hIcon = AfxGetApp()->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_MAP } BEGIN_MESSAGE_MAP(CMp3Dlg, CDialog) //{{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_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnReleasedcaptureSlider1) ON_WM_TIMER() ON_BN_CLICKED(IDC_del, Ondel) ON_LBN_DBLCLK(IDC_LIST, OnDblclkList) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMp3Dlg message handlers BOOL 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(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(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 dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon SetWindowText("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 initialization here return TRUE; // return TRUE unless you set the focus to a control } void CMp3Dlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, 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 painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int 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 icon dc.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); 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); } } // 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 here CDialog::OnCancel(); } void CMp3Dlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here UpdateData(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文件(*.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;second=0; dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观 dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色 mtime.Format("d:d:d",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("d:d:d",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 (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 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("d:d:d",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); } 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_SETAUDIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn); // return mcisetvolumn.dwValue; return 0; } void CMp3Dlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here Setvolumn(m_slider.GetPos()); *pResult = 0; } void CMp3Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default CString 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("d:d:d",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_FRAME(cdf));//获取当前播放文件的信息 UpdateData(false); CDialog::OnTimer(nIDEvent); } 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;//歌曲时间置0 if (index!=CB_ERR) { m_list.DeleteString(index); } } 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("d:d:d",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); } mp3.cpp文件 // Mp3.cpp: implementation of the Mp3 class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Mp3播放器.h" #include "Mp3.h" #ifdef _DEBUG #undef THIS_FILE static 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)(LPVOID)&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)&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); } 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;//指定播放音乐的设备ID MCI_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_)
【文件预览】:
mp3Player
----mp3Player.rc(6KB)
----mp3Player.plg(716B)
----StdAfx.cpp(211B)
----mp3Player.aps(790KB)
----resource.h(1KB)
----mp3.cpp(2KB)
----mp3Player.dsw(526B)
----mp3Player.cpp(2KB)
----mp3Player.h(1KB)
----Debug()
--------mp3Player.res(772KB)
--------vc60.pdb(372KB)
--------mp3PlayerDlg.obj(50KB)
--------vc60.idb(217KB)
--------mp3Player.pch(5.46MB)
--------mp3Player.pdb(409KB)
--------mp3Player.obj(24KB)
--------mp3Player.exe(1.01MB)
--------StdAfx.obj(103KB)
--------mp3.obj(19KB)
--------mp3Player.ilk(240KB)
----ReadMe.txt(4KB)
----mp3PlayerDlg.h(2KB)
----StdAfx.h(1KB)
----mp3Player.dsp(4KB)
----mp3Player.ncb(81KB)
----mp3Player.clw(2KB)
----mp3.h(712B)
----mp3Player.opt(49KB)
----res()
--------mp3Player.rc2(401B)
--------mp3Player.ico(1KB)
--------Tulips.bmp(769KB)
----mp3PlayerDlg.cpp(9KB)