计时器:由于需要暂停/恢复计时、绘图,所以专门作了一个类。 其中用了三个状态#define T_CLOSE 0
#define T_START 1
#define T_PAUSE 2
应该可以优化。
附 mytimer.h mytimer.cpp
//定时器
#ifndef __MYTIMER_H
#define __MYTIMER_H

#define T_CLOSE 0
#define T_START 1
#define T_PAUSE 2

class MYTIMER

...{
public:
MYTIMER();
~MYTIMER();

void StartTimer();
void PauseTimer();
void StopTimer();
void GoOnTimer();
int GetTimeLong();
void TimerInc();
void Clear();
void Draw();
void LoadPic();
void SetHINSTANCE(HINSTANCE h);
void SetHWND(HWND h);
void SetHDC(HDC h);

void SetPos(int x,int y);

private:
int time_long;
int iState;
POINT pos;

HDC hdcmem;
HDC hdc;
HWND hwnd;
HINSTANCE hinstance;

HBITMAP hbm_number;
};

#endif


mytimer.cpp

#include "stdafx.h"
#include "resource.h"

#include "mytimer.h"

void MYTIMER::StartTimer()

...{
if(T_START==iState)

...{
return;
}

time_long=0;
iState=T_START;

SetTimer(hwnd,1,1000,NULL);

// MessageBox(hwnd,"start timer","timer",MB_OK);
}

void MYTIMER::PauseTimer()

...{
if(T_START == iState)

...{
KillTimer(hwnd,1);
iState=T_PAUSE;
}
}
void MYTIMER::StopTimer()

...{
KillTimer(hwnd,1);

iState=T_CLOSE;
}

void MYTIMER::Clear()

...{
time_long=0;
}

int MYTIMER::GetTimeLong()

...{
return time_long;
}

void MYTIMER::GoOnTimer()

...{
if(T_PAUSE == iState)

...{
iState=T_START;
SetTimer(hwnd,1,1000,NULL);
}
// MessageBox(hwnd,"Timer GO ON","timer",MB_OK);
}

void MYTIMER::TimerInc()

...{
if(time_long<=999)
time_long++;
}

void MYTIMER::SetHWND(HWND h)

...{
hwnd=h;
}

void MYTIMER::SetHINSTANCE(HINSTANCE h)

...{
hinstance=h;
}

void MYTIMER::LoadPic(void)

...{
if(!hinstance)

...{
MessageBox(hwnd,"Timer:Load Picture:instance is null","bitmap",MB_OK);
}

hbm_number=LoadBitmap(hinstance,MAKEINTRESOURCE(IDB_TIMER_NUMBER));

if(!hbm_number)

...{
MessageBox(hwnd,"Timer:Load Picture:bitmap handle is null","bitmap",MB_OK);
}


hdc=GetDC(hwnd);
hdcmem=CreateCompatibleDC(hdc);
if(!hdcmem)

...{
MessageBox(hwnd,"Timer:Load Picture:hdcmem is null","bitmap",MB_OK);
}

ReleaseDC(hwnd,hdc);
}

void MYTIMER::SetHDC(HDC h)

...{
hdc=h;
}
void MYTIMER::Draw()

...{
int i=0;
//hdc=GetDC(hwnd);
if(!hdc)

...{
MessageBox(hwnd,"Timer:Draw:DC is null","bitmap",MB_OK);
}

SelectObject(hdcmem,hbm_number);

TextOut(hdc,pos.x,pos.y,"TIME:",5);

i=time_long/100;
i%=10;
BitBlt(hdc,pos.x,pos.y+16,16,32,hdcmem,i*16,0,SRCCOPY);
i=time_long/10;
i%=10;
BitBlt(hdc,pos.x+16,pos.y+16,16,32,hdcmem,i*16,0,SRCCOPY);

i=time_long%10;
BitBlt(hdc,pos.x+32,pos.y+16,16,32,hdcmem,i*16,0,SRCCOPY);

}

MYTIMER::MYTIMER()

...{
hwnd=0;
time_long=0;
iState=T_CLOSE;
}

MYTIMER::~MYTIMER()

...{
DeleteObject(hbm_number);

hwnd=0;
time_long=0;
if(iState!=T_CLOSE)

...{
KillTimer(hwnd,1);
}
}

void MYTIMER::SetPos(int x,int y)

...{
pos.x=x;
pos.y=y;
}
