small3
Download: small3.zip
This project demonstrates the use of a timer to provide animation.
We show the modifications to ChildView.cpp
.
Visual Studio C++ was used to add message handlers for WM_TIMER,
WM_CREATE, and WM_CLOSE messages. This automatically updated the
message map in ChildView.cpp
and added necessary headers
to ChildView.h
The OnPaint
handler was modified to provide mapping
between the world and the screen using viewport/window
transformations. A reference grid and origin marker were also added.
#define ID_TIMER 1 // CChildView BEGIN_MESSAGE_MAP(CChildView, CWnd) ON_WM_PAINT() ON_WM_TIMER() ON_WM_CREATE() ON_WM_CLOSE() END_MESSAGE_MAP() // CChildView message handlers void CChildView::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Handle isotropic mapping CRect win; GetClientRect(&win); dc.SetMapMode(MM_ISOTROPIC); dc.SetWindowExt(480,-320); dc.SetViewportExt(win.Width(),win.Height()); dc.SetViewportOrg(win.Width()/2,win.Height()/2); // Draw a reference grid CPen pen (PS_SOLID,0,RGB(220,220,255)); CPen *oldpen = (CPen *) dc.SelectObject(&pen); int u = 4*point2::resolution; for (int i=-4; i<=4; i++) { int v = i*point2::resolution; dc.MoveTo(-u,v); dc.LineTo(u,v); dc.MoveTo(v,-u); dc.LineTo(v,u); } dc.SelectObject(oldpen); int w = 5; dc.Ellipse(-w,-w,w,w); // Draw our system of three points a.draw(dc); b.draw(dc); c.draw(dc); // Do not call CWnd::OnPaint() for painting messages } void CChildView::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default a.rotate(5.0); Invalidate(); CWnd::OnTimer(nIDEvent); } int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!SetTimer(ID_TIMER,50,NULL)) { AfxMessageBox(_T("SetTimer Failed"),MB_OK); return -1; } return 0; } void CChildView::OnClose() { // TODO: Add your message handler code here and/or call default KillTimer(ID_TIMER); CWnd::OnClose(); }
The screenshot below does not capture the flavor of an animation, of course. You have to run the program itself.
Maintained by John Loomis, updated Sun Jan 28 15:28:07 2007