XTPSoundManager.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:3k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPSoundManager.cpp : implementation of the CXTPSoundManager class.
  2. //
  3. // This file is a part of the XTREME COMMANDBARS MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "XTPSoundManager.h"
  22. #pragma warning (disable : 4201)
  23. #include <mmsystem.h>
  24. #pragma warning (default : 4201)
  25. #pragma message("   Automatically linking with WinMM library")
  26. #pragma message("      (Windows Multimedia System)")
  27. #pragma comment(lib, "winmm.lib")
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33. CXTPSoundManager::CXTPSoundManager()
  34. {
  35. m_hThread = 0;
  36. m_bSystemSounds = TRUE;
  37. m_bTerminate = FALSE;
  38. m_soundState = xtpSoundNone;
  39. }
  40. CXTPSoundManager* AFX_CDECL XTPSoundManager()
  41. {
  42. static CXTPSoundManager s_managerInstance;
  43. return &s_managerInstance;
  44. }
  45. CXTPSoundManager::~CXTPSoundManager()
  46. {
  47. StopThread();
  48. }
  49. DWORD WINAPI CXTPSoundManager::SoundThreadProc(LPVOID)
  50. {
  51. XTPSoundManagerState& soundState = XTPSoundManager()->m_soundState;
  52. while (!XTPSoundManager()->m_bTerminate)
  53. {
  54. switch (soundState)
  55. {
  56. case xtpSoundMenuCommand:
  57. ::PlaySound (_T("MenuCommand"), NULL, (SND_SYNC | SND_NODEFAULT | SND_ALIAS | SND_NOWAIT));
  58. soundState = xtpSoundNone;
  59. break;
  60. case xtpSoundMenuPopup:
  61. ::PlaySound (_T("MenuPopup"), NULL, (SND_SYNC | SND_NODEFAULT | SND_ALIAS | SND_NOWAIT));
  62. soundState = xtpSoundNone;
  63. break;
  64. }
  65. ::Sleep (5);
  66. }
  67. ::PlaySound(NULL, NULL, SND_PURGE);
  68. return 0;
  69. }
  70. void CXTPSoundManager::StartThread()
  71. {
  72. ASSERT (m_hThread == NULL);
  73. m_soundState = xtpSoundNone;
  74. m_bTerminate = FALSE;
  75. DWORD dwThreadID;
  76. m_hThread = ::CreateThread(NULL, 0, SoundThreadProc, NULL, CREATE_SUSPENDED, &dwThreadID);
  77. if (m_hThread != NULL)
  78. {
  79. ::SetThreadPriority(m_hThread, THREAD_PRIORITY_BELOW_NORMAL);
  80. ::ResumeThread(m_hThread);
  81. }
  82. }
  83. void CXTPSoundManager::StopThread()
  84. {
  85. if (m_hThread)
  86. {
  87. m_bTerminate = TRUE;
  88. DWORD dwCount = 0, dwExitCode = 0;
  89. while (GetExitCodeThread(m_hThread, &dwExitCode) && dwExitCode == STILL_ACTIVE)
  90. {
  91. dwCount++;
  92. if (dwCount > 10)
  93. {
  94. TerminateThread(m_hThread, 0);
  95. break;
  96. }
  97. ::WaitForSingleObject(m_hThread, 100);
  98. }
  99. ::CloseHandle(m_hThread);
  100. m_hThread = 0;
  101. }
  102. }
  103. void CXTPSoundManager::PlaySystemSound(XTPSoundManagerState state)
  104. {
  105. if (m_bSystemSounds)
  106. {
  107. if (m_hThread == 0)
  108. StartThread();
  109. m_soundState = state;
  110. }
  111. }
  112. void CXTPSoundManager::EnableSystemSounds(BOOL bEnable)
  113. {
  114. m_bSystemSounds = bEnable;
  115. }