Chapter 18: Progress-Based Controls Visual C++ and MFC Fundamentals
UINT SetTimer(UINT nIDEvent, UINT nElapse,
void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD));
This function call creates a timer for your application. Like the other controls, a timer
uses an identifier. This is passed as the nIDEvent argument. As mentioned already, when
it is accessed, a timer starts counting up to a set value. Once it reaches that value, it stops
and starts counting again. The nElapse argument specifies the number of milliseconds
that the timer must count before starting again. The lpfnTimer argument is the name of a
procedure that handles the timing event of the control. This argument can be set to
NULL, in which case the timing event would rest on the CWnd’s responsibility.
Practical Learning: Using Timer Controls
- To create a timer, in the OnInitDialog() event, before the return line, type
SetTimer(1, 200, 0);:
BOOL CRandShapesDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
DlgWidth = GetSystemMetrics(SM_CXSCREEN);
DlgHeight = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(&wndTopMost, 0, 0, DlgWidth, DlgHeight,
SWP_SHOWWINDOW);
SetTimer(1, 200, 0);
return TRUE; // return TRUE unless you set the focus to a control
}
- Save All
18.1.3..The Timer Messages and Methods...................................................
When a timer is accessed or made available, it starts counting. Once the nElapse value of
the CWnd::SetTimer() method is reached, its sends a WM_TIMER message to the
application.
We saw that a timer is initiated with a call to SetTimer(). When you do not need the
timer anymore, call the CWnd::KillTimer() method. Its syntax is:
BOOL KillTimer(int nIDEvent);
The nIDEvent argument identifies the timer that was created with a previous call to
SetTimer().