关于定时器次数的问题
遇到个设置定时器次数的问题,始终找不到答案,定时器就是每隔多长时间就做出一个反应,比如下面的程序每隔三秒钟弹出个对话框,但是想限制对话框弹出的次数该怎么做呢?我想让它只弹出三次就不再弹出该怎么修改呢?程序如下,望高手帮忙:
#include<windows.h>
#include<stdio.h>
//全局变量
TCHAR szWindowClass[] = "演示程序";
TCHAR szWindowTitle[] = "http://www. VC驿站";
//窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam , LPARAM lParam)
{
switch(msg)
{
//WM_CREATE消息响应,设置时钟消息。
case WM_CREATE:
{
SetTimer(hWnd, 1, 3000, NULL);//设置编号为1的定时器,时间间隔3S
return 0;
}
//WM_DESTROY消息响应
case WM_DESTROY:
{
KillTimer(hWnd, 1);//销毁编号为1的时钟
PostQuitMessage(0);
return 0;
}
//WM_TIMER消息响应,时间到了自动响应这个消息。
case WM_TIMER:
MessageBox(hWnd,"时间到了","提示",0);
//不关心的消息使用缺省处理
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE, LPTSTR lpCmdLine, int nCmdShow)
{
//填充窗口类
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;
//注册窗口类
RegisterClassEx(&wcex);
//创建窗口
HWND hWnd = CreateWindowEx(
0,
szWindowClass,
szWindowTitle,
WS_OVERLAPPEDWINDOW,
128,
96,
640,
480,
HWND_DESKTOP,
NULL,
hInstance,
NULL);
//创建窗口失败
if(!hWnd)
return 0;
//显示并更新窗口
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
//设置消息循环
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 1;
}