GME  13
memdc.h
Go to the documentation of this file.
00001 #ifndef _MEMDC_H_
00002 #define _MEMDC_H_
00003 
00005 // CMemDC - memory DC
00006 //
00007 // Author: Keith Rule
00008 // Email:  keithr@europa.com
00009 // Copyright 1996-2002, Keith Rule
00010 //
00011 // You may freely use or modify this code provided this
00012 // Copyright is included in all derived versions.
00013 //
00014 // History - 10/3/97 Fixed scrolling bug.
00015 //                   Added print support. - KR
00016 //
00017 //           11/3/99 Fixed most common complaint. Added
00018 //                   background color fill. - KR
00019 //
00020 //           11/3/99 Added support for mapping modes other than
00021 //                   MM_TEXT as suggested by Lee Sang Hun. - KR
00022 //
00023 //           02/11/02 Added support for CScrollView as supplied
00024 //                    by Gary Kirkham. - KR
00025 //
00026 // This class implements a memory Device Context which allows
00027 // flicker free drawing.
00028 
00029 class CMemDC : public CDC {
00030 private:        
00031         CBitmap         m_bitmap;               // Offscreen bitmap
00032         CBitmap*        m_oldBitmap;    // bitmap originally found in CMemDC
00033         CDC*            m_pDC;                  // Saves CDC passed in constructor
00034         CRect           m_rect;                 // Rectangle of drawing area.
00035         BOOL            m_bMemDC;               // TRUE if CDC really is a Memory DC.
00036 public:
00037         
00038         CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
00039         {
00040                 ASSERT(pDC != NULL); 
00041 
00042                 // Some initialization
00043                 m_pDC = pDC;
00044                 m_oldBitmap = NULL;
00045                 m_bMemDC = !pDC->IsPrinting();
00046 
00047                 // Get the rectangle to draw
00048                 if (pRect == NULL) {
00049                         pDC->GetClipBox(&m_rect);
00050                 } else {
00051                         m_rect = *pRect;
00052                 }
00053 
00054                 if (m_bMemDC) {
00055                         // Create a Memory DC
00056                         CreateCompatibleDC(pDC);
00057                         pDC->LPtoDP(&m_rect);
00058 
00059                         m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
00060                         m_oldBitmap = SelectObject(&m_bitmap);
00061 
00062                         SetMapMode(pDC->GetMapMode());
00063 
00064                         SetWindowExt(pDC->GetWindowExt());
00065                         SetViewportExt(pDC->GetViewportExt());
00066 
00067                         pDC->DPtoLP(&m_rect);
00068                         SetWindowOrg(m_rect.left, m_rect.top);
00069                 } else {
00070                         // Make a copy of the relevent parts of the current DC for printing
00071                         m_bPrinting = pDC->m_bPrinting;
00072                         m_hDC       = pDC->m_hDC;
00073                         m_hAttribDC = pDC->m_hAttribDC;
00074                 }
00075 
00076                 // Fill background 
00077                 FillSolidRect(m_rect, pDC->GetBkColor());
00078         }
00079         
00080         ~CMemDC()       
00081         {               
00082                 if (m_bMemDC) {
00083                         // Copy the offscreen bitmap onto the screen.
00084                         m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
00085                                 this, m_rect.left, m_rect.top, SRCCOPY);                        
00086                         
00087                         //Swap back the original bitmap.
00088                         SelectObject(m_oldBitmap);              
00089                 } else {
00090                         // All we need to do is replace the DC with an illegal value,
00091                         // this keeps us from accidently deleting the handles associated with
00092                         // the CDC that was passed to the constructor.                  
00093                         m_hDC = m_hAttribDC = NULL;
00094                 }       
00095         }
00096         
00097         // Allow usage as a pointer     
00098         CMemDC* operator->() 
00099         {
00100                 return this;
00101         }       
00102 
00103         // Allow usage as a pointer     
00104         operator CMemDC*() 
00105         {
00106                 return this;
00107         }
00108 };
00109 
00110 #endif