Error

C++博客 首页 新随笔 联系 聚合 管理
  217 Posts :: 61 Stories :: 32 Comments :: 0 Trackbacks

////////////////////////////////////////////////////////////////

简介:内存DC,又名“双缓冲”,是解决windows窗口自绘中出现“闪屏”的常规手段。

1)、为屏幕 DC 创建兼容的内存 DC
2)、创建位图
3)、把位图选入设备环境
4)、把绘制好的图形“拷贝“到屏幕上

 

看这个代码:

首先看构造,从一个CDC构造。然后看了一下成员函数,好像没几个,估计这是一个可以完全替换的CDC的省心的东东。

然后看析构:析构是一个BitBit,联想自己做内存DC的时候,最后一步也是内存到DC的贴图动作。

公开接口就两个,重载的CDC* 和 ->操作,直接能当作CDC使用。

这几个细节需要注意:

1.m_bMemDC = !pDC->IsPrinting();  // 以前关注不多,这是用于判断这个DC是不是用于print,如果是就不使用“内存DC”,至于为什么还不了解。我理解是没有需要。

2.FillSolidRect(m_rect, pDC->GetBkColor());  // WM_ERASEBKGND,针对这个消息的细节处理。

 

这个类持有了一个“前台”DC,它本身是一个“后台”DC,每次后台克隆前台执行绘画然后把结果贴回去。

这里还有一个细节,就是SelectObject。为了保证不泄漏,最好的办法是,每次工作完成将所有的GDI对象复位。

///////////////////////////////////////////////////////////////

class CMemDC : public CDC
{
public:

    // constructor sets up the memory DC
    CMemDC(CDC* pDC) : CDC()
    {
        ASSERT(pDC != NULL);

        m_pDC = pDC;
        m_pOldBitmap = NULL;
#ifndef _WIN32_WCE_NO_PRINTING
        m_bMemDC = !pDC->IsPrinting();
#else
        m_bMemDC = FALSE;
#endif

        if (m_bMemDC)    // Create a Memory DC
        {
            pDC->GetClipBox(&m_rect);
            CreateCompatibleDC(pDC);
            m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
            m_pOldBitmap = SelectObject(&m_bitmap);
#ifndef _WIN32_WCE
            SetWindowOrg(m_rect.left, m_rect.top);
#endif
            // EFW - Bug fix - Fill background in case the user has overridden
            // WM_ERASEBKGND.  We end up with garbage otherwise.
            // CJM - moved to fix a bug in the fix.
            FillSolidRect(m_rect, pDC->GetBkColor());
        }
        else        // Make a copy of the relevent parts of the current DC for printing
        {
#if !defined(_WIN32_WCE) || ((_WIN32_WCE > 201) && !defined(_WIN32_WCE_NO_PRINTING))
            m_bPrinting = pDC->m_bPrinting;
#endif
            m_hDC       = pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }

    }

    // Destructor copies the contents of the mem DC to the original DC
    ~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_pOldBitmap);
        } 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;}

private:
    CBitmap  m_bitmap;      // Offscreen bitmap
    CBitmap* m_pOldBitmap;  // 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.
};

posted on 2012-10-08 10:41 Enic 阅读(209) 评论(0)  编辑 收藏 引用 所属分类: 代码片段分享

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理