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

📄 playcddlg.cpp

📁 VC++高级编程技巧与示例
💻 CPP
字号:
// PlayCDDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CPlayCDDlg dialog

CPlayCDDlg::CPlayCDDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlayCDDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlayCDDlg)
		// 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 CPlayCDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlayCDDlg)
	DDX_Control(pDX, IDC_SELECT, m_Select);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPlayCDDlg, CDialog)
	//{{AFX_MSG_MAP(CPlayCDDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BACKWARD, OnBackward)
	ON_BN_CLICKED(IDC_FORWARD, OnForward)
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_OPENCDDOOR, OnOpencddoor)
	ON_BN_CLICKED(IDC_CLOSECDDOOR, OnClosecddoor)
	ON_BN_CLICKED(IDC_PREV, OnPrev)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_WM_DESTROY()
	ON_CBN_SELCHANGE(IDC_SELECT, OnSelchangeSelect)
	//}}AFX_MSG_MAP
	ON_WM_DEVICECHANGE()
	ON_MESSAGE(MM_MCINOTIFY, OnMciNotify)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayCDDlg message handlers

BOOL CPlayCDDlg::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
	//●:
	try
	{
		m_NOS=0;
		m_Playing=FALSE;
		m_Audio.Open(_PLAYCD,"",this);
		m_Audio.SetTimeFormat(MCI_FORMAT_TMSF);
		if(!m_Audio.GetCDInDrive())
		{
			AfxMessageBox("请插入CD光碟");
			m_Audio.Close();
			m_Playing=FALSE;
			return TRUE;
		}
		m_NOS=m_Audio.GetCDNumOfSongs();
		CString str,sm,ss;
		int m,s,f;
		for(int i=0;i<m_NOS;i++)
		{
			str.Format("★ 第%d首歌曲 ★ 歌曲长度:",i+1);
			m_Audio.GetCDTrackLength(i+1,m,s,f);
			sm.Format("%d分钟",m);
			ss.Format("%d秒",s);
			str=str+sm+ss;
			m_Select.AddString(str);
		}
		m_Select.SetCurSel(0);
		m_CurSel=0;
		m_Playing=TRUE;
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//●:
//
void CPlayCDDlg::OnBackward() 
{
	// TODO: Add your control notification handler code here
	if(!m_Playing)
	{
		AfxMessageBox("尚未打开CD设备!");
		return;
	}
	try
	{
		m_Audio.Rewind(5);
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//前进:
void CPlayCDDlg::OnForward() 
{
	// TODO: Add your control notification handler code here
	if(!m_Playing)
	{
		AfxMessageBox("尚未打开CD设备!");
		return;
	}
	try
	{
		m_Audio.Forward(5);
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//上一首:
void CPlayCDDlg::OnPrev() 
{
	// TODO: Add your control notification handler code here
	try
	{
		m_CurSel=(m_CurSel==0)?0:m_CurSel-1;
		m_Select.SetCurSel(m_CurSel);
		m_Audio.PlayCD(m_CurSel+1,0,0,0);
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//下一首:
void CPlayCDDlg::OnNext() 
{
	// TODO: Add your control notification handler code here
	try
	{
		m_CurSel=(m_CurSel==m_NOS-1)?m_NOS-1:m_CurSel+1;
		m_Select.SetCurSel(m_CurSel);
		m_Audio.PlayCD(m_CurSel+1,0,0,0);
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//播放:
void CPlayCDDlg::OnPlay() 
{
	// TODO: Add your control notification handler code here
	if(!m_Playing)
	{
		AfxMessageBox("尚未打开CD设备!");
		return;
	}
	try
	{
		m_Audio.PlayCD(m_CurSel+1,0,0,0);
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//停止:
void CPlayCDDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	if(!m_Playing)
	{
		AfxMessageBox("尚未打开CD设备!");
		return;
	}
	try
	{
		m_Audio.Stop();
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//打开光驱门:
void CPlayCDDlg::OnOpencddoor() 
{
	// TODO: Add your control notification handler code here
	try
	{
		m_Audio.OpenCDDoor();
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//关闭光驱门:
void CPlayCDDlg::OnClosecddoor() 
{
	// TODO: Add your control notification handler code here
	try
	{
		m_Audio.CloseCDDoor();
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//退出:
void CPlayCDDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if(m_Playing)
	{
		try
		{
		m_Audio.Stop();
		m_Audio.Close();
		}
		catch(CAudioException * e)
		{
			AfxMessageBox(e->GetErrorMessage());
   		    delete e;
			return;
		}
	}
}

//当用户选择了另外的一首音轨时:
void CPlayCDDlg::OnSelchangeSelect() 
{
	// TODO: Add your control notification handler code here
	if(!m_Playing)
	{
		AfxMessageBox("尚未打开CD设备!");
		return;
	}
	try
	{
		m_CurSel=m_Select.GetCurSel();
		m_Audio.PlayCD(m_CurSel+1,0,0,0);
	}
	catch(CAudioException * e)
	{
		AfxMessageBox(e->GetErrorMessage());
		delete e;
		return;
	}
}

//处理MCI传来的消息:
long CPlayCDDlg::OnMciNotify(UINT wFlags, LONG lDevId)
{
	switch(wFlags) 
	{
		case MCI_NOTIFY_SUCCESSFUL:
			{
				m_CurSel=(m_CurSel==m_NOS-1)?m_CurSel:m_CurSel+1;
				m_Select.SetCurSel(m_CurSel);
				m_Audio.PlayCD(m_CurSel+1,0,0,0);
				break;
			}
		case MCI_NOTIFY_FAILURE:
			{
				break;
			}
		case MCI_NOTIFY_SUPERSEDED:
			{
				break;
			}
	}	
	return 0L;
}

BOOL CPlayCDDlg::OnDeviceChange(UINT nEventType,DWORD dwData)
{
	DEV_BROADCAST_HDR * pHeader = (DEV_BROADCAST_HDR *) dwData;
	switch(nEventType) 
	{
		//CD门关闭:
		case DBT_DEVICEARRIVAL:
			if (pHeader->dbch_devicetype == DBT_DEVTYP_VOLUME) 
			{
				PDEV_BROADCAST_VOLUME pVol=(PDEV_BROADCAST_VOLUME) dwData;
				if (pVol->dbcv_flags == DBTF_MEDIA)
				{
					TCHAR driver[4];
					//读取系统中最后一个驱动器号:
					for (UINT i=0; !(pVol->dbcv_unitmask & (1<<i)); i++);
					wsprintf(driver, _T("%c:\\"), 'A'+i);		
					//判断最后的一个驱动器是否为CDROM:
					if (GetDriveType(driver)==DRIVE_CDROM) 
					{
						//如果是CDROM,则重新打开CD设备:
						try
						{
							m_NOS=0;
							if(!m_Playing)
							{
								m_Audio.Open(_PLAYCD,"",this);
								m_Audio.SetTimeFormat(MCI_FORMAT_TMSF);
							}
							if(!m_Audio.GetCDInDrive())
							{
								AfxMessageBox("请插入CD光碟");
								return TRUE;
							}
							m_NOS=m_Audio.GetCDNumOfSongs();
							CString str,sm,ss;
							int m,s,f;
							for(int i=0;i<m_NOS;i++)
							{
								str.Format("★ 第%d首歌曲 ★ 歌曲长度:",i+1);
								m_Audio.GetCDTrackLength(i+1,m,s,f);
								sm.Format("%d分钟",m);
								ss.Format("%d秒",s);
								str=str+sm+ss;
								m_Select.AddString(str);
							}
							m_Select.SetCurSel(0);
							m_CurSel=0;
							m_Playing=TRUE;
						}
						catch(CAudioException * e)
						{
							AfxMessageBox(e->GetErrorMessage());
 		                    delete e;
						}
					}
				}
				break;
			}

		//CD门打开:
		case DBT_DEVICEREMOVECOMPLETE:
			{
				//删除信息列表:
				while(m_Select.DeleteString(0));
				m_NOS=0;
			}
		break;
	}
	return TRUE;
}

⌨️ 快捷键说明

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