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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值