Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# MemDC.h

Go to the documentation of this file.

    00001 // MemDC.h : header file
    00002 //
    00003
    00004 #ifndef MEMDC_H
    00005 #define MEMDC_H
    00006
    00007 //////////////////////////////////////////////////
    00008 // CMemDC - memory DC
    00009 //
    00010 // Author: Keith Rule
    00011 // Email:  keithr@europa.com
    00012 // Copyright 1996-1997, Keith Rule
    00013 //
    00014 // You may freely use or modify this code provided this
    00015 // Copyright is included in all derived versions.
    00016 //
    00017 // History - 10/3/97 Fixed scrolling bug.
    00018 //                   Added print support.
    00019 //           25 feb 98 - fixed minor assertion bug
    00020 //
    00021 // This class implements a memory Device Context
    00022
    00023 class CMemDC : public CDC
    00024 {
    00025 public:
    00026
    00027     // constructor sets up the memory DC
    00028     CMemDC(CDC* pDC) : CDC()
    00029     {
    00030         ASSERT(pDC != NULL);
    00031
    00032         m_pDC = pDC;
    00033         m_pOldBitmap = NULL;
    00034         m_bMemDC = !pDC->IsPrinting();
    00035
    00036         if (m_bMemDC)    // Create a Memory DC
    00037         {
    00038             pDC->GetClipBox(&m_rect);
    00039             CreateCompatibleDC(pDC);
    00040             m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
    00041             m_pOldBitmap = SelectObject(&m_bitmap);
    00042             SetWindowOrg(m_rect.left, m_rect.top);
    00043         }
    00044         else        // Make a copy of the relevent parts of the current DC for printing
    00045         {
    00046             m_bPrinting = pDC->m_bPrinting;
    00047             m_hDC       = pDC->m_hDC;
    00048             m_hAttribDC = pDC->m_hAttribDC;
    00049         }
    00050     }
    00051
    00052     // Destructor copies the contents of the mem DC to the original DC
    00053     ~CMemDC()
    00054     {
    00055         if (m_bMemDC)
    00056         {
    00057             // Copy the offscreen bitmap onto the screen.
    00058             m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
    00059                           this, m_rect.left, m_rect.top, SRCCOPY);
    00060
    00061             //Swap back the original bitmap.
    00062             SelectObject(m_pOldBitmap);
    00063         } else {
    00064             // All we need to do is replace the DC with an illegal value,
    00065             // this keeps us from accidently deleting the handles associated with
    00066             // the CDC that was passed to the constructor.
    00067             m_hDC = m_hAttribDC = NULL;
    00068         }
    00069     }
    00070
    00071     // Allow usage as a pointer
    00072     CMemDC* operator->() {return this;}
    00073
    00074     // Allow usage as a pointer
    00075     operator CMemDC*() {return this;}
    00076
    00077 private:
    00078     CBitmap  m_bitmap;      // Offscreen bitmap
    00079     CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
    00080     CDC*     m_pDC;         // Saves CDC passed in constructor
    00081     CRect    m_rect;        // Rectangle of drawing area.
    00082     BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
    00083 };
    00084
    00085 /////////////////////////////////////////////////////////////////////////////
    00086
    00087 //{{AFX_INSERT_LOCATION}}
    00088 // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
    00089
    00090 #endif //MEMDC_H