1.结论
对同一定时器,多次调用SetTimer,定时器会被重置。
2.验证
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
DWORD tick;
static DWORD tick_s;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
GetClientRect(hWnd, &rect);
tick_s = ::GetTickCount();
DrawText(hdc, tmp_g, -1, &rect, DT_LEFT);
SetTimer(hWnd, 1, 3000, NULL);
Sleep(1000);
SetTimer(hWnd, 1, 3000, NULL);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_TIMER:
KillTimer(hWnd, 1);
tick = GetTickCount();
tmp_g.Format( _T("times:%ld"), tick - tick_s);
//UpdateWindow(hWnd);
InValidateRect( hWnd, NULL, TRUE ); //使用InvalidRect使窗口重绘
结果会发现界面上显示的时间段为:4000毫秒