⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3playerdlg.cpp

📁 VisualC高级编程技术精粹.rar
💻 CPP
字号:
// Mp3PlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Mp3Player.h"
#include "Mp3PlayerDlg.h"


#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()

/////////////////////////////////////////////////////////////////////////////
// CMp3PlayerDlg dialog

CMp3PlayerDlg::CMp3PlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMp3PlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMp3PlayerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_csInfo = _T("");
	m_csPathName = _T("");
	m_csSongName = _T("");
	m_bIsPressPauseBtn = FALSE;
	m_bIsPressStopBtn = FALSE;
	m_bIsOpened = FALSE;
	m_bIsNoSelectFile = FALSE;
	m_bIsPauseOpen = FALSE;	
}

void CMp3PlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMp3PlayerDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMp3PlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CMp3PlayerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
	ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnButtonPause)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_QUIT, OnButtonQuit)
	ON_BN_CLICKED(IDC_BUTTON_VOLUME, OnButtonVolume)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMp3PlayerDlg message handlers

BOOL CMp3PlayerDlg::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
	
	// TODO: Add extra initialization here	
	DisplayInfo(0,_T("欢迎使用Mp3Player!"),_T(""));

	GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMp3PlayerDlg::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 CMp3PlayerDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMp3PlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMp3PlayerDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	char szFileFilter[]="All Music Files(*.*)|*.*||"; 
	CFileDialog FileOpenDlg(TRUE,NULL,NULL,
		                    OFN_HIDEREADONLY,
							szFileFilter/*, this*/);
	if( FileOpenDlg.DoModal() == IDOK )
	{
		m_csPathName = FileOpenDlg.GetPathName();
		m_csSongName = FileOpenDlg.GetFileName();
		DisplayInfo(1,_T("你选择的歌曲是:"),m_csSongName);

		m_bIsOpened = TRUE;	
		m_bIsNoSelectFile = FALSE;
	}
	else
	{
		m_bIsNoSelectFile = TRUE;
	}

	if (m_bIsOpened && m_mcimp3.MCIIsPlaying())
	{
		m_mcimp3.MCIClose();		
		m_mcimp3.MCIStop();
	}

	if (m_bIsOpened && m_mcimp3.MCIIsPlaying() 
		&& m_bIsNoSelectFile)
	{		
		m_mcimp3.MCIResume();
	}

	if (m_bIsOpened && m_bIsPressPauseBtn)
	{
		m_bIsPauseOpen = TRUE;
	}

	BOOL bIsPlayBtnEnabled = GetDlgItem(
		                     IDC_BUTTON_PLAY)->IsWindowEnabled();
	if (!bIsPlayBtnEnabled)
	{
		GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow(TRUE);
	}	
}

void CMp3PlayerDlg::OnButtonPlay() 
{
	// TODO: Add your control notification handler code here			

	if (m_bIsPauseOpen)
	{
		if (m_bIsNoSelectFile)
		{
			m_mcimp3.MCIResume();
			DisplayInfo(1,_T("继续播放歌曲:"),m_csSongName);
			m_bIsPressPauseBtn = FALSE;
		}
		else
		{
			m_mcimp3.MCIOpen(m_csPathName);
			m_mcimp3.MCIPlay();	
			DisplayInfo(1,_T("正在播放的歌曲是:"),m_csSongName);			

			GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow(FALSE);

			m_bIsNoSelectFile = TRUE;
		}

	}
	else if (m_bIsOpened && m_bIsPressPauseBtn)
	{
		m_mcimp3.MCIPlay();
		DisplayInfo(1,_T("继续播放歌曲:"),m_csSongName);
		m_bIsPressPauseBtn = FALSE;

		GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow(FALSE);
	}
	else
	{
		if (m_bIsNoSelectFile)
		{
			DisplayInfo(0,_T("你还没有选择歌曲,点击播放没有意义!"),_T(""));
			return;
		}

		m_mcimp3.MCIOpen(m_csPathName);
		m_mcimp3.MCIGetLength(&m_dwLen);
		m_mcimp3.MCIPlay();	
		SetTimer(1, 1000, NULL);
		DisplayInfo(1,_T("正在播放的歌曲是:"),m_csSongName);

		m_bIsPressStopBtn = FALSE;

		GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow(FALSE);
	} 
	
	BOOL bIsPauseBtnEnabled = GetDlgItem(
		                          IDC_BUTTON_PAUSE)->IsWindowEnabled();
	if (!bIsPauseBtnEnabled)
	{
		GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(TRUE);
	}

	BOOL bIsStopBtnEnabled = GetDlgItem(
		                          IDC_BUTTON_STOP)->IsWindowEnabled();
	if (!bIsStopBtnEnabled)
	{
		GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(TRUE);
	}
}

void CMp3PlayerDlg::OnButtonPause() 
{
	// TODO: Add your control notification handler code here
	BOOL bIsPlaying = m_mcimp3.MCIIsPlaying();
	if (bIsPlaying)
	{
		m_mcimp3.MCIPause();
		DisplayInfo(1,_T("暂停播放歌曲:"),m_csSongName);

		m_bIsPressPauseBtn = TRUE;
	}

	BOOL bIsPlayBtnEnabled = GetDlgItem(
		                         IDC_BUTTON_PLAY)->IsWindowEnabled();
	if (!bIsPlayBtnEnabled)
	{
		GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow(TRUE);
	}
}

void CMp3PlayerDlg::OnButtonStop() 
{
	// TODO: Add your control notification handler code here
	if (m_mcimp3.MCIStop()==0)	//停止播放
	{	
		m_bIsPressStopBtn = TRUE;
		m_bIsPressPauseBtn = FALSE;
		GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
		DisplayInfo(1,_T("停止播放歌曲:"),m_csSongName);

		m_mcimp3.MCIClose();
		GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(FALSE);
	}
	
	BOOL bIsPlayBtnEnabled = GetDlgItem(
		                         IDC_BUTTON_PLAY)->IsWindowEnabled();
	if (!bIsPlayBtnEnabled)
	{
		GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow(TRUE);
	}
}

void CMp3PlayerDlg::OnButtonQuit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void CMp3PlayerDlg::OnButtonVolume() 
{
	// TODO: Add your control notification handler code here
	WinExec( "C:\\WINDOWS\\System32\\sndvol32.exe", SW_NORMAL );	
}

void CMp3PlayerDlg::DisplayInfo(bool bOPInfo,
                                CString csInfo,
								CString csSongName)
{
	if (!bOPInfo)
	{
		m_csInfo = csInfo;
	}
	else
	{
		m_csInfo = csInfo;
		m_csInfo += csSongName;
	}
	
	GetDlgItem(IDC_STATIC_INFO)->SetWindowText(m_csInfo);
}

void CMp3PlayerDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if ( nIDEvent == 1)
	{		
		if ( 0 == m_dwLen )
        {
            KillTimer(1);
            DisplayInfo(1,_T("已经播放完歌曲:"),m_csSongName);  
			GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(FALSE);
			GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
			
            return;
        }
        m_dwLen--;
	}
	
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -