CMemDC类的使用

这篇文章给出一个叫CMemDC的类,这个类封装了绘制离屏缓冲区的操作。因此,向一个已经存在的应用程序或者ActiveX控件中添加CMemDC来解决闪烁问题是件非常简单的事情。

 如何用CMemDC修改MFC应用程序

  • 把memdc.h添加到你的工程中
  • 在stdafx.h中添加#include "memdc.h"
  • 为WM_ERASEBKGND消息添加windows消息处理函数
  • 然后对消息处理函数做如下改动:

          

[cpp]  view plain copy
  1. // Change this code  
  2. BOOL CExampleView::OnEraseBkgnd(CDC* pDC)   
  3. {  
  4.       // TODO: Add your message handler code here and/or call default  
  5.       return CView::OnEraseBkgnd(pDC);  
  6. }  
  7.    
  8. // To this code  
  9. BOOL CExampleView::OnEraseBkgnd(CDC* pDC)   
  10. {  
  11.       return FALSE;  
  12. }  

  • 然后对你的OnDraw函数做出如下改动:

          

[cpp]  view plain copy
  1. void CExampleView::OnDraw(CDC* dc)  
  2. {  
  3.     CMemDC pDC(dc);  
  4.     CExampleDoc* pDoc = GetDocument();  
  5.     ASSERT_VALID(pDoc);  
  6.     // TODO: add draw code for native data here - use pDC   
  7.      //as the device context to draw to  
  8. }  

做出如上改动后编译你的代码,你就会发现闪烁问题已经被你解决了。

如何用CMemDC修改一个MFC Active X空间

要用CMemDC你只需要在OnDraw函数中做出一点很小的改变,如下:

 

          

[c-sharp]  view plain copy
  1. void CParticleTestCtlCtrl::OnDraw(CDC* pdc, const CRect& rcBounds,   
  2.                                   const CRect& rcInvalid)  
  3. {  
  4.     CMemDC pDC(pdc, &rcBounds);  
  5.     // TODO: add draw code for native data here  
  6.     // - use pDC as the device context to draw   
  7. }  

唯一一点区别就是rcBounds是通过CMemDC的构造函数传递的。

源代码

         

[cpp]  view plain copy
  1. #ifndef _MEMDC_H_  
  2. #define _MEMDC_H_  
  3.    
  4. //  
  5. // CMemDC - memory DC  
  6. //  
  7. // Author: Keith Rule  
  8. // Email:  keithr@europa.com  
  9. // Copyright 1996-2002, Keith Rule  
  10. //  
  11. // You may freely use or modify this code provided this  
  12. // Copyright is included in all derived versions.  
  13. //  
  14. // History - 10/3/97 Fixed scrolling bug.  
  15. //               Added print support. - KR  
  16. //  
  17. //       11/3/99 Fixed most common complaint. Added  
  18. //            background color fill. - KR  
  19. //  
  20. //       11/3/99 Added support for mapping modes other than  
  21. //            MM_TEXT as suggested by Lee Sang Hun. - KR  
  22. //  
  23. //       02/11/02 Added support for CScrollView as supplied  
  24. //             by Gary Kirkham. - KR  
  25. //  
  26. // This class implements a memory Device Context which allows  
  27. // flicker free drawing.  
  28.    
  29. class CMemDC : public CDC {  
  30. private:         
  31.     CBitmap    m_bitmap;        // Offscreen bitmap  
  32.     CBitmap*       m_oldBitmap; // bitmap originally found in CMemDC  
  33.     CDC*       m_pDC;           // Saves CDC passed in constructor  
  34.     CRect      m_rect;          // Rectangle of drawing area.  
  35.     BOOL       m_bMemDC;        // TRUE if CDC really is a Memory DC.  
  36. public:  
  37.       
  38.     CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()  
  39.     {  
  40.         ASSERT(pDC != NULL);   
  41.    
  42.         // Some initialization  
  43.         m_pDC = pDC;  
  44.         m_oldBitmap = NULL;  
  45.         m_bMemDC = !pDC->IsPrinting();  
  46.    
  47.         // Get the rectangle to draw  
  48.         if (pRect == NULL) {  
  49.              pDC->GetClipBox(&m_rect);  
  50.         } else {  
  51.              m_rect = *pRect;  
  52.         }  
  53.    
  54.         if (m_bMemDC) {  
  55.              // Create a Memory DC  
  56.              CreateCompatibleDC(pDC);  
  57.              pDC->LPtoDP(&m_rect);  
  58.    
  59.              m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(),   
  60.                                                   m_rect.Height());  
  61.              m_oldBitmap = SelectObject(&m_bitmap);  
  62.    
  63.              SetMapMode(pDC->GetMapMode());  
  64.    
  65.              SetWindowExt(pDC->GetWindowExt());  
  66.              SetViewportExt(pDC->GetViewportExt());  
  67.    
  68.              pDC->DPtoLP(&m_rect);  
  69.              SetWindowOrg(m_rect.left, m_rect.top);  
  70.         } else {  
  71.              // Make a copy of the relevent parts of the current   
  72.              // DC for printing  
  73.              m_bPrinting = pDC->m_bPrinting;  
  74.              m_hDC       = pDC->m_hDC;  
  75.              m_hAttribDC = pDC->m_hAttribDC;  
  76.         }  
  77.    
  78.         // Fill background   
  79.         FillSolidRect(m_rect, pDC->GetBkColor());  
  80.     }  
  81.       
  82.     ~CMemDC()        
  83.     {            
  84.         if (m_bMemDC) {  
  85.              // Copy the offscreen bitmap onto the screen.  
  86.              m_pDC->BitBlt(m_rect.left, m_rect.top,   
  87.                            m_rect.Width(),  m_rect.Height(),  
  88.                   this, m_rect.left, m_rect.top, SRCCOPY);              
  89.                
  90.              //Swap back the original bitmap.  
  91.              SelectObject(m_oldBitmap);          
  92.         } else {  
  93.              // All we need to do is replace the DC with an illegal  
  94.              // value, this keeps us from accidentally deleting the   
  95.              // handles associated with the CDC that was passed to   
  96.              // the constructor.                
  97.              m_hDC = m_hAttribDC = NULL;  
  98.         }         
  99.     }  
  100.       
  101.     // Allow usage as a pointer      
  102.     CMemDC* operator->()   
  103.     {  
  104.         return this;  
  105.     }         
  106.    
  107.     // Allow usage as a pointer      
  108.     operator CMemDC*()   
  109.     {  
  110.         return this;  
  111.     }  
  112. };  
  113.    
  114. #endif  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值