- 陶小凡
-
不知道你的SetTimer是MFC的还是API的,所以都给你列出来
MFC中的SetTimer
CWnd::SetTimer
UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );
Return Value
The timer identifier of the new timer if the function is successful. An application passes this value to the KillTimer member function to kill the timer. Nonzero if successful; otherwise 0.
Parameters
nIDEvent
Specifies a nonzero timer identifier.
nElapse
Specifies the time-out value, in milliseconds.
lpfnTimer
Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the WM_TIMER messages are placed in the application"s message queue and handled by the CWnd object.
Remarks
Installs a system timer. A time-out value is specified, and every time a time-out occurs, the system posts aWM_TIMER message to the installing application"s message queue or passes the message to an application-defined TimerProc callback function.
The lpfnTimer callback function need not be named TimerProc, but it must be defined as follows:
void CALLBACK EXPORT TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent // timer identification
DWORD dwTime // system time
);
Timers are a limited global resource; therefore it is important that an application check the value returned by the SetTimer member function to verify that a timer is actually available.
Example
// This example shows how to use CWnd::SetTimer, CWnd::KillTimer, and how to handle WM_TIMER messages. A timer is set up to send a WM_TIMER message to the main frame window every 2 seconds in OnStartTimer(). OnStopTimer will stop the timer by calling CWnd::KillTimer. OnTimer was set up through class wizard to handle WM_TIMER messages for the main frame window. In this example the PC speaker will beep every 2 seconds.
void CMainFrame::OnStartTimer()
{
m_nTimer = SetTimer(1, 2000, 0);
}
void CMainFrame::OnStopTimer()
{
KillTimer(m_nTimer);
}
void CMainFrame::OnTimer(UINT nIDEvent)
{
MessageBeep(0xFFFFFFFF); // Beep
// Call base class handler.
CMDIFrameWnd::OnTimer(nIDEvent);
}
Windows API中的SetTimer
SetTimer
The SetTimer function creates a timer with the specified time-out value.
UINT_PTR SetTimer(
HWND hWnd, // handle to window
UINT_PTR nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // timer procedure
);
Parameters
hWnd
[in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.
nIDEvent
[in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored.
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 SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored.
uElapse
[in] Specifies the time-out value, in milliseconds.
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 Values
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 nIDTimer 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 own timer which has the same identifier as a timer owned by another window. The timers are distinct.
- 苏州马小云
-
用WM_TIMER来设置定时器SetTimer函数的原型UINT_PTR SetTimer(HWND hWnd, // 窗口句柄UINT_PTR nIDEvent, // 定时器ID,多个定时器时,可以通过该ID判断是哪个定时器UINT nElapse, // 时间间隔,单位为毫秒TIMERPROC lpTimerFunc // 回调函数);返回值:类型:UINT_PTR如果函数成功,hWnd参数为0,则返回新建立的时钟编号,可以把这个时钟编号传递给KillTimer来销毁时钟.如果函数成功,hWnd参数为非0,则返回一个非零的整数,可以把这个非零的整数传递给KillTimer来销毁时钟.如果函数失败,返回值是零.若想获得更多的错误信息,调用GetLastError函数.
- 北境漫步
-
Windows API
SetTimer(HWND,UNIT,UINT,TIMERPROC);
参数意义:
1.记时器所在窗口句柄
2.序号
3.记时周期
4.记时器响应函数
CWnd类的
CWnd::SetTimer
UINT SetTimer(
UINT nIDEvent,
UINT nElapse,
void (CALLBACK EXPORT* lpfnTimer)(
HWND, UINT, UINT, DWORD) );
CWnd类的用例:
void CMainFrame::OnStartTimer()
{ m_nTimer = SetTimer(1, 2000, 0);
}
void CMainFrame::OnStopTimer()
{KillTimer(m_nTimer);
}
void CMainFrame::OnTimer(UINT nIDEvent)
{
MessageBeep(0xFFFFFFFF); // Beep
// Call base class handler.
CMDIFrameWnd::OnTimer(nIDEvent);
}