资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
声明CMemoryDC类的头文件MemoryDC.h如下:#pragma once#include Afxwin.hclass CMemoryDCpublic: CMemoryDC(CDC *dc, RECT * rect,bool autoRender = false); CMemoryDC(void); bool IsOK(); void Render(CDC * p_objectDC = NULL); CDC* GetMemoryDC(); operator CDC * ();private: bool m_bAutoRender; CRect m_DCRect; CDC* m_pOriginalDC; CDC m_MemoryDC; CBitmap m_MemoryBmp;类的实现文件CMemoryDC.cpp如下:#include .MemoryDC.hCMemoryDC:CMemoryDC(CDC *dc, RECT * rect, bool autoRender) m_bAutoRender = autoRender; m_pOriginalDC = dc; if (dc=NULL | rect=NULL) return; if (!m_MemoryDC.CreateCompatibleDC(dc) return; m_DCRect.SetRect(rect-left, rect-top, rect-right, rect-bottom); if (!m_MemoryBmp.CreateCompatibleBitmap(dc, m_DCRect.Width(), m_DCRect.Height() return; m_MemoryDC.SelectObject(m_MemoryBmp);CMemoryDC:CMemoryDC(void) if (m_bAutoRender) Render(); if (m_MemoryDC.m_hDC!=NULL) m_MemoryDC.DeleteDC(); if (m_MemoryBmp.m_hObject!=NULL) m_MemoryBmp.DeleteObject();bool CMemoryDC:IsOK() return m_MemoryDC.m_hDC!=NULL & m_MemoryBmp.m_hObject != NULL;void CMemoryDC:Render(CDC * p_objectDC) if (!IsOK() return; CDC * pDC = (p_objectDC=NULL ? m_pOriginalDC : p_objectDC); CSize Size = m_MemoryDC.GetViewportExt() ; pDC-BitBlt( m_DCRect.left, m_DCRect.top, m_DCRect.Width(), m_DCRect.Height(), &m_MemoryDC, 0,0, SRCCOPY);CDC* CMemoryDC:GetMemoryDC() return & m_MemoryDC;CMemoryDC:operator CDC * () return & m_MemoryDC;使用这个类可以大大简化内存DC的创建操作。如果我们在窗口消息WM_PAINT的响应函数中使用内存DC,只要用如下这样简便的代码便可实现:CRect Rect;GetClientRect(Rect);CPaintDC dc(this); / device context for painting CMemoryDC MemDC(&dc, Rect, true); if (MemDC.IsOK() / 使用MemDC画窗口 / MemDC析构时会自动把图像复制到dc,无需其它操作使用CMemoryDC创建内存DC防止窗口闪烁,编程的代码和不使用内存DC时相比,数量和复杂性几乎没有增加。
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号