![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
We then get rid of the old metafile with the DeleteMetaFile() function and install the new metafile as the main metafile. class CRedraw { public: void PlayMetaFile(CDC* pRefreshDC){ HMETAFILE OldMetaFileHandle = pDC->Close(); if(OldMetaFileHandle == NULL) return; pRefreshDC->PlayMetaFile(OldMetaFileHandle); CMetaFileDC* ReplacementMetaFile = new CMetaFileDC(); ReplacementMetaFile->Create(); ReplacementMetaFile->PlayMetaFile(OldMetaFileHandle); DeleteMetaFile(OldMetaFileHandle); ⇐ delete pDC; ⇐ pDC = ReplacementMetaFile;} ⇐ }; The new metafile is ready to go, and so is the CRedraw class. Lets put it to work now. Creating the Circles ProgramTo use the CRedraw class, we include Redraw.h in the view classs header file, CirclesView.h. // CirclesView.h : interface of the CCirclesView class // /////////////////////////////////////////////////////////////////////////// . . . #include "Redraw.h" ⇐ We then create an object of the CRedraw class, redraw. class CCirclesView : public CView { protected: // create from serialization only CCirclesView(); DECLARE_DYNCREATE(CCirclesView) CPoint point1, point2; CRedraw redraw; We duplicate all our device context actions in this object and then play them back in OnDraw() when needed. void CCirclesView::OnDraw(CDC* pDC) { CCirclesDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); redraw.PlayMetaFile(pDC); ⇐ } Were ready to support the programs drawing actions, which are to draw circles using the mouse. We do so by adding the following code to OnLButtonDown() to store the location at which the mouse goes down, point1: void CCirclesView::OnLButtonDown(UINT nFlags, CPoint point) { point1 = point; ⇐ CView::OnLButtonDown(nFlags, point); } When the mouse button goes up, were supposed to draw a circle from point1 to the current point, which we name point2. void CCirclesView::OnLButtonUp(UINT nFlags, CPoint point) { point2 = point; ⇐ . . . First, we get a device context corresponding to the view. void CCirclesView::OnLButtonUp(UINT nFlags, CPoint point) { point2 = point; CClientDC* pDC; ⇐ pDC = new CClientDC(this); ⇐ . . . Now, we draw the circle in the view using Ellipse(). void CCirclesView::OnLButtonUp(UINT nFlags, CPoint point) { point2 = point; CClientDC* pDC; pDC = new CClientDC(this); pDC->Ellipse(point1.x, point1.y, point2.x, point2.y); ⇐ . . . We must duplicate all drawing actions in the metafile as well, so we use the CRedraw classs GetDC() function to get the metafile and draw in it the same way. void CCirclesView::OnLButtonUp(UINT nFlags, CPoint point) { point2 = point; CClientDC* pDC; pDC = new CClientDC(this); pDC->Ellipse(point1.x, point1.y, point2.x, point2.y); redraw.GetDC()->Ellipse(point1.x, point1.y, point2.x, point2.y); ⇐ CView::OnLButtonUp(nFlags, point); } That completes the Circles program. Run it now and draw a few circles, as shown in Figure 6.3. When you minimize and maximize the programs window, the view is refreshed from the metafile, and you can keep going with other circles as you like. Our metafile class CRedraw is a success. The code for this example, Redraw.h, appears in Listing 6.2, and CirclesView.h and CirclesView.cpp appears in Listing 6.3.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |