TimeElapse.cpp
资源名称:连连看源程序.rar [点击查看]
上传用户:z3308516
上传日期:2013-01-23
资源大小:703k
文件大小:5k
源码类别:
2D图形编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "Resource.h"
- #include "timeelapse.h"
- #define ShowMessage(X) MessageBox(NULL,X,"发生错误",MB_OK|MB_TOPMOST|MB_ICONWARNING);
- PostQuitMessage(0);
- CTimeElapse::CTimeElapse(){
- bPause=FALSE;
- bBackMidi=FALSE;
- hBitmap=NULL;
- }
- void CTimeElapse::CTimeElapseInit(HWND hParent,int totalSecond,int udelay,BOOL bnet)
- {
- RECT rc;
- bNet=bnet; //是否是网络部分对打
- iReorder=3;
- iExplore=3;
- delay=udelay;
- this->hParent =hParent;
- this->totalSecond =totalSecond;
- GetClientRect(hParent,&rc);
- rc.top =50;
- rc.bottom =80;
- rc.left =(rc.right -rc.left)/2-300;
- rc.right =rc.left+600;
- memcpy((void*)&rcFill,(const void*)&rc,sizeof(RECT));
- if(!hBitmap)
- hBitmap=LoadBitmap((HINSTANCE)GetModuleHandle(NULL),(LPCTSTR)IDB_TIMEBAR);
- // hbr=CreateSolidBrush(RGB(240,240,128));
- }
- CTimeElapse::~CTimeElapse(void)
- {
- if(hBitmap)
- DeleteObject(hBitmap);
- }
- void CTimeElapse::SetTotalTime(int totalSecond){
- this->totalSecond =totalSecond;
- }
- void CTimeElapse::ShowElapseTime(){
- HDC hDC;
- RECT rcDraw;
- char szTimeElapse[10];
- HFONT hfont,oldfont;
- hDC=GetDC(hParent);
- hfont=CreateFont(45,0,0,0,0,0,0,0,0,0,0,0,0,FONTNAME);
- oldfont=(HFONT)SelectObject(hDC,hfont);
- memcpy((void*)&rcDraw,(const void*)&rcFill,sizeof(RECT));
- rcDraw.top =10;
- rcDraw.bottom= 50;
- rcDraw.left=0;
- rcDraw.right =800;
- SetTextColor(hDC,RGB(255,0,0));
- wsprintf((LPTSTR)szTimeElapse," %d ",totalSecond);
- DrawText(hDC,(LPCTSTR)szTimeElapse,strlen(szTimeElapse),&rcDraw,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
- SelectObject(hDC,oldfont);
- DeleteObject(hfont);
- ReleaseDC(hParent,hDC);
- }
- void CTimeElapse::FillRegion(BOOL bRef){
- HDC hDC;
- HPEN hPen,oldPen;
- RECT rcRealFill;
- HDC tempDC;
- hDC=GetDC(hParent);
- tempDC=CreateCompatibleDC(hDC);
- SelectObject(tempDC,hBitmap);
- memcpy((void*)&rcRealFill,(const void*)&rcFill,sizeof(RECT));
- rcRealFill.right =numPices*totalSecond+rcRealFill.left;
- if(!bRef){
- BitBlt(hDC,rcRealFill.left,rcRealFill.top,rcRealFill.right-rcRealFill.left,rcRealFill.bottom,tempDC,0,0,SRCCOPY);
- }
- DeleteDC(tempDC);
- hPen=CreatePen(PS_SOLID,1,RGB(255,255,255));
- oldPen=(HPEN)SelectObject(hDC,hPen);
- MoveToEx(hDC,rcRealFill.right+1,rcRealFill.top,NULL);
- LineTo(hDC,rcRealFill.right +1,rcRealFill.top+30);
- SelectObject(hDC,oldPen);
- DeleteObject(hPen);
- ReleaseDC(hParent,hDC);
- }
- void CTimeElapse::Begin(){
- HANDLE hThread;
- DWORD did;
- threadExit=FALSE;
- numPices=(rcFill.right -rcFill.left)/totalSecond;
- hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadElapseTime,(LPVOID)this,0,&did);
- if(bBackMidi)
- PlayMidiSound();
- if(hThread){
- CloseHandle(hThread);
- }else{
- ShowMessage("线程创建失败");
- }
- }
- void CTimeElapse::Stop(){
- threadExit=TRUE;
- if(bBackMidi){
- mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
- mciSendCommand (OpenParms.wDeviceID, MCI_CLOSE, NULL, NULL);
- }
- }
- void CTimeElapse::CloseMidiSound(){
- mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
- mciSendCommand (OpenParms.wDeviceID, MCI_CLOSE, NULL, NULL);
- bBackMidi=FALSE;
- }
- int CTimeElapse::delay =0;
- LRESULT CALLBACK CTimeElapse::ThreadElapseTime(LPARAM lParam){
- CTimeElapse *pElapse;
- pElapse=(CTimeElapse*)lParam;
- while(!threadExit && pElapse->totalSecond !=0){
- if(!bPause){
- pElapse->ShowElapseTime();
- pElapse->FillRegion(TRUE);
- pElapse->totalSecond --;
- }
- Sleep(delay);
- }
- if(pElapse->totalSecond==0)
- SendMessage(pElapse->hParent,OUTOF_TIME,0,0);
- return 0;
- }
- void CTimeElapse::InvalidIt(HWND hParent){
- InvalidateRect(hParent,&rcFill,TRUE);
- }
- void CTimeElapse::Pause(){
- bPause=!bPause;
- if(bPause && bBackMidi){
- StopMidiSound();
- }else if(bBackMidi)
- ContinuePlay();
- }
- void CTimeElapse::PlayMidiSound(){
- MCIERROR mer;
- bBackMidi=TRUE;
- OpenParms.lpstrDeviceType =(LPCSTR) MCI_DEVTYPE_SEQUENCER;//MIDI类型
- OpenParms.lpstrElementName = (LPCSTR) "back";
- OpenParms.wDeviceID = 0;
- OpenParms.dwCallback=0;
- mer=mciSendCommand (NULL, MCI_OPEN,MCI_WAIT | MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT,(DWORD)(LPVOID) &OpenParms);
- if(!mer){
- PlayParms.dwFrom = 0;
- mciSendCommand (OpenParms.wDeviceID,MCI_PLAY,
- MCI_FROM, (DWORD)(LPVOID)
- &PlayParms);
- }
- }
- void CTimeElapse::StopMidiSound(){
- mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
- }
- void CTimeElapse::QueryStatus(){
- MCI_STATUS_PARMS StatusParms;
- if(bBackMidi){
- if(!threadExit && !bPause){
- memset((void*)&StatusParms,0,sizeof(StatusParms));
- StatusParms.dwItem = MCI_STATUS_MODE;
- mciSendCommand (OpenParms.wDeviceID, MCI_STATUS,MCI_WAIT | MCI_STATUS_ITEM,(DWORD)(LPVOID) &StatusParms);
- if(MCI_MODE_STOP==StatusParms.dwReturn){
- mciSendCommand (OpenParms.wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&PlayParms);
- mciSendCommand (OpenParms.wDeviceID, MCI_CLOSE, NULL, NULL);
- PlayMidiSound();
- }
- }
- }
- }
- void CTimeElapse::ContinuePlay(){
- mciSendCommand(OpenParms.wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID)&PlayParms);
- }
- //MCI_PLAY_PARMS CTimeElapse::PlayParms={0};
- //MCI_OPEN_PARMS CTimeElapse::OpenParms={0};
- BOOL CTimeElapse::threadExit=FALSE;
- BOOL CTimeElapse::bPause =FALSE;