ex123Dlg.cpp
上传用户:qdhmjx
上传日期:2022-07-11
资源大小:2226k
文件大小:7k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ex123Dlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ex123.h"
  5. #include "ex123Dlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CEx123Dlg dialog
  50. CEx123Dlg::CEx123Dlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CEx123Dlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CEx123Dlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. m_bIsPause=FALSE;
  59. m_bIsPlaying=FALSE;
  60. m_MCIDeviceID=0;
  61. }
  62. void CEx123Dlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CEx123Dlg)
  66. // NOTE: the ClassWizard will add DDX and DDV calls here
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CEx123Dlg, CDialog)
  70. //{{AFX_MSG_MAP(CEx123Dlg)
  71. ON_WM_SYSCOMMAND()
  72. ON_WM_PAINT()
  73. ON_WM_QUERYDRAGICON()
  74. ON_WM_DESTROY()
  75. ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
  76. ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
  77. ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
  78. //}}AFX_MSG_MAP
  79. ON_MESSAGE(MM_MCINOTIFY,OnMciNotify)
  80. END_MESSAGE_MAP()
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CEx123Dlg message handlers
  83. BOOL CEx123Dlg::OnInitDialog()
  84. {
  85. CDialog::OnInitDialog();
  86. // Add "About..." menu item to system menu.
  87. // IDM_ABOUTBOX must be in the system command range.
  88. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  89. ASSERT(IDM_ABOUTBOX < 0xF000);
  90. CMenu* pSysMenu = GetSystemMenu(FALSE);
  91. if (pSysMenu != NULL)
  92. {
  93. CString strAboutMenu;
  94. strAboutMenu.LoadString(IDS_ABOUTBOX);
  95. if (!strAboutMenu.IsEmpty())
  96. {
  97. pSysMenu->AppendMenu(MF_SEPARATOR);
  98. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  99. }
  100. }
  101. // Set the icon for this dialog.  The framework does this automatically
  102. //  when the application's main window is not a dialog
  103. SetIcon(m_hIcon, TRUE); // Set big icon
  104. SetIcon(m_hIcon, FALSE); // Set small icon
  105. // TODO: Add extra initialization here
  106. return TRUE;  // return TRUE  unless you set the focus to a control
  107. }
  108. void CEx123Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  109. {
  110. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  111. {
  112. CAboutDlg dlgAbout;
  113. dlgAbout.DoModal();
  114. }
  115. else
  116. {
  117. CDialog::OnSysCommand(nID, lParam);
  118. }
  119. }
  120. // If you add a minimize button to your dialog, you will need the code below
  121. //  to draw the icon.  For MFC applications using the document/view model,
  122. //  this is automatically done for you by the framework.
  123. void CEx123Dlg::OnPaint() 
  124. {
  125. if (IsIconic())
  126. {
  127. CPaintDC dc(this); // device context for painting
  128. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  129. // Center icon in client rectangle
  130. int cxIcon = GetSystemMetrics(SM_CXICON);
  131. int cyIcon = GetSystemMetrics(SM_CYICON);
  132. CRect rect;
  133. GetClientRect(&rect);
  134. int x = (rect.Width() - cxIcon + 1) / 2;
  135. int y = (rect.Height() - cyIcon + 1) / 2;
  136. // Draw the icon
  137. dc.DrawIcon(x, y, m_hIcon);
  138. }
  139. else
  140. {
  141. CDialog::OnPaint();
  142. }
  143. }
  144. // The system calls this to obtain the cursor to display while the user drags
  145. //  the minimized window.
  146. HCURSOR CEx123Dlg::OnQueryDragIcon()
  147. {
  148. return (HCURSOR) m_hIcon;
  149. }
  150. void CEx123Dlg::OnDestroy() 
  151. {
  152. CDialog::OnDestroy();
  153. //退出程序前先停止声音播放,再关闭设备
  154. //这里忽略了错误处理
  155. mciSendCommand(m_MCIDeviceID,MCI_STOP,MCI_WAIT,NULL);
  156. mciSendCommand(m_MCIDeviceID,MCI_CLOSE,MCI_WAIT,NULL);
  157. }
  158. void CEx123Dlg::ShowMCIError(DWORD dwError)
  159. {
  160. char szErrorBuf[MAXERRORLENGTH];
  161. MessageBeep(MB_ICONEXCLAMATION);
  162. if(mciGetErrorString(dwError,(LPSTR)szErrorBuf,MAXERRORLENGTH))
  163. MessageBox(szErrorBuf,"MCI错误",MB_ICONEXCLAMATION);
  164. else
  165. MessageBox("未知错误!","MCI错误",MB_ICONEXCLAMATION);
  166. }
  167. LRESULT CEx123Dlg::OnMciNotify(WPARAM wParam,LPARAM lParam)
  168. {
  169. //如果一个文件播放完毕,则重置两个状态为FALSE
  170. //使得用户可以再次播放该文件
  171. if(wParam==MCI_NOTIFY_SUCCESSFUL)
  172. {
  173. m_bIsPause=FALSE;
  174. m_bIsPlaying=FALSE;
  175. return 0;
  176. }
  177. return -1;
  178. }
  179. void CEx123Dlg::OnButtonOpen() 
  180. {
  181. CString filename;
  182. MCI_OPEN_PARMS mciOpenParms;
  183. DWORD dwError;
  184. //利用通用对话框打开一个WAV波形文件
  185. static char szFilter[]="波形音频文件(*.wav)|*.wav||";
  186. CFileDialog dlg(TRUE,"wav",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);
  187. if(dlg.DoModal()==IDOK)
  188. {
  189. filename=dlg.GetPathName();//得到用户选择的文件名
  190. if(m_bIsPlaying) //如果正在播放,则先关掉
  191. {
  192. dwError=mciSendCommand(m_MCIDeviceID,MCI_CLOSE,0,NULL);
  193. if(dwError)
  194. {
  195. ShowMCIError(dwError);
  196. return;
  197. }
  198. }
  199. mciOpenParms.lpstrDeviceType="waveaudio";
  200. mciOpenParms.lpstrElementName=filename;
  201. dwError=mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);
  202. //如果打不开音频设备则报告错误并返回
  203. if(dwError)
  204. {
  205. ShowMCIError(dwError);
  206. return;
  207. }
  208. //如果打开成功,则取得设备ID值并设置两个状态指示
  209. m_MCIDeviceID=mciOpenParms.wDeviceID;
  210. m_bIsPause=FALSE;
  211. m_bIsPlaying=FALSE;
  212. }
  213. }
  214. void CEx123Dlg::OnButtonPlay() 
  215. {
  216. MCI_PLAY_PARMS mciPlayParms;
  217. DWORD dwError;
  218. if(!m_bIsPlaying)
  219. {
  220. //处理MM_MCINOTIFY消息的窗口句柄
  221. mciPlayParms.dwCallback=(long)GetSafeHwnd();
  222. //从头开始播放文件
  223. mciPlayParms.dwFrom=0;
  224. dwError=mciSendCommand(m_MCIDeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);
  225. //如果错误,则显示错误并返回
  226. if(dwError)
  227. {
  228. ShowMCIError(dwError);
  229. return;
  230. }
  231. }
  232. if(m_bIsPlaying)
  233. {
  234. if(!m_bIsPause) //若正在播放,先暂停
  235. dwError=mciSendCommand(m_MCIDeviceID,MCI_PAUSE,0,NULL);
  236. else //若现在处于暂停,则恢复放音
  237. dwError=mciSendCommand(m_MCIDeviceID,MCI_RESUME,0,NULL);
  238. if(dwError) //遇到错误返回
  239. {
  240. ShowMCIError(dwError);
  241. return;
  242. }
  243. m_bIsPause=!m_bIsPause;//切换放音/暂停状态
  244. }
  245. m_bIsPlaying=TRUE;
  246. }
  247. void CEx123Dlg::OnButtonStop() 
  248. {
  249. DWORD dwError;
  250. dwError=mciSendCommand(m_MCIDeviceID,MCI_STOP,MCI_WAIT,NULL);
  251. if(dwError)
  252. {
  253. ShowMCIError(dwError);
  254. return;
  255. }
  256. m_bIsPause=FALSE;
  257. m_bIsPlaying=FALSE;
  258. }