65.9K
CodeProject 正在变化。 阅读更多。
Home

MFC 中的无闪烁绘图

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.96/5 (169投票s)

1999年11月21日

CPOL

1分钟阅读

viewsIcon

1225943

downloadIcon

18190

一个简单的动画示例, 用于在几种模式下显示 CMemDC

Sample Image

一个简单的动画示例,用于展示 CMemDC 在几种模式下的用法

引言

消除 MFC 应用程序的闪烁是一个被广泛讨论的话题。你可以在书籍和在线资源中找到相关内容。然而,所提出的技术有些复杂,通常难以添加到现有的应用程序中。一种经常被提及的技术称为双缓冲。双缓冲允许在离屏内存中绘制新的屏幕,然后将完成的屏幕逐位图 (bit-blited) 回到物理屏幕上。

本文介绍了一个名为 CMemDC 的类,它封装了与写入离屏缓冲区相关的大部分问题。将 CMemDC 添加到现有的应用程序或 MFC Active X 控件几乎是微不足道的。

修改 MFC 应用程序以使用 CMemDC

  • 将文件 memdc.h 添加到你的项目中。
  • 将行 #include "memdc.h" 添加到 stdafx.h
  • 添加一个用于 WM_ERASEBKGND 消息的 Windows 消息处理程序。
  • 将消息处理程序中的代码更改如下
    // Change this code
    BOOL CExampleView::OnEraseBkgnd(CDC* pDC) 
    {
          // TODO: Add your message handler code here and/or call default
          return CView::OnEraseBkgnd(pDC);
    }
     
    // To this code
    BOOL CExampleView::OnEraseBkgnd(CDC* pDC) 
    {
          return FALSE;
    }
  • 将你的 OnDraw 代码更改为以下内容
    void CExampleView::OnDraw(CDC* dc)
    {
        CMemDC pDC(dc);
        CExampleDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        // TODO: add draw code for native data here - use pDC 
         //as the device context to draw to
    }

在进行这些更改后编译你的代码,你将会注意到之前看到的闪烁消失了。

修改 MFC Active X 控件以使用 CMemDC

要添加 CMemDC 支持,你遵循添加到应用程序的支持说明,但是你在 OnDraw 函数中进行一个小小的更改。

void CParticleTestCtlCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, 
                                  const CRect& rcInvalid)
{
    CMemDC pDC(pdc, &rcBounds);
    // TODO: add draw code for native data here
    // - use pDC as the device context to draw 
}

唯一的实质性区别是 rcBounds 被传递给 CMemDC 构造函数。

源代码

#ifndef _MEMDC_H_
#define _MEMDC_H_
 
//////////////////////////////////////////////////
// CMemDC - memory DC
//
// Author: Keith Rule
// Email:  keithr@europa.com
// Copyright 1996-2002, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// History - 10/3/97 Fixed scrolling bug.
//               Added print support. - KR
//
//       11/3/99 Fixed most common complaint. Added
//            background color fill. - KR
//
//       11/3/99 Added support for mapping modes other than
//            MM_TEXT as suggested by Lee Sang Hun. - KR
//
//       02/11/02 Added support for CScrollView as supplied
//             by Gary Kirkham. - KR
//
// This class implements a memory Device Context which allows
// flicker free drawing.
 
class CMemDC : public CDC {
private:       
    CBitmap    m_bitmap;        // Offscreen bitmap
    CBitmap*       m_oldBitmap; // bitmap originally found in CMemDC
    CDC*       m_pDC;           // Saves CDC passed in constructor
    CRect      m_rect;          // Rectangle of drawing area.
    BOOL       m_bMemDC;        // TRUE if CDC really is a Memory DC.
public:
    
    CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
    {
        ASSERT(pDC != NULL); 
 
        // Some initialization
        m_pDC = pDC;
        m_oldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
 
        // Get the rectangle to draw
        if (pRect == NULL) {
             pDC->GetClipBox(&m_rect);
        } else {
             m_rect = *pRect;
        }
 
        if (m_bMemDC) {
             // Create a Memory DC
             CreateCompatibleDC(pDC);
             pDC->LPtoDP(&m_rect);
 
             m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), 
                                                  m_rect.Height());
             m_oldBitmap = SelectObject(&m_bitmap);
 
             SetMapMode(pDC->GetMapMode());
 
             SetWindowExt(pDC->GetWindowExt());
             SetViewportExt(pDC->GetViewportExt());
 
             pDC->DPtoLP(&m_rect);
             SetWindowOrg(m_rect.left, m_rect.top);
        } else {
             // Make a copy of the relevant parts of the current 
             // DC for printing
             m_bPrinting = pDC->m_bPrinting;
             m_hDC       = pDC->m_hDC;
             m_hAttribDC = pDC->m_hAttribDC;
        }
 
        // Fill background 
        FillSolidRect(m_rect, pDC->GetBkColor());
    }
    
    ~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 accidentally 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;
    }
};
 
#endif
© . All rights reserved.