API函数之三 SetTimer Function

来源:互联网 发布:我国消防现状数据 编辑:程序博客网 时间:2024/06/07 01:19

The SetTimer function creates a timer with the specified time-out value.

Syntax

  1. UINT_PTR SetTimer(      
  2.     HWND hWnd,
  3.     UINT_PTR nIDEvent,
  4.     UINT uElapse,
  5.     TIMERPROC lpTimerFunc
  6. );

Parameters

hWnd
[in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If a NULL value for hWnd is passed in along with an nIDEvent of an existing timer, that timer will be replaced in the same way that an existing non-NULL hWnd timer will be.
nIDEvent
[in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, and the nIDEvent does not match an existing timer then it is ignored and a new timer ID is generated. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimerreplaces a timer, the timer is reset. Therefore, a message will be sentafter the current time-out value elapses, but the previously settime-out value is ignored. If the call is not intended to replace anexisting timer, nIDEvent should be 0 if the hWnd is NULL.
uElapse
[in] Specifies the time-out value, in milliseconds.

Windows NT/2000/XP: If uElapse is greater than USER_TIMER_MAXIMUM, the timeout is set to 1.

Windows 2000/XP: If uElapse is less than USER_TIMER_MINIMUM, the timeout is set to USER_TIMER_MINIMUM.

Windows Server 2003: If uElapse is greater than USER_TIMER_MAXIMUM, the timeout is set to USER_TIMER_MAXIMUM.

Windows XP SP2/Windows Server 2003 SP1: If uElapse is less than USER_TIMER_MINIMUM, the timeout is set to USER_TIMER_MINIMUM. If uElapse is greater than USER_TIMER_MAXIMUM, the timeout is set to USER_TIMER_MAXIMUM.

lpTimerFunc
[in] Pointer to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc. If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message's MSG structure contains the value of the hWnd parameter.

Return Value

If the function succeeds and the hWnd parameter is NULL, the return value is an integer identifying the new timer. An application can pass this value to the KillTimer function to destroy the timer.

If the function succeeds and the hWnd parameter is not NULL, then the return value is a nonzero integer. An application can pass the value of the nIDEvent parameter to the KillTimer function to destroy the timer.

If the function fails to create a timer, the return value is zero. To get extended error information, call GetLastError.


Remarks

An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER.

The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter.

The timer identifier, nIDEvent,is specific to the associated window. Another window can have its owntimer which has the same identifier as a timer owned by another window.The timers are distinct.

SetTimer can reuse timer IDs in the case where hWnd is NULL.

原创粉丝点击