DSDemoDlg.cpp
上传用户:sdpcwz
上传日期:2009-12-14
资源大小:1237k
文件大小:6k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // DSDemoDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DSDemo.h"
  5. #include "DSDemoDlg.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. // CDSDemoDlg dialog
  50. CDSDemoDlg::CDSDemoDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CDSDemoDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CDSDemoDlg)
  54. //}}AFX_DATA_INIT
  55. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  56. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  57. }
  58. void CDSDemoDlg::DoDataExchange(CDataExchange* pDX)
  59. {
  60. CDialog::DoDataExchange(pDX);
  61. //{{AFX_DATA_MAP(CDSDemoDlg)
  62. DDX_Control(pDX, ID_EFFECTVOLUME, m_SliderEffectVolume);
  63. DDX_Control(pDX, ID_BKVOLUME, m_SliderBKVolume);
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CDSDemoDlg, CDialog)
  67. //{{AFX_MSG_MAP(CDSDemoDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_NOTIFY(NM_RELEASEDCAPTURE, ID_BKVOLUME, OnReleasedcaptureBkvolume)
  72. ON_NOTIFY(NM_RELEASEDCAPTURE, ID_EFFECTVOLUME, OnReleasedcaptureEffectvolume)
  73. ON_BN_CLICKED(IDC_EFFECT, OnEffect)
  74. ON_BN_CLICKED(IDC_PLAYBKMUSIC, OnPlaybkmusic)
  75. ON_WM_CLOSE()
  76. //}}AFX_MSG_MAP
  77. END_MESSAGE_MAP()
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CDSDemoDlg message handlers
  80. BOOL CDSDemoDlg::OnInitDialog()
  81. {
  82. CDialog::OnInitDialog();
  83. // Add "About..." menu item to system menu.
  84. // IDM_ABOUTBOX must be in the system command range.
  85. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  86. ASSERT(IDM_ABOUTBOX < 0xF000);
  87. CMenu* pSysMenu = GetSystemMenu(FALSE);
  88. if (pSysMenu != NULL)
  89. {
  90. CString strAboutMenu;
  91. strAboutMenu.LoadString(IDS_ABOUTBOX);
  92. if (!strAboutMenu.IsEmpty())
  93. {
  94. pSysMenu->AppendMenu(MF_SEPARATOR);
  95. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  96. }
  97. }
  98. // Set the icon for this dialog.  The framework does this automatically
  99. //  when the application's main window is not a dialog
  100. SetIcon(m_hIcon, TRUE); // Set big icon
  101. SetIcon(m_hIcon, FALSE); // Set small icon
  102. // TODO: Add extra initialization here
  103. //设置音量范围
  104. m_SliderBKVolume.SetRange(0,100);
  105. m_SliderEffectVolume.SetRange(0,100);
  106. //初始时的音量均设成最大值
  107. m_SliderBKVolume.SetPos(100);
  108. m_SliderEffectVolume.SetPos(100);
  109. //选中播放背景音乐复选框
  110. ((CButton*)GetDlgItem(IDC_PLAYBKMUSIC))->SetCheck(1);  
  111. //初始化DirectSound对象
  112. if(!InitializeDirectSound(*AfxGetMainWnd()))
  113. {
  114. AfxMessageBox("初始化DirectSound失败!",MB_OK);
  115. CDialog::OnOK();
  116. return TRUE  ;
  117. }
  118. //创建声音缓冲区
  119. m_lpBkGrdDSBuffer=new CDSBuffer("bk.wav",TRUE,m_lpDS);
  120.     m_lpYinXiaoDSBuffer=new CDSBuffer("effect.wav",FALSE,m_lpDS);
  121. //播放背景音乐
  122. m_lpBkGrdDSBuffer->PlaySound();
  123. return TRUE;  // return TRUE  unless you set the focus to a control
  124. }
  125. void CDSDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
  126. {
  127. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  128. {
  129. CAboutDlg dlgAbout;
  130. dlgAbout.DoModal();
  131. }
  132. else
  133. {
  134. CDialog::OnSysCommand(nID, lParam);
  135. }
  136. }
  137. // If you add a minimize button to your dialog, you will need the code below
  138. //  to draw the icon.  For MFC applications using the document/view model,
  139. //  this is automatically done for you by the framework.
  140. void CDSDemoDlg::OnPaint() 
  141. {
  142. if (IsIconic())
  143. {
  144. CPaintDC dc(this); // device context for painting
  145. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  146. // Center icon in client rectangle
  147. int cxIcon = GetSystemMetrics(SM_CXICON);
  148. int cyIcon = GetSystemMetrics(SM_CYICON);
  149. CRect rect;
  150. GetClientRect(&rect);
  151. int x = (rect.Width() - cxIcon + 1) / 2;
  152. int y = (rect.Height() - cyIcon + 1) / 2;
  153. // Draw the icon
  154. dc.DrawIcon(x, y, m_hIcon);
  155. }
  156. else
  157. {
  158. CDialog::OnPaint();
  159. }
  160. }
  161. // The system calls this to obtain the cursor to display while the user drags
  162. //  the minimized window.
  163. HCURSOR CDSDemoDlg::OnQueryDragIcon()
  164. {
  165. return (HCURSOR) m_hIcon;
  166. }
  167. BOOL CDSDemoDlg::InitializeDirectSound(HWND hWnd)
  168. {
  169. // 创建背景音乐和音效DirectSound对象
  170. if (DS_OK != DirectSoundCreate(NULL, &m_lpDS, NULL))
  171. {
  172. ::MessageBox(NULL,"创建DirectSound对象失败n找不到可利用的声音设备",NULL,MB_OK);
  173. return FALSE;
  174. }
  175. // 设置协作级别
  176. if (DS_OK != m_lpDS->SetCooperativeLevel(hWnd, DSSCL_NORMAL))
  177. {
  178. ::MessageBox(NULL,"设置协作级别失败",NULL,MB_OK);
  179. return FALSE;
  180. }
  181. return TRUE;
  182. }
  183. void CDSDemoDlg::ClearDSBuffer()
  184. {
  185.     m_lpBkGrdDSBuffer->StopSound();
  186. m_lpYinXiaoDSBuffer->StopSound();
  187. if (m_lpBkGrdDSBuffer)
  188. delete m_lpBkGrdDSBuffer;
  189. if (m_lpYinXiaoDSBuffer)
  190. delete m_lpYinXiaoDSBuffer;
  191. }
  192. void CDSDemoDlg::OnReleasedcaptureBkvolume(NMHDR* pNMHDR, LRESULT* pResult) 
  193. {
  194. int nPos = m_SliderBKVolume.GetPos();
  195. m_lpBkGrdDSBuffer->SetVolume(nPos);
  196. *pResult = 0;
  197. }
  198. void CDSDemoDlg::OnReleasedcaptureEffectvolume(NMHDR* pNMHDR, LRESULT* pResult) 
  199. {
  200. int nPos = m_SliderEffectVolume.GetPos();
  201. m_lpYinXiaoDSBuffer->SetVolume(nPos);
  202. *pResult = 0;
  203. }
  204. void CDSDemoDlg::OnEffect() 
  205. {
  206. m_lpYinXiaoDSBuffer->PlaySound();
  207. }
  208. void CDSDemoDlg::OnPlaybkmusic() 
  209. {
  210. CButton* pButton = (CButton*)GetDlgItem(IDC_PLAYBKMUSIC);
  211. BOOL bFlag = (pButton->GetCheck() == 1);
  212. if (bFlag) m_lpBkGrdDSBuffer->PlaySound();
  213. else m_lpBkGrdDSBuffer->StopSound();
  214. }
  215. void CDSDemoDlg::OnClose() 
  216. {
  217. // 删除声音缓冲区对象
  218. ClearDSBuffer();
  219. CDialog::OnClose();
  220. }