Dela via


CWnd::SetTimer

Installs a system timer.

UINT_PTR SetTimer( 
   UINT_PTR nIDEvent, 
   UINT nElapse, 
   void (CALLBACK* lpfnTimer 
)(HWND, 
   UINT, 
   UINT_PTR, 
   DWORD 
)  
);

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.

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.

Remarks

A time-out value is specified, and every time a time-out occurs, the system posts a WM_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

);

Example

This example uses CWnd::SetTimer, CWnd::OnTimer, and CWnd::KillTimer 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 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);
}

Requirements

Header: afxwin.h

See Also

Concepts

CWnd Members

Reference

CWnd Class

Hierarchy Chart

WM_TIMER

CWnd::KillTimer

SetTimer