Sometimes we need to do some tasks regularly in the program, such as 5 seconds after sending, 10 seconds after the window and other operations. This requires a timer-like component. This component is called a timer in Windows.h.
Set a timer
The first step, of course, is to set a timer, in the Win32 API, there is no concept of objects, so don't expect to be like C #:
New Timer (Callback_func); T.start (); /* ...... */ t.stop ();
Of course the above is just an example, and C # is not really the way to create a timer.
In the Win32 API, we use SetTimer (hwnd, Timerid, Elapsedmillisec, Callbackfunc) to set up a timer, and the following excerpt is modified from MSDN:
Uint_ptr WINAPI SetTimer ( hwnd hwnd, uint_ptr nidevent, UINT uelapse, timerproc lptimerfunc);
Parameters:
HWND [HWND]
Handle to the timer-related window. The window must be controlled by the thread that created the timer.
nidevent [Uint_ptr]
Non-0 timer identifier.
If this parameter is a timer ID that already exists, the timer will replace it.
If this parameter is not an existing timer ID, then this parameter is ignored and a timerid is regenerated;
If you do not want to replace the timer, this parameter should be 0 and the HWND parameter is NULL.
uelapse [UINT]
The timer time-out, in milliseconds.
If this parameter is less than User_timer_minimum (0x0000000A), User_timer_minimum is used by default.
If it is greater than user_timer_maximum (0x7FFFFFFF), User_timer_maximum is used by default.
Lptimerfunc [Timerproc]
A pointer to a function that executes at times. If this parameter is NULL, the system sends a WM_USER message to the queue.
The message that is sent by the HWND member of the MSG struct contains the HWND parameter.
return value:
If the function succeeds, the return value is the ID of the new timer. You can use this ID to delete this timer.
If the function fails with a return value of 0, call GetLastError to get more error information.
Attention:
A Wm_timer case table is included in the window procedure. But to handle wm_timer messages or to use a custom callback function at creation time.
On the thread that created the TIMER, you need to dispatch the message even if you are using a custom callback function instead of Wm_timer.
The wparam parameter of the Wm_timer is timerid-nidevent.
The timer ID, nidevent, is associated with the window. The other window can have its own timer, and the ID can be the same as the timer ID of the other window. But the timer itself is unique.
In cases where HWND is NULL, this API can reuse the ID of the timer.
[C + +] Timer in Win32 API related everything