CMemDC是一個很經典的內存DC,實現了MFC的雙緩衝繪圖。主題實現思路是,將要繪製的背景繪製到內存中,然後在CMemDC析構的時候繪製到屏幕上。
- class CMemDC : public CDC {
- private:
- CBitmap m_bitmap;
- CBitmap* m_oldBitmap;
- CDC* m_pDC;
- CRect m_rect;
- BOOL m_bMemDC;
- public:
- //內存DC構造函數,根據傳入的pDC判斷是否爲打印機的DC。
- //如果爲打印機的DC(m_bMemDC爲True),則創建內存m_bitmap內存DC。假如參數bCopyFirst爲真,則直接畫圖。這時就不是雙緩衝。假如
- //參數bCopyFirst爲假,則等到CMemDC析構後才繪圖,實現雙緩衝。
- //如果爲打印機DC(m_bMemDC爲False),則說明傳入DC非內存DC,簡單複製即可,不需要雙緩衝。
- CMemDC(CDC* pDC, CRect rect = CRect(0,0,0,0), BOOL bCopyFirst = FALSE) : CDC(), m_oldBitmap(NULL), m_pDC(pDC)
- {
- ASSERT(m_pDC != NULL); // 判斷定宏,肯定m_pDC不爲空,若爲空這不繼續進行
- m_bMemDC = !pDC->IsPrinting();
- if (m_bMemDC){
- // Create a Memory DC
- CreateCompatibleDC(pDC); //創建內存環境
- if ( rect == CRect(0,0,0,0) )
- pDC->GetClipBox(&m_rect);
- else
- m_rect = rect;
- m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
- m_oldBitmap = SelectObject(&m_bitmap);
- SetWindowOrg(m_rect.left, m_rect.top);
- if(bCopyFirst)
- {
- this->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
- m_pDC, m_rect.left, m_rect.top, SRCCOPY);
- }
- } else {
- // Make a copy of the relevent parts of the current DC for printing
- m_bPrinting = pDC->m_bPrinting;
- m_hDC = pDC->m_hDC;
- m_hAttribDC = pDC->m_hAttribDC;
- }
- }
- //析構函數,假如雙緩衝模式,則繪製圖。
- ~CMemDC()
- {
- if (m_bMemDC) {
- // Copy the offscreen bitmap onto the screen.
- m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
- this, m_rect.left, m_rect.top, SRCCOPY);
- //Swap back the original bitmap.
- SelectObject(m_oldBitmap);
- } else {
- // All we need to do is replace the DC with an illegal value,
- // this keeps us from accidently deleting the handles associated with
- // the CDC that was passed to the constructor.
- m_hDC = m_hAttribDC = NULL;
- }
- }
- // Allow usage as a pointer
- CMemDC* operator->() {return this;}
- // Allow usage as a pointer
- operator CMemDC*() {return this;}
- };