Home

MFC Controls: Timers

 
 

Overview

A timer is a non-spatial object that uses recurring lapses of time from a computer or from your application. To work, every lapse of period, the control sends a message to the operating system. The message is something to the effect of "I have counted the number of lapses you asked me to count".

As opposed to the time set on your computer, a timer is partly but greatly under your control. Users do not see nor use a timer as a control. As a programmer, you decide if, why, when, and how to use this control.

The Timer Control

Unlike most other controls, the MFC timer has neither a button to represent it nor a class. To create a timer, you simply call the CWnd::SetTimer() method. Its syntax is:

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

This member 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.

Here is an example:

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
}

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().

Here is an example of a WM_TIMER message that initiates an OnTimer() event from a CRandShapesDlg class:

void CRandShapesDlg::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);

	int x = (rand() % DlgWidth) + 10;
	int y = (rand() % DlgHeight) + 10;
	CBrush BrushRand(RGB(rand() % 255, rand() % 255, rand() % 255));
	CPen PenRand(PS_SOLID, 1, RGB(rand() % 255,
	rand() % 255, rand() % 255));

	CBrush *pOldBrush = dc.SelectObject(&BrushRand);
	CPen *pOldPen = dc.SelectObject(&PenRand);

	switch(rand() % 5)
	{
		case 0:
			dc.Ellipse(x, abs(y-200), abs(y-x), y);
			break;
		case 1:
			dc.Rectangle(y, x, abs(y-x), (x+y)%255);
			break;
		case 2:
			dc.RoundRect(y, x, y, x, abs(x-y), x+y);
			break;
		case 3:
			dc.Ellipse(y, x, abs(x-y), x+y);
			break;
		case 4:
			dc.Rectangle(x, y, abs(x-y), x+y);
			break;
	}

	dc.SelectObject(pOldBrush);
	dc.SelectObject(pOldPen);

	CDialog::OnTimer(nIDEvent);
}
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);

	ShowCursor(FALSE);

	srand((unsigned)time(NULL));

	SetTimer(1, 200, 0);

	return TRUE; // return TRUE unless you set the focus to a control
}
Related Articles

Color Changer

 


Copyright © 2003-2005 FunctionX, Inc.