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

📄 mymediaplayerdlg.cpp

📁 又是一个新的CD播放器的代码,有不错的学习效果
💻 CPP
字号:
// MyMediaPlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyMediaPlayer.h"
#include "MyMediaPlayerDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMyMediaPlayerDlg dialog

CMyMediaPlayerDlg::CMyMediaPlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyMediaPlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyMediaPlayerDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_VolumeCtrl = new CVolumeCtrl(this);

}

CMyMediaPlayerDlg::~CMyMediaPlayerDlg()
{

}

void CMyMediaPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyMediaPlayerDlg)
	DDX_Control(pDX, IDC_SLIDER_VOLUME, m_sliderVolume);
	DDX_Control(pDX, IDC_TIME, m_msgTime);
	DDX_Control(pDX, IDC_CAPTION, m_msgCaption);
	DDX_Control(pDX, IDC_NEXT, m_btnNext);
	DDX_Control(pDX, IDC_STOP, m_btnStop);
	DDX_Control(pDX, IDC_PREF, m_btnPref);
	DDX_Control(pDX, IDC_RESUME, m_btnResume);
	DDX_Control(pDX, IDC_PLAYCD, m_btnPlayCD);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyMediaPlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CMyMediaPlayerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PLAYCD, OnPlaycd)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_RESUME, OnResume)
	ON_BN_CLICKED(IDC_PREF, OnPref)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_VOLUME, OnReleasedcaptureSliderVolume)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyMediaPlayerDlg message handlers

BOOL CMyMediaPlayerDlg::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
	if(m_VolumeCtrl->amdInitialize()) {
		if(m_VolumeCtrl->amdGetVolumeControl()) {
			m_VolumeCtrl->GetVolume(m_nVolume);
		}
		else {
			MessageBox("amdGetVolumeControl failed");
		}
	}
	else {
		MessageBox("amdInitialize failed");
	}

/*	m_msgCaption.SetWindowText("");
	m_msgTime   .SetWindowText("");

	m_btnResume.EnableWindow(FALSE);
	m_btnStop.EnableWindow(FALSE);
	m_btnNext.EnableWindow(FALSE);
	m_btnPref.EnableWindow(FALSE);

	m_btnPlayCD .EnableWindow(TRUE);
*/
    //打开设备
    m_CDPlayer.Open();
	//创建一个定时器
	SetTimer( 1, 1000, NULL );
	
    //初始化滑动块控件
	m_sliderVolume.SetRangeMin(0);
	m_sliderVolume.SetRangeMax(65000);	//65535
	m_sliderVolume.SetLineSize(100);
	m_sliderVolume.SetPageSize(1000);

	//设置滑动块控件的值
	DWORD nVolume ;
	m_VolumeCtrl->GetVolume(nVolume);
	m_sliderVolume.SetPos(nVolume);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyMediaPlayerDlg::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 CMyMediaPlayerDlg::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 CMyMediaPlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyMediaPlayerDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	// CDialog::OnOK();
}

void CMyMediaPlayerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CMyMediaPlayerDlg::OnPlaycd() 
{
	m_CDPlayer.Play();

	m_btnResume.EnableWindow(TRUE);
	m_btnStop.EnableWindow(TRUE);
	m_btnNext.EnableWindow(TRUE);
	m_btnPref.EnableWindow(TRUE);

	m_btnPlayCD .EnableWindow(FALSE);

	SetTimer(1,1000,NULL);
}

/*void CMyMediaPlayerDlg::OnTimer(UINT nIDEvent) 
{
	int nTotalTracks   = m_CDPlayer.GetTotalTracks ();
	int nCurrentTracks = m_CDPlayer.GetCurrentTrack();
	int nMinute = m_CDPlayer.GetMinutes();
	int nSecond = m_CDPlayer.GetSeconds();

	CString strCaption ;
	strCaption.Format("共%d首 第%d首\t\t正在播放",nTotalTracks,nCurrentTracks);
	m_msgCaption.SetWindowText(strCaption);
	
	CString strTime;
	strTime.Format("%d分%d秒",nMinute,nSecond);
	m_msgTime.SetWindowText(strTime);

	CDialog::OnTimer(nIDEvent);
}
*/

void CMyMediaPlayerDlg::OnTimer(UINT nIDEvent) 
{	
	BOOL bDriveReady = TRUE;
	int nTotalTracks   = m_CDPlayer.GetTotalTracks ();
    int nCurrTracks   = m_CDPlayer.GetCurrentTrack();
//	int nMinute = m_CDPlayer.GetMinutes();
//	int nSecond = m_CDPlayer.GetSeconds();

	CString strCaption ;
	strCaption.Format("共 %d 首 ",nTotalTracks);
	m_msgCaption.SetWindowText(strCaption);

	//获得当前曲目已经播放的时间,并显示
	CString strStatus,strCurr;
    strStatus.Format( " %02d:%02d",
	m_CDPlayer.GetMinutes(),
	m_CDPlayer.GetSeconds() );
	nCurrTracks = m_CDPlayer.GetCurrentTrack();
	
	//没有CD
	if( m_CDPlayer.GetCurrentTrack() == -1 )
	{
		strStatus = "请插入CD";
		nCurrTracks = 0;
		bDriveReady = FALSE;
	}
	SetDlgItemText( IDC_TRACKINFO, strStatus );  
	strCurr.Format(" 第%d首正在播放 ",nCurrTracks);
    if(nCurrTracks > 0)
    {
        SetDlgItemText( IDC_CURR,strCurr);
    }
   

	//获得CD的总共长度,并显示
	CString strLength;
	int nMinutes, nSeconds;
	m_CDPlayer.GetTotalLength( &nMinutes,
		&nSeconds );
	strLength.Format( "总长度: %02d:%02d",
		nMinutes, nSeconds );
	if( nMinutes == -1 )
		strLength = "总长度: 00:00";
	SetDlgItemText( IDC_TOTALTIME, strLength );

	//获得当前们曲目长度,并显示
	m_CDPlayer.GetTrackLength(
		m_CDPlayer.GetCurrentTrack(),
		&nMinutes, &nSeconds );
	strLength.Format( " %02d:%02d",
		nMinutes, nSeconds );
	if( nMinutes == -1 )
		strLength = " 00:00";
	SetDlgItemText( IDC_TIME, strLength );

	//根据播放状态改变按钮的可用与不可用状态
	CWnd *pWnd;
	pWnd = GetDlgItem( IDC_PREF );
	pWnd->EnableWindow( bDriveReady );
	pWnd = GetDlgItem( IDC_NEXT );
	pWnd->EnableWindow( bDriveReady );

	BOOL bPaused;
	if( m_CDPlayer.IsPlaying( &bPaused ) ){
		pWnd = GetDlgItem( IDC_PLAYCD );
		pWnd->EnableWindow( bPaused );
		pWnd = GetDlgItem( IDC_STOP );
		pWnd->EnableWindow( bDriveReady );
		pWnd = GetDlgItem( IDC_RESUME );
		pWnd->EnableWindow( bDriveReady && !bPaused );
		}
	else{
		pWnd = GetDlgItem( IDC_PLAYCD );
		pWnd->EnableWindow( bDriveReady );
		pWnd = GetDlgItem( IDC_STOP );
		pWnd->EnableWindow( FALSE );
		pWnd = GetDlgItem( IDC_RESUME );
		pWnd->EnableWindow( FALSE );
		}

	CDialog::OnTimer(nIDEvent);
}


void CMyMediaPlayerDlg::OnStop() 
{
	m_CDPlayer.Stop();

	KillTimer(1);
    //设置按键
	m_btnResume.EnableWindow(FALSE);
	m_btnStop.EnableWindow(FALSE);
	
	m_btnNext.EnableWindow(TRUE);
	m_btnPref.EnableWindow(TRUE);
	m_btnPlayCD .EnableWindow(TRUE);
	//设置显示
	CString strCurr ;
	strCurr.Format(" 停止播放 ");
	SetDlgItemText( IDC_CURR,strCurr);
	
	CString strTime;
	strTime.Format("%d分%d秒",0,0);
	m_msgTime.SetWindowText(strTime);
}

void CMyMediaPlayerDlg::OnResume() 
{
	m_CDPlayer.Pause();
	//设置显示
	CString strCurr ;
	strCurr.Format(" 暂停播放 ");
	SetDlgItemText( IDC_CURR,strCurr);
    //设置按键
	m_btnResume.EnableWindow(FALSE);
	m_btnStop.EnableWindow(FALSE);

	m_btnNext.EnableWindow(TRUE);
	m_btnPref.EnableWindow(TRUE);
	m_btnPlayCD .EnableWindow(TRUE);
}

void CMyMediaPlayerDlg::OnPref() 
{
	//向前跳跃一个曲目
	int nCurrentTracks = m_CDPlayer.GetCurrentTrack() - 1;

	if( nCurrentTracks < 1 )
		nCurrentTracks = m_CDPlayer.GetTotalTracks();

	m_CDPlayer.SeekTo( nCurrentTracks, 0, 0, 0 );
}

void CMyMediaPlayerDlg::OnNext() 
{
	//向后跳跃一个曲目
	int nCurrentTracks = m_CDPlayer.GetCurrentTrack() + 1;

	if( nCurrentTracks > m_CDPlayer.GetTotalTracks() )
		nCurrentTracks = 1;

	m_CDPlayer.SeekTo( nCurrentTracks, 0, 0, 0 );
}

void CMyMediaPlayerDlg::OnReleasedcaptureSliderVolume(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CString msg;
	DWORD nVolume = m_sliderVolume.GetPos();  //获取滑动块的位置
	m_VolumeCtrl->SetVolume(nVolume);     //设置系统音量

	//m_VolumeCtrl
	*pResult = 0;
}

⌨️ 快捷键说明

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