|
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
}
|