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: 在此添加绘制代码
}
相关文章推荐
- 回收键盘
- python中的setdefaultencoding函数使用
- cocos环境下AndroidStudio安装以及编译APK文件
- css常见解决方案
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
- mysql常用命令
- C# JSON字符串序列化与反序列化
- openstack中的setup.py和setup.cfg的理解
- JNI DETECTED ERROR IN APPLICATION解决记录
- 精通Groovy_A 介绍, 与Java对比, 入门, 没有类型
- ng2-bootstrap——Native Angular2 directives for Bootstrap
- js获取页面宽度高度及屏幕分辨率
- iOS 循环线程
- 部署ArcGIS JS API 离线包(Tomcat与IIS)
- ORACLE 11G R2 DG BROKER 自动FAILE OVER
- 黄聪:WordPress: 使用 wp_insert_attachment 上传附件
- 安卓利用Handlers,AsyncTask和Loaders运行后台程序
- C语言中的深拷贝和浅拷贝
- 嵌入式学习:我是这样学习嵌入式的
- MVC设计模式下的Dao的设计以及基础框架的搭建(以对数据库进行插入和查询为例)