您的位置:首页 > 其它

mfc 内存dc

2016-04-22 16:24 169 查看
//this class is designed to manager the memory instead of mfc
/////////////////////////////////////////////////////////////////////////////////
//if there have any bugs, pls inform me
//Tel: 021-64397490-564/565
//E-mail:x_qk@netease.com
//////////////////////////////////////////////////////////////////////////////////
class GMemDC : public CDC

{
public:
// create a memory dc
BOOL Create(CDC *pDC,             // device from which to create compatible dc
const RECT& rClient,  // client rectangle of window receiving paint message
CBrush *pBkBrush = NULL,// brush to paint background of memory DC, default is brush of 'pDC'
const PAINTSTRUCT *pPS = NULL); // paint struct if 'pDC' is actually a CPaintDC

// copy this memory dc to a dc
BOOL Copy(CDC *pDC,              // device to copy memory dc to
const RECT& rDraw,     // destination rectangle of 'pDC'
const POINT *ppt = NULL);// top-left corner of mem dc

// release the memory dc
BOOL Release();

// return the internal paint structure
const PAINTSTRUCT *GetPaintStruct() const;

public:
GMemDC();
~GMemDC();

private:
PAINTSTRUCT m_ps;                 // copy of paint dc paint structure

BOOL bPaintStructInitialized;     // internal paint structure is initialized
CBitmap *pOldBitmap;              // bitmap to restore when dc is released
CBitmap Bitmap;                   // bitmap to select in create

// privatize compiler gifts
GMemDC(const GMemDC&);
GMemDC& operator=(const GMemDC&);
};

.h

/////////////////////////////////////////////////////////////////////////////////
//if there have any bugs, pls inform me
//Tel: 021-64397490-564/565
//E-mail:x_qk@netease.com
//////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "JZMemDC.h"

GMemDC::GMemDC() : pOldBitmap(NULL), bPaintStructInitialized(FALSE)
{
}

GMemDC::~GMemDC()
{
// check if release was called
ASSERT(pOldBitmap == NULL);
ASSERT(bPaintStructInitialized == FALSE);
}

BOOL GMemDC::Create(CDC *pDC, const RECT& rDest, CBrush *pBkBrush, const PAINTSTRUCT *pPS)
{
BOOL bOK = FALSE;
pOldBitmap = NULL;

ASSERT(pDC != NULL);
ASSERT(m_hDC == NULL);

if (pDC != NULL)
{
if (CreateCompatibleDC(pDC) != 0)
{
CRect rDestRect(rDest);

#ifdef _MFC_DOCS_HAVE_NO_ERRORS_
int iBitsPerPixel = pDC->GetDeviceCaps(BITSPIXEL);

#ifdef _DEBUG
int iThisBitsPerPixel = GetDeviceCaps(BITSPIXEL);
// these should be the same
ASSERT(iThisBitsPerPixel == iBitsPerPixel);
#endif

// The documentation for CreateCompatibleBitmap() says the
// height and width parameters are in 'bits', not pixels.
// Our experience with this functions says otherwise.
int iWidth = rDestRect.Width() * iBitsPerPixel;
int iHeight = rDestRect.Height() * iBitsPerPixel;

CSize sizeBitmap(iWidth, iHeight);
#endif

CSize sizeBitmap(rDestRect.Width(), rDestRect.Height());

// create an uninitialized bitmap
if (Bitmap.CreateCompatibleBitmap(pDC, sizeBitmap.cx, sizeBitmap.cy) != 0)
{
pOldBitmap = SelectObject(&Bitmap);
if (pOldBitmap != NULL)
{
CBrush *pBrush = pBkBrush;
if (pBrush == NULL)
pBrush = pDC->GetCurrentBrush();

ASSERT(pBrush != NULL);

// initialize background
if (pBrush != NULL)
FillRect(rDestRect, pBrush);

if (pPS != NULL)
{
bPaintStructInitialized = TRUE;
memcpy((void *)&m_ps, (void *)pPS, sizeof(m_ps));
}

bOK = TRUE;
}
}
}
}

ASSERT(bOK == TRUE);

return bOK;
}

BOOL GMemDC::Copy(CDC *pDC, const RECT& rDest, const POINT *pptSource)
{
BOOL bOK = FALSE;
CRect r(rDest);

ASSERT(this != NULL);
ASSERT(pDC != NULL);
// must be true in order to use BitBlt()
ASSERT(pDC->GetDeviceCaps(RASTERCAPS) & RC_BITBLT);

POINT ptSrcTopLeft;
if (pptSource != NULL)
ptSrcTopLeft = (*pptSource);
else // default to destination rect upper-left
{
ptSrcTopLeft.x = r.left;
ptSrcTopLeft.y = r.top;
}

if (pDC != NULL)
bOK = pDC->BitBlt(r.left, r.top, r.Width(), r.Height(),
this, ptSrcTopLeft.x, ptSrcTopLeft.y, SRCCOPY);

ASSERT(bOK == TRUE);

return bOK;
}

BOOL GMemDC::Release()
{
BOOL bOK = FALSE;

ASSERT(pOldBitmap != NULL);

if (pOldBitmap != NULL)
{
SelectObject(pOldBitmap);
pOldBitmap = NULL;
bOK = TRUE;
}

bPaintStructInitialized = FALSE;

return bOK;
}

const PAINTSTRUCT *GMemDC::GetPaintStruct() const
{
const PAINTSTRUCT *pPS;

if (bPaintStructInitialized == TRUE)
pPS = (const PAINTSTRUCT *)(&m_ps);
else
pPS = NULL;

return pPS;
}
用法
void CJZRightBox::OnDraw(CDC* pDC)
{
//CDocument* pDoc = GetDocument();
CRect rct;
GMemDC m_MemDc;//内存dc
GetClientRect(&rct);
if (m_MemDc.Create(pDC, rct, NULL))
{
DrawRightBoard(&m_MemDc);
m_MemDc.Copy(pDC, rct);
m_MemDc.Release();
}

// TODO: 在此添加绘制代码
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: