MFC制作简易音乐播放器
生活随笔
收集整理的這篇文章主要介紹了
MFC制作简易音乐播放器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 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_)
最后運行效果如下:
總結
以上是生活随笔為你收集整理的MFC制作简易音乐播放器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (Python+Tensorflow)编
- 下一篇: NSIS脚本解释