// 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;//指定播放音乐设备的ID
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))
{
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;
mciSendCommand(DeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciplayparms);
}
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 setvolumn(DWORD vol)
{
MCI_DGV_SETAUDIO_PARMS mcisetvolumn;
mcisetvolumn.dwCallback=NULL;
mcisetvolumn.dwItem=MCI_DGV_SETAUDIO_VOLUME;
mcisetvolumn.dwValue=vol;
mciSendCommand(DeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAUDIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn);
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMfcDlg dialog
CMfcDlg::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 Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}
void CMfcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMfcDlg)
DDX_Control(pDX, IDC_SLIDER1, 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_EXIT, OnExit)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)
ON_BN_CLICKED(IDC_flie, Onflie)
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 handlers
BOOL 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 dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_slider.SetRange(0,1000);
m_slider.SetPos(500);
GetDlgItem(IDC_PLAY)->EnableWindow(false);
GetDlgItem(IDC_PAUSE)->EnableWindow(false);
GetDlgItem(IDC_STOP)->EnableWindow(false);
// TODO: Add extra initialization here
return 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 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
{
//
// 给窗体添加背景
//
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1);
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap* pbmpPri = dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rc.Width(), rc.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()
{
// TODO: Add your control notification handler code here
CAboutDlg dlg;
dlg.DoModal();
}
void CMfcDlg::OnExit()
{
// TODO: Add your control notification handler code here
CDialog : OnCancel();
}
void CMfcDlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_int=m_slider.GetPos();
setvolumn(m_int);
UpdateData(false);
*pResult = 0;
}
void CMfcDlg::Onflie()
{
// TODO: Add your control notification handler code here
char szFlieFilter[]="mp3文件(*.mp3)|*.mp3|"
"wma文件(*.wma)|*.wma|"
"wav文件(*.wav)|*.wav|"
"所有文件(*.*)|*.*|";
CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY,szFlieFilter);
if(dlg.DoModal()==IDOK)
{
CString strFilepath=dlg.GetPathName();
CString strFilename=dlg.GetFileName();
SetDlgItemText(IDC_dizhi,strFilename);
Load(this->m_hWnd,strFilepath);
GetDlgItem(IDC_PLAY)->EnableWindow(true);
GetDlgItem(IDC_PAUSE)->EnableWindow(true);
GetDlgItem(IDC_STOP)->EnableWindow(true);
}
}
void CMfcDlg::OnPlay()
{
// TODO: Add your control notification handler code here
play();
}
void CMfcDlg::OnPause()
{
// TODO: Add your control notification handler code here
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()
{
// TOD