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

📄 cdplayerdlg.cpp

📁 我找的VC++实现的CD简易播放器
💻 CPP
字号:
// CDPlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CDPlayer.h"
#include "CDPlayerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCDPlayerDlg dialog

CCDPlayerDlg::CCDPlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCDPlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCDPlayerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	// Open the CD device.
	m_CDAudio.Open();

}

void CCDPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCDPlayerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCDPlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CCDPlayerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CREATE()
	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_EJECT, OnEject)
	ON_BN_CLICKED(IDC_FORWARD, OnForward)
	ON_BN_CLICKED(IDC_BACK, OnBack)
	ON_BN_CLICKED(IDC_SKIPBACK, OnSkipback)
	ON_BN_CLICKED(IDC_SKIPFORWARD, OnSkipforward)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCDPlayerDlg message handlers

BOOL CCDPlayerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CCDPlayerDlg::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();
	}
}

HCURSOR CCDPlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

int CCDPlayerDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Kick off the timer.
	SetTimer( 1, 1000, NULL );

	return 0;
}

void CCDPlayerDlg::OnTimer(UINT nIDEvent) 
{

	BOOL bDriveReady = TRUE;

	// Format an information string. We'll use
	// it to display the track and current time
	// within the track.
	CString strStatus;
	strStatus.Format( "[%d] %02d:%02d",
		m_CDAudio.GetCurrentTrack(),
		m_CDAudio.GetMinutes(),
		m_CDAudio.GetSeconds() );

	// If the current track is -1, that means
	// we have no audio CD. Update the information
	// string to reflect this condition.
	if( m_CDAudio.GetCurrentTrack() == -1 ){
		strStatus = "No CD Audio";
		bDriveReady = FALSE;
		}
	SetDlgItemText( IDC_TRACKINFO, strStatus );

	// The next information string will contain the total
	// time for this CD. We'll use the GetTotalLength()
	// function to obtain the minutes and seconds.
	CString strLength;
	int nMinutes, nSeconds;
	m_CDAudio.GetTotalLength( &nMinutes,
		&nSeconds );
	strLength.Format( "Total Time: %02d:%02d",
		nMinutes, nSeconds );
	if( nMinutes == -1 )
		strLength = "Total Time: 00:00";
	SetDlgItemText( IDC_TOTALTIME, strLength );

	// The next information string will contain the total
	// time for this track. We'll use the GetTrackLength()
	// function to obtain the minutes and seconds.
	m_CDAudio.GetTrackLength(
		m_CDAudio.GetCurrentTrack(),
		&nMinutes, &nSeconds );
	strLength.Format( "Track Length: %02d:%02d",
		nMinutes, nSeconds );
	if( nMinutes == -1 )
		strLength = "Total Time: 00:00";
	SetDlgItemText( IDC_TRACKTIME, strLength );

	// We'll use the boolean flags we've already
	// gotten in this function to update the
	// dialog buttons.
	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 );

	// Update the approriate buttons
	// that depend on whether the CD is playing.
	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 CCDPlayerDlg::OnPlay() 
{
	
	// If the drive is ready, begin the
	// audio playback.
	if( m_CDAudio.IsDriveReady() )
		m_CDAudio.Play();

}

void CCDPlayerDlg::OnStop() 
{
	
	// Stop the playback.
	m_CDAudio.Stop();

}

void CCDPlayerDlg::OnPause() 
{

	// Pause the playback.
	m_CDAudio.Pause();

}

void CCDPlayerDlg::OnEject() 
{
	
	// If the drive is read, close it.
	// Otherwise, attempt to open the drive.
	if( !m_CDAudio.IsDriveReady() )
		m_CDAudio.CloseDrive();
	else m_CDAudio.OpenDrive();

}

void CCDPlayerDlg::OnForward() 
{

	// Get the current minutes, seconds
	// and track.
	int nMinutes = m_CDAudio.GetMinutes();
	int nSeconds = m_CDAudio.GetSeconds();
	int nTrack = m_CDAudio.GetCurrentTrack();

	// If minutes == -1, then the
	// CD device didn't open or there's
	// some other problem.
	if( nMinutes == -1 )
		return;

	// Go forward five seconds. Make
	// sure that the seconds haven't gone
	// out of range.
	nSeconds += 5;
	if( nSeconds > 59 ){
		nMinutes++;
		nSeconds -= 60;
		}

	// Get the length of this track so
	// that we can check to make sure
	// we haven't gone past the end
	// of the track.
	int nTrackMinutes, nTrackSeconds;
	m_CDAudio.GetTrackLength( nTrack,
		&nTrackMinutes, &nTrackSeconds );

	// Check that the new values will be
	// in range, not past the end of the
	// track.
	if( nMinutes * 60 + nSeconds >
		nTrackMinutes * 60 + nTrackSeconds ){
		nMinutes = nTrackMinutes;
		nSeconds = nTrackSeconds;
		}

	// Seek to the new location in this track.
	m_CDAudio.SeekTo( nTrack,
		nMinutes, nSeconds, 0 );

}

void CCDPlayerDlg::OnBack() 
{

	// Get the current minutes, seconds
	// and track.
	int nMinutes = m_CDAudio.GetMinutes();
	int nSeconds = m_CDAudio.GetSeconds();
	int nTrack = m_CDAudio.GetCurrentTrack();

	// If minutes == -1, then the
	// CD device didn't open or there's
	// some other problem.
	if( nMinutes == -1 )
		return;

	// Back up five seconds. Make sure
	// we haven't gone out of range.
	nSeconds -= 5;
	if( nSeconds < 0 ){
		nMinutes--;
		nSeconds += 60;
		if( nMinutes < 0 )
			nMinutes = 0;
		}

	// Seek to the new location in this track.
	m_CDAudio.SeekTo( nTrack,
		nMinutes, nSeconds, 0 );

}

void CCDPlayerDlg::OnSkipback() 
{

	// Get the current track and deccrement.
	int nTrack = m_CDAudio.GetCurrentTrack() - 1;

	// If we're below the lowest allowable
	//track, go up to the highest track.
	if( nTrack < 1 )
		nTrack = m_CDAudio.GetTotalTracks();

	// Seek to the beginninig of the
	// previous track.
	m_CDAudio.SeekTo( nTrack, 0, 0, 0 );

}

void CCDPlayerDlg::OnSkipforward() 
{

	// Get the current track and increment.
	int nTrack = m_CDAudio.GetCurrentTrack() + 1;

	// Get the total number of tracks and
	// make sure nTrack hasn't exceeded it.
	if( nTrack > m_CDAudio.GetTotalTracks() )
		nTrack = 1;

	// Seek to the beginning of the next track.
	m_CDAudio.SeekTo( nTrack, 0, 0, 0 );

}

⌨️ 快捷键说明

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