📄 ch9p1_cdplayer.cpp
字号:
// Ch9p1_CDPlayer.cpp : Defines the entry point for the application.
//
#include <windows.h>
#include <string>
#include <vector>
#include <sstream>
#include "resource.h"
#include "cdplayer.h"
using namespace AudioEngine;
using namespace std;
CCDPlayer g_CDPlayer;
int g_CurTrack = 1;
bool g_cdindrive = false;
void RefreshGUI(HWND hDlg)
{
// gray out buttons if no CD is in drive
EnableWindow(GetDlgItem(hDlg, IDC_PREVIOUSTRACK), g_cdindrive);
EnableWindow(GetDlgItem(hDlg, IDC_PLAY), g_cdindrive);
EnableWindow(GetDlgItem(hDlg, IDC_STOP), g_cdindrive);
EnableWindow(GetDlgItem(hDlg, IDC_NEXTTRACK), g_cdindrive);
// put current track/time into LCD label
HWND label = GetDlgItem(hDlg, IDC_LCD);
char buf[32] = { 0 };
if (g_cdindrive) {
// ask the cd player what the current position is
CTrackMinSecFrame pos = g_CDPlayer.GetCurrentPos();
// if we get a blank pos back, fill in the last track we played
// just to make the GUI look better.
if (pos == CTrackMinSecFrame()) {
pos.m_Track = g_CurTrack;
}
_snprintf(buf, sizeof(buf), "%02d - %02d:%02d", pos.m_Track, pos.m_Minute, pos.m_Second);
// set g_CurTrack in case they stop then play later
g_CurTrack = pos.m_Track;
// set label on play button
if (g_CDPlayer.IsPlaying()) {
SendMessage(GetDlgItem(hDlg, IDC_PLAY), WM_SETTEXT, 0, (LPARAM)"Pause");
}
else if (g_CDPlayer.IsPaused()) {
SendMessage(GetDlgItem(hDlg, IDC_PLAY), WM_SETTEXT, 0, (LPARAM)"Resume");
}
else {
SendMessage(GetDlgItem(hDlg, IDC_PLAY), WM_SETTEXT, 0, (LPARAM)"Play");
}
}
SendMessage(label, WM_SETTEXT, 0, (LPARAM)buf);
}
void OnComboBoxChange(HWND hDlg, HWND hcombobox)
{
int ndx = SendMessage(hcombobox, CB_GETCURSEL, 0, 0);
if (ndx != CB_ERR) {
// uninit any device we may already be using
g_CDPlayer.UnInit();
// get drive letter (hidden in combo box item userdata)
char drive = (char)SendMessage(hcombobox, CB_GETITEMDATA, ndx, 0);
// initialize the CD player for this drive
g_CDPlayer.Init(hDlg, drive);
g_cdindrive = g_CDPlayer.IsCDInDrive();
}
RefreshGUI(hDlg);
}
void PutAvailableDevicesInComboBox(HWND hDlg, UINT comboboxid)
{
vector<CCDDeviceInfo> devices = CCDPlayer::GetAvailableDevices();
HWND hcombobox = GetDlgItem(hDlg, comboboxid);
if (!hcombobox) return;
// clear contents of combo box
SendMessage(hcombobox, CB_RESETCONTENT, 0, 0);
// add devices
for (vector<CCDDeviceInfo>::iterator iter = devices.begin(); iter != devices.end(); ++iter) {
stringstream stream;
stream << "<" << iter->m_DriveLetter << ":\\> " << iter->m_Description << " (";
if (iter->m_CDProductCode.size() > 2) {
stream << "UPC: " << iter->m_CDProductCode << ")";
}
else {
stream << "ID: " << iter->m_CDProductCode << ")";
}
string desc = stream.str();
int ndx = SendMessage(hcombobox, CB_ADDSTRING, 0, (LPARAM)desc.c_str());
SendMessage(hcombobox, CB_SETITEMDATA, (WPARAM)ndx, (LPARAM)iter->m_DriveLetter);
}
SendMessage(GetDlgItem(hDlg, comboboxid), CB_SETCURSEL, 0, 0);
OnComboBoxChange(hDlg, (HWND)hcombobox);
}
void OnNextTrack(HWND hDlg)
{
g_CurTrack++;
if (g_CurTrack > g_CDPlayer.GetNumTracks()) g_CurTrack = 1;
if (g_CDPlayer.IsPlaying()) {
g_CDPlayer.Play(g_CurTrack);
}
RefreshGUI(hDlg);
}
void OnPrevTrack(HWND hDlg)
{
g_CurTrack--;
if (g_CurTrack < 1) g_CurTrack = 1;
if (g_CDPlayer.IsPlaying()) {
g_CDPlayer.Play(g_CurTrack);
}
}
void OnPlay(HWND hDlg)
{
if (g_CDPlayer.IsPaused()) {
g_CDPlayer.Resume();
}
else {
if (g_CDPlayer.IsPlaying()) {
g_CDPlayer.Pause();
}
else {
g_CDPlayer.Play(g_CurTrack);
}
}
RefreshGUI(hDlg);
}
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
try {
switch( msg )
{
case WM_DEVICECHANGE:
g_cdindrive = g_CDPlayer.IsCDInDrive();
RefreshGUI(hDlg);
break;
case MM_MCINOTIFY:
// a track has completed... goto the next one
if (wParam == MCI_NOTIFY_SUCCESSFUL) {
OnNextTrack(hDlg);
OnPlay(hDlg);
}
break;
case WM_INITDIALOG:
{
// create display update timer
SetTimer(hDlg, 1, 1000, NULL);
PutAvailableDevicesInComboBox(hDlg, IDC_DEVICES);
g_cdindrive = g_CDPlayer.IsCDInDrive();
RefreshGUI(hDlg);
}
break;
case WM_TIMER:
// update track display
RefreshGUI(hDlg);
break;
case WM_COMMAND:
switch(HIWORD(wParam)) {
case CBN_SELCHANGE:
{
// user has selected a new drive in the combo box
OnComboBoxChange(hDlg, (HWND)lParam);
}
break;
case BN_CLICKED:
{
// a button was clicked... which one?
switch(LOWORD(wParam)) {
case IDC_EJECT:
g_CDPlayer.OpenCDDoor();
RefreshGUI(hDlg);
break;
case IDC_NEXTTRACK:
OnNextTrack(hDlg);
break;
case IDC_PREVIOUSTRACK:
OnPrevTrack(hDlg);
break;
case IDC_PLAY:
OnPlay(hDlg);
break;
case IDC_STOP:
g_CDPlayer.Stop();
RefreshGUI(hDlg);
break;
} // switch LOWORD(wParam)
}
break;
} // notification code
break;
case WM_CLOSE:
ShowWindow(hDlg, SW_HIDE);
g_CDPlayer.UnInit();
KillTimer(hDlg, 1);
PostQuitMessage(0);
break;
case WM_DESTROY:
break;
default:
return FALSE; // Didn't handle message
}
}
catch(char *err) {
MessageBox(hDlg, err, "Ch9p1_CDPlayer MCI Error Message", MB_OK);
return(FALSE);
}
return TRUE; // Handled message
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hDlg = NULL;
MSG msg;
try {
// Display the main dialog box.
hDlg = CreateDialog( hInstance, MAKEINTRESOURCE(IDD_CDPLAYER),
NULL, MainDlgProc );
ShowWindow(hDlg, SW_SHOW);
while(1)
{
// Windows messages are available
if(GetMessage(&msg, NULL, 0, 0))
{
if(!IsDialogMessage(hDlg, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else {
DestroyWindow(hDlg);
return(msg.wParam);
}
}
}
catch(...) {
// TODO
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -