// MFC音乐播放器Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "MFC音乐播放器.h"
#include "MFC音乐播放器Dlg.h"
#include "Mmsystem.h"
#include "Digitalv.h"
#pragma comment(lib,"Winmm.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif//定义三个全局变量
HWND m_hWnd;//记录当前窗口的句柄
DWORD DeviceID;//指定播放音乐的设备
MCI_OPEN_PARMS mciopenparms;//指定打开音乐文件的参数void 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;//打开文件成功就关联文件到设备
}void play()
{MCI_PLAY_PARMS mciplayparms;mciplayparms.dwCallback=(DWORD)m_hWnd;mciplayparms.dwFrom=0;//每次播放都是从0开始播放 dwFrom用于设置音乐播放的起始位置mciSendCommand(DeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciplayparms);//dwCallback与MCI_NOTIFY是配对出现的
}void pause()
{mciSendCommand(DeviceID,MCI_PAUSE,0,0);
}
void resume()
{mciSendCommand(DeviceID,MCI_RESUME,0,0);
}
void stop()
{mciSendCommand(DeviceID,MCI_STOP,0,0);//当点击停止按钮时,将所有的信息都清除掉mciSendCommand(DeviceID,MCI_CLOSE,0,0);//点关闭按钮的时候,清除设备信息,再点播放按钮,就播放不了音乐了}
DWORD setvolume(DWORD vol)
{MCI_DGV_SETAUDIO_PARMS setvolume;//这是设置音量的参数数据结构setvolume.dwCallback=NULL;setvolume.dwItem=MCI_DGV_SETAUDIO_VOLUME;//动作是设置音量setvolume.dwValue=vol;//音量值是volmciSendCommand(DeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_ITEM|MCI_DGV_SETAUDIO_VALUE,(DWORD)(LPVOID)&setvolume);return 0;
}/
// CAboutDlg dialog used for App Aboutclass 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_MSGDECLARE_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()/
// CMFCDlg dialogCMFCDlg::CMFCDlg(CWnd* pParent /*=NULL*/): CDialog(CMFCDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CMFCDlg)m_int = 0;//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CMFCDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CMFCDlg)DDX_Control(pDX, IDC_SLIDER, m_slider);DDX_Text(pDX, IDC_vol, m_int);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CMFCDlg, CDialog)//{{AFX_MSG_MAP(CMFCDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_about, Onabout)ON_BN_CLICKED(IDC_exitbt, Onexitbt)ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER, OnCustomdrawSlider)ON_BN_CLICKED(IDC_filechoice, Onfilechoice)ON_BN_CLICKED(IDC_play, Onplay)ON_BN_CLICKED(IDC_pause, Onpause)ON_BN_CLICKED(IDC_stop, Onstop)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CMFCDlg message handlersBOOL CMFCDlg::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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small iconm_slider.SetRange(0,1000);//滑块的移动范围0~1000m_slider.SetPos(500);//滑块指针的初始位置设为500代表初始音量为50GetDlgItem(IDC_play)->EnableWindow(false);//文件没有读取时所有按钮应该是不可选的GetDlgItem(IDC_pause)->EnableWindow(false);GetDlgItem(IDC_stop)->EnableWindow(false);// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control
}void CMFCDlg::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 CMFCDlg::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_BITMAP); //IDB_BITMAP:自己导入位图的IDBITMAP 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 CMFCDlg::OnQueryDragIcon()
{return (HCURSOR) m_hIcon;
}void CMFCDlg::Onabout()
{CAboutDlg dlg;dlg.DoModal();
}void CMFCDlg::Onexitbt() //退出软件按钮的函数
{CDialog::OnCancel();
}void CMFCDlg::OnCustomdrawSlider(NMHDR* pNMHDR, LRESULT* pResult)
{UpdateData(true);m_int=m_slider.GetPos()/10;setvolume(m_slider.GetPos());UpdateData(false);*pResult = 0;
}void CMFCDlg::Onfilechoice()
{char szFileFilter[]="mp3文件(*.mp3)|*.mp3|""wma文件(*.wma)|*.wma|""wav文件(*.wav)|*.wav|""所有文件(*.*)|*.*|";//文件类型过滤 CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY,szFileFilter);if(dlg.DoModal()==IDOK){CString strFilepath=dlg.GetPathName();CString strFilename=dlg.GetFileName();SetDlgItemText(IDC_filename,strFilename);Load(this->m_hWnd,strFilepath);//文件读取成功时所有按钮变为可选GetDlgItem(IDC_play)->EnableWindow(true);GetDlgItem(IDC_pause)->EnableWindow(true);GetDlgItem(IDC_stop)->EnableWindow(true);}
}void CMFCDlg::Onplay()
{play();SetDlgItemText(IDC_pause,"暂停");
}void CMFCDlg::Onpause()
{CString strtemp;GetDlgItemText(IDC_pause,strtemp);//获取按钮状态if(strtemp.Compare("暂停")==0){pause();SetDlgItemText(IDC_pause,"恢复");} if(strtemp.Compare("恢复")==0){resume();SetDlgItemText(IDC_pause,"暂停");}
}void CMFCDlg::Onstop()
{stop(); SetDlgItemText(IDC_pause,"暂停");GetDlgItem(IDC_play)->EnableWindow(false);//当stop按钮按下时,播放和暂停应该均不可用GetDlgItem(IDC_pause)->EnableWindow(false);
}
头文件如下:
// MFC音乐播放器Dlg.h : header file
//#if !defined(AFX_MFCDLG_H__207E26CE_BBBE_4D81_8A38_AEA46E10AB5B__INCLUDED_)
#define AFX_MFCDLG_H__207E26CE_BBBE_4D81_8A38_AEA46E10AB5B__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/
// CMFCDlg dialogclass CMFCDlg : public CDialog
{
// Construction
public:CMFCDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CMFCDlg)enum { IDD = IDD_MFC_DIALOG };CSliderCtrl m_slider;int m_int;//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CMFCDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CMFCDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void Onabout();afx_msg void Onexitbt();afx_msg void OnCustomdrawSlider(NMHDR* pNMHDR, LRESULT* pResult);afx_msg void Onfilechoice();afx_msg void Onplay();afx_msg void Onpause();afx_msg void Onstop();//}}AFX_MSGDECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MFCDLG_H__207E26CE_BBBE_4D81_8A38_AEA46E10AB5B__INCLUDED_)
最后运行效果如下: