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

📄 mycdplayerdlg.cpp

📁 本文件中包含的是《VC编程100例》一书中的源代码
💻 CPP
字号:
// MyCDPlayerDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CMyCDPlayerDlg dialog

CMyCDPlayerDlg::CMyCDPlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyCDPlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyCDPlayerDlg)
		// 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);
}

void CMyCDPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyCDPlayerDlg)
	DDX_Control(pDX, IDC_MUSIC_LIST, m_lstMusics);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyCDPlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CMyCDPlayerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_FORWARD, OnForward)
	ON_BN_CLICKED(IDC_BACK, OnBack)
	ON_BN_CLICKED(IDC_SKIPBACK, OnSkipback)
	ON_BN_CLICKED(IDC_SKIPFORWARD, OnSkipforward)
	ON_CBN_SELCHANGE(IDC_MUSIC_LIST, OnSelchangeMusicList)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyCDPlayerDlg message handlers

BOOL CMyCDPlayerDlg::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

	//打开设备
	m_CDAudio.Open();
	//创建一个定时器
	SetTimer( 1, 1000, NULL );
	//将曲目加到CComboBox控件中
	for(int i=0;i<m_CDAudio.GetTotalTracks();i++)
	{
		CString str;
		str.Format("曲目%d",i+1);
		m_lstMusics.AddString(str);
	}
	m_lstMusics.SetCurSel(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyCDPlayerDlg::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 CMyCDPlayerDlg::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 CMyCDPlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CMyCDPlayerDlg::OnDestroy() 
{
	//杀掉定时器
	KillTimer(1);
	CDialog::OnDestroy();	
}
void CMyCDPlayerDlg::OnTimer(UINT nIDEvent) 
{	
	BOOL bDriveReady = TRUE;

	//获得当前曲目已经播放的时间,并显示
	CString strStatus;
	strStatus.Format( "[%d] %02d:%02d",
		m_CDAudio.GetCurrentTrack(),
		m_CDAudio.GetMinutes(),
		m_CDAudio.GetSeconds() );

	//没有CD
	if( m_CDAudio.GetCurrentTrack() == -1 ){
		strStatus = "没有CD";
		bDriveReady = FALSE;
		}
	SetDlgItemText( IDC_TRACKINFO, strStatus );

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

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

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

	BOOL bPaused;
	if( m_CDAudio.IsPlaying( &bPaused ) ){
		pWnd = GetDlgItem( IDC_PLAY );
		pWnd->EnableWindow( bPaused );
		pWnd = GetDlgItem( IDC_STOP );
		pWnd->EnableWindow( bDriveReady );
		pWnd = GetDlgItem( IDC_PAUSE );
		pWnd->EnableWindow( bDriveReady && !bPaused );
		}
	else{
		pWnd = GetDlgItem( IDC_PLAY );
		pWnd->EnableWindow( bDriveReady );
		pWnd = GetDlgItem( IDC_STOP );
		pWnd->EnableWindow( FALSE );
		pWnd = GetDlgItem( IDC_PAUSE );
		pWnd->EnableWindow( FALSE );
		}

	CDialog::OnTimer(nIDEvent);
}

void CMyCDPlayerDlg::OnPlay() 
{
	// 如果光驱中的CD已经准备好就播放
	if( m_CDAudio.IsDriveReady() )
		m_CDAudio.Play();
}

void CMyCDPlayerDlg::OnStop() 
{
	// 停止播放
	m_CDAudio.Stop();
}

void CMyCDPlayerDlg::OnPause() 
{
	// 暂停播放
	m_CDAudio.Pause();
}

void CMyCDPlayerDlg::OnForward() 
{
	
	// 获得当前曲目的播放长度
	int nMinutes = m_CDAudio.GetMinutes();
	int nSeconds = m_CDAudio.GetSeconds();
	int nTrack = m_CDAudio.GetCurrentTrack();

	if( nMinutes == -1 )
		return;

	//向前跳跃5秒,保证没有出界
	nSeconds += 5;
	if( nSeconds > 59 ){
		nMinutes++;
		nSeconds -= 60;
		}

	int nTrackMinutes, nTrackSeconds;
	m_CDAudio.GetTrackLength( nTrack,
		&nTrackMinutes, &nTrackSeconds );

	if( nMinutes * 60 + nSeconds >
		nTrackMinutes * 60 + nTrackSeconds ){
		nMinutes = nTrackMinutes;
		nSeconds = nTrackSeconds;
		}

	//定位到新的位置
	m_CDAudio.SeekTo( nTrack,
		nMinutes, nSeconds, 0 );
}

void CMyCDPlayerDlg::OnBack() 
{
	// 获得当前曲目的播放长度
	int nMinutes = m_CDAudio.GetMinutes();
	int nSeconds = m_CDAudio.GetSeconds();
	int nTrack = m_CDAudio.GetCurrentTrack();

	if( nMinutes == -1 )
		return;

	//向前跳跃5秒,保证没有出界
	nSeconds -= 5;
	if( nSeconds < 0 ){
		nMinutes--;
		nSeconds += 60;
		if( nMinutes < 0 )
			nMinutes = 0;
		}

	//定位到新的位置
	m_CDAudio.SeekTo( nTrack,
		nMinutes, nSeconds, 0 );
}

void CMyCDPlayerDlg::OnSkipback() 
{
	//向后跳跃一个曲目
	int nTrack = m_CDAudio.GetCurrentTrack() - 1;

	if( nTrack < 1 )
		nTrack = m_CDAudio.GetTotalTracks();

	m_CDAudio.SeekTo( nTrack, 0, 0, 0 );
}

void CMyCDPlayerDlg::OnSkipforward() 
{
	//向前跳跃一个曲目
	int nTrack = m_CDAudio.GetCurrentTrack() + 1;

	if( nTrack > m_CDAudio.GetTotalTracks() )
		nTrack = 1;

	m_CDAudio.SeekTo( nTrack, 0, 0, 0 );
}

void CMyCDPlayerDlg::OnSelchangeMusicList() 
{
	//当CComboBox的曲目选择改变时,播放被选择曲目
	int nSelTrack = m_lstMusics.GetCurSel();
	m_CDAudio.SeekTo(nSelTrack+1,0,0,0);
}


⌨️ 快捷键说明

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