TimeElapse.cpp
上传用户:z3308516
上传日期:2013-01-23
资源大小:703k
文件大小:5k
源码类别:

2D图形编程

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "Resource.h"
  3. #include "timeelapse.h"
  4. #define ShowMessage(X) MessageBox(NULL,X,"发生错误",MB_OK|MB_TOPMOST|MB_ICONWARNING); 
  5. PostQuitMessage(0);
  6. CTimeElapse::CTimeElapse(){
  7. bPause=FALSE;
  8. bBackMidi=FALSE;
  9. hBitmap=NULL;
  10. }
  11. void CTimeElapse::CTimeElapseInit(HWND hParent,int totalSecond,int udelay,BOOL bnet)
  12. {
  13. RECT rc;
  14. bNet=bnet; //是否是网络部分对打
  15. iReorder=3;
  16. iExplore=3;
  17. delay=udelay;
  18. this->hParent =hParent;
  19. this->totalSecond =totalSecond;
  20. GetClientRect(hParent,&rc);
  21. rc.top =50;
  22. rc.bottom =80;
  23. rc.left =(rc.right -rc.left)/2-300;
  24. rc.right =rc.left+600;
  25. memcpy((void*)&rcFill,(const void*)&rc,sizeof(RECT));
  26. if(!hBitmap)
  27. hBitmap=LoadBitmap((HINSTANCE)GetModuleHandle(NULL),(LPCTSTR)IDB_TIMEBAR);
  28. // hbr=CreateSolidBrush(RGB(240,240,128));
  29. }
  30. CTimeElapse::~CTimeElapse(void)
  31. {
  32. if(hBitmap)
  33. DeleteObject(hBitmap);
  34. }
  35. void CTimeElapse::SetTotalTime(int totalSecond){
  36. this->totalSecond =totalSecond;
  37. }
  38. void CTimeElapse::ShowElapseTime(){
  39. HDC hDC;
  40. RECT rcDraw;
  41. char szTimeElapse[10];
  42. HFONT hfont,oldfont;
  43. hDC=GetDC(hParent);
  44. hfont=CreateFont(45,0,0,0,0,0,0,0,0,0,0,0,0,FONTNAME);
  45. oldfont=(HFONT)SelectObject(hDC,hfont);
  46. memcpy((void*)&rcDraw,(const void*)&rcFill,sizeof(RECT));
  47. rcDraw.top =10;
  48. rcDraw.bottom= 50;
  49. rcDraw.left=0;
  50. rcDraw.right =800;
  51. SetTextColor(hDC,RGB(255,0,0));
  52. wsprintf((LPTSTR)szTimeElapse,"  %d  ",totalSecond);
  53. DrawText(hDC,(LPCTSTR)szTimeElapse,strlen(szTimeElapse),&rcDraw,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  54. SelectObject(hDC,oldfont);
  55. DeleteObject(hfont);
  56. ReleaseDC(hParent,hDC);
  57. }
  58. void CTimeElapse::FillRegion(BOOL bRef){
  59. HDC hDC;
  60. HPEN hPen,oldPen;
  61. RECT rcRealFill;
  62. HDC tempDC;
  63. hDC=GetDC(hParent);
  64. tempDC=CreateCompatibleDC(hDC);
  65. SelectObject(tempDC,hBitmap);
  66. memcpy((void*)&rcRealFill,(const void*)&rcFill,sizeof(RECT));
  67. rcRealFill.right =numPices*totalSecond+rcRealFill.left;
  68. if(!bRef){
  69. BitBlt(hDC,rcRealFill.left,rcRealFill.top,rcRealFill.right-rcRealFill.left,rcRealFill.bottom,tempDC,0,0,SRCCOPY);
  70. }
  71. DeleteDC(tempDC);
  72. hPen=CreatePen(PS_SOLID,1,RGB(255,255,255));
  73. oldPen=(HPEN)SelectObject(hDC,hPen);
  74. MoveToEx(hDC,rcRealFill.right+1,rcRealFill.top,NULL);
  75. LineTo(hDC,rcRealFill.right +1,rcRealFill.top+30);
  76. SelectObject(hDC,oldPen);
  77. DeleteObject(hPen);
  78. ReleaseDC(hParent,hDC);
  79. }
  80. void CTimeElapse::Begin(){
  81. HANDLE hThread;
  82. DWORD did;
  83. threadExit=FALSE;
  84. numPices=(rcFill.right -rcFill.left)/totalSecond;
  85. hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadElapseTime,(LPVOID)this,0,&did);
  86. if(bBackMidi)
  87. PlayMidiSound();
  88. if(hThread){
  89. CloseHandle(hThread);
  90. }else{
  91. ShowMessage("线程创建失败");
  92. }
  93. }
  94. void CTimeElapse::Stop(){
  95. threadExit=TRUE;
  96. if(bBackMidi){
  97. mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
  98. mciSendCommand (OpenParms.wDeviceID, MCI_CLOSE, NULL, NULL);
  99. }
  100. }
  101. void CTimeElapse::CloseMidiSound(){
  102. mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
  103. mciSendCommand (OpenParms.wDeviceID, MCI_CLOSE, NULL, NULL);
  104. bBackMidi=FALSE;
  105. }
  106. int CTimeElapse::delay =0;
  107. LRESULT CALLBACK CTimeElapse::ThreadElapseTime(LPARAM lParam){
  108. CTimeElapse *pElapse;
  109. pElapse=(CTimeElapse*)lParam;
  110. while(!threadExit && pElapse->totalSecond !=0){
  111. if(!bPause){
  112. pElapse->ShowElapseTime();
  113. pElapse->FillRegion(TRUE);
  114. pElapse->totalSecond --;
  115. }
  116. Sleep(delay);
  117. }
  118. if(pElapse->totalSecond==0)
  119. SendMessage(pElapse->hParent,OUTOF_TIME,0,0);
  120. return 0;
  121. }
  122. void CTimeElapse::InvalidIt(HWND hParent){
  123. InvalidateRect(hParent,&rcFill,TRUE);
  124. }
  125. void CTimeElapse::Pause(){
  126. bPause=!bPause;
  127. if(bPause  && bBackMidi){
  128. StopMidiSound();
  129. }else if(bBackMidi)
  130. ContinuePlay();
  131. }
  132. void CTimeElapse::PlayMidiSound(){
  133. MCIERROR mer;
  134. bBackMidi=TRUE;
  135. OpenParms.lpstrDeviceType =(LPCSTR) MCI_DEVTYPE_SEQUENCER;//MIDI类型 
  136. OpenParms.lpstrElementName = (LPCSTR) "back"; 
  137. OpenParms.wDeviceID = 0; 
  138. OpenParms.dwCallback=0;
  139. mer=mciSendCommand (NULL, MCI_OPEN,MCI_WAIT | MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT,(DWORD)(LPVOID) &OpenParms);
  140. if(!mer){
  141. PlayParms.dwFrom = 0;
  142. mciSendCommand (OpenParms.wDeviceID,MCI_PLAY, 
  143. MCI_FROM, (DWORD)(LPVOID) 
  144. &PlayParms);
  145. }
  146. }
  147. void CTimeElapse::StopMidiSound(){
  148. mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
  149. }
  150. void CTimeElapse::QueryStatus(){
  151. MCI_STATUS_PARMS StatusParms; 
  152. if(bBackMidi){
  153. if(!threadExit && !bPause){
  154. memset((void*)&StatusParms,0,sizeof(StatusParms));
  155. StatusParms.dwItem = MCI_STATUS_MODE;
  156. mciSendCommand (OpenParms.wDeviceID, MCI_STATUS,MCI_WAIT | MCI_STATUS_ITEM,(DWORD)(LPVOID) &StatusParms);
  157. if(MCI_MODE_STOP==StatusParms.dwReturn){
  158. mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
  159. mciSendCommand (OpenParms.wDeviceID, MCI_CLOSE, NULL, NULL);
  160. PlayMidiSound();
  161. }
  162. }
  163. }
  164. }
  165. void CTimeElapse::ContinuePlay(){
  166. mciSendCommand(OpenParms.wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID)&PlayParms);
  167. }
  168. //MCI_PLAY_PARMS CTimeElapse::PlayParms={0};
  169. //MCI_OPEN_PARMS CTimeElapse::OpenParms={0};
  170. BOOL CTimeElapse::threadExit=FALSE;
  171. BOOL CTimeElapse::bPause =FALSE;