BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_CREATE () ON_WM_TIMER () END_MESSAGE_MAP () int CMainWindow::OnCreate (LPCREATESTRUCT lpcs) { if (CFrameWnd::OnCreate (lpcs) == -1) return -1; if (!SetTimer (ID_TIMER_ELLIPSE, 100, NULL) ¦¦ !SetTimer (ID_TIMER_RECTANGLE, 100, NULL)) { MessageBox (_T ("Error: SetTimer failed")); return -1; } return 0; } void CMainWindow::OnTimer (UINT nTimerID) { CRect rect; GetClientRect (&rect); int x1 = rand () % rect.right; int x2 = rand () % rect.right; int y1 = rand () % rect.bottom; int y2 = rand () % rect.bottom; CClientDC dc (this); CBrush brush (RGB (rand () % 255, rand () % 255, rand () % 255)); CBrush* pOldBrush = dc.SelectObject (&brush); if (nTimerID == ID_TIMER_ELLIPSE) dc.Ellipse (min (x1, x2), min (y1, y2), max (x1, x2), max (y1, y2)); else // nTimerID == ID_TIMER_RECTANGLE dc.Rectangle (min (x1, x2), min (y1, y2), max (x1, x2), max (y1, y2)); dc.SelectObject (pOldBrush); }