Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
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. Let’s put it to work now. #### Creating the Circles Program To use the CRedraw class, we include Redraw.h in the view class’s 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);                       ⇐

    }
We’re ready to support the program’s 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, we’re 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 class’s 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 program’s 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. Figure 6.3 This program has automatic screen refresh. Associating Metafiles with Device Contexts Sometimes its not enough to set up only a metafile device context, because you need to query that device context about character line height, width, and so on. However, a metafile device context doesn’t have such features. Instead, you can associate a real device context with a metafile device context using the CMetaFileDC class’s CreateEnhanced() function; queries to the metafile device context returns answers from the real device context from then on.
Previous | Table of Contents | Next

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