CMemDC代碼解

CMemDC是一個很經典的內存DC,實現了MFC的雙緩衝繪圖。主題實現思路是,將要繪製的背景繪製到內存中,然後在CMemDC析構的時候繪製到屏幕上

  1. class CMemDC : public CDC {  
  2.     private:  
  3.         CBitmap m_bitmap;   
  4.         CBitmap* m_oldBitmap;   
  5.         CDC* m_pDC;   
  6.         CRect m_rect;   
  7.         BOOL m_bMemDC;   
  8.     public:  
  9.         //內存DC構造函數,根據傳入的pDC判斷是否爲打印機的DC。  
  10.                 //如果爲打印機的DC(m_bMemDC爲True),則創建內存m_bitmap內存DC。假如參數bCopyFirst爲真,則直接畫圖。這時就不是雙緩衝。假如  
  11.                 //參數bCopyFirst爲假,則等到CMemDC析構後才繪圖,實現雙緩衝。  
  12.                 //如果爲打印機DC(m_bMemDC爲False),則說明傳入DC非內存DC,簡單複製即可,不需要雙緩衝。  
  13.         CMemDC(CDC* pDC, CRect rect = CRect(0,0,0,0), BOOL bCopyFirst = FALSE) : CDC(), m_oldBitmap(NULL), m_pDC(pDC)  
  14.         {  
  15.             ASSERT(m_pDC != NULL); // 判斷定宏,肯定m_pDC不爲空,若爲空這不繼續進行
  16.               
  17.             m_bMemDC = !pDC->IsPrinting();  
  18.               
  19.             if (m_bMemDC){  
  20.                 // Create a Memory DC  
  21.                 CreateCompatibleDC(pDC);  //創建內存環境
  22.                 if ( rect == CRect(0,0,0,0) )  
  23.                     pDC->GetClipBox(&m_rect);  
  24.                 else  
  25.                     m_rect = rect;  
  26.   
  27.                 m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());  
  28.                 m_oldBitmap = SelectObject(&m_bitmap);  
  29.                 SetWindowOrg(m_rect.left, m_rect.top);  
  30.                 if(bCopyFirst)  
  31.                 {  
  32.                     this->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),  
  33.                             m_pDC, m_rect.left, m_rect.top, SRCCOPY);  
  34.                 }  
  35.             } else {  
  36.                 // Make a copy of the relevent parts of the current DC for printing  
  37.                 m_bPrinting = pDC->m_bPrinting;  
  38.                 m_hDC = pDC->m_hDC;  
  39.                 m_hAttribDC = pDC->m_hAttribDC;  
  40.             }  
  41.         }  
  42.           
  43.         //析構函數,假如雙緩衝模式,則繪製圖。  
  44.         ~CMemDC()  
  45.   
  46.         {  
  47.             if (m_bMemDC) {  
  48.                 // Copy the offscreen bitmap onto the screen.  
  49.                 m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),  
  50.                     this, m_rect.left, m_rect.top, SRCCOPY);  
  51.                 //Swap back the original bitmap.  
  52.                 SelectObject(m_oldBitmap);  
  53.             } else {  
  54.                 // All we need to do is replace the DC with an illegal value,  
  55.                 // this keeps us from accidently deleting the handles associated with  
  56.                 // the CDC that was passed to the constructor.  
  57.                 m_hDC = m_hAttribDC = NULL;  
  58.             }  
  59.         }  
  60.           
  61.         // Allow usage as a pointer  
  62.         CMemDC* operator->() {return this;}  
  63.           
  64.         // Allow usage as a pointer  
  65.         operator CMemDC*() {return this;}  
  66.     };  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章