分享

一个类似VS2010中的CMemDC类,VC6中用

 GANG8285 2015-08-20
/使用方法/参考  http://blog.csdn.net/mplus/article/details/6131526 ,创建CMemDC对象
//时传一个已有的DC的指针进去(大小可选)如:
/*void CDemoView::OnDraw(CDC* pDC)
 { 
	 CFont  font; 
	 font.CreateFontIndirect(&m_lf); 
	 CMemDC  pMemDC(pDC); 
	 CFont* oldfont = pMemDC->SelectObject(&font); 
        //..Draw something here. 
	 pMemDC->SelectObject(oldfont); 
 } 
*/

//MemDC.H
#ifndef _MEMDC_H_
#define _MEMDC_H_

//////////////////////////////////////////////////
// CMemDC - memory DC
//
// Author: Keith Rule
// Email:  keithr@europa.com
// Copyright 1996-1997, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// This class implements a memory Device Context

class CMemDC : public CDC 
{
private:
CBitmap* m_bitmap;
CBitmap* m_oldBitmap;
CDC* m_pDC;
CRect m_rcBounds;
bool  m_bMemDC;
public:
void Construct(CDC* pDC)
{
        ASSERT(pDC != NULL); 
        // Some initialization
        m_pDC = pDC;
        m_oldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
        if (m_bMemDC) {
            // Create a Memory DC
            CreateCompatibleDC(pDC);
            pDC->LPtoDP(&m_rcBounds);
m_bitmap = new CBitmap;
            m_bitmap->CreateCompatibleBitmap(pDC, m_rcBounds.Width(), m_rcBounds.Height());
            m_oldBitmap = SelectObject(m_bitmap);
            SetMapMode(pDC->GetMapMode());
            pDC->DPtoLP(&m_rcBounds);
            SetWindowOrg(m_rcBounds.left, m_rcBounds.top);
        } 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;
        }
        // Fill background 
        FillSolidRect(m_rcBounds, pDC->GetBkColor());
    }
CMemDC(CDC* pDC                       ) : CDC() { pDC->GetClipBox(&m_rcBounds); Construct(pDC); }
CMemDC(CDC* pDC, const CRect& rcBounds) : CDC()
{
CreateCompatibleDC(pDC);
m_bitmap = new CBitmap;
m_bitmap->CreateCompatibleBitmap(pDC, rcBounds.Width(), rcBounds.Height());
m_oldBitmap = SelectObject(m_bitmap);
m_pDC = pDC;
m_rcBounds = rcBounds;
}
~CMemDC() 
{
m_pDC->BitBlt(m_rcBounds.left, m_rcBounds.top, m_rcBounds.Width(), m_rcBounds.Height(), 
this, m_rcBounds.left, m_rcBounds.top, SRCCOPY);
SelectObject(m_oldBitmap);
if (m_bitmap != NULL) 
delete m_bitmap;
}
CMemDC* operator->() 
{
return this;
}
};

#endif

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多