修改后的CMemDC类:(增加了红色的那行,非常重要喔。另外加了些注释)
/////////////////////////////////////////////////////////////////////////////
// CMemDC
class CMemDC : public CDC
{
public:
CDCHandle m_dc; // 屏幕的DC Owner DC
CBitmap m_bitmap; // Offscreen bitmap
CBitmapHandle m_hOldBitmap; // Originally selected bitmap
RECT m_rc; // Rectangle of drawing area
CMemDC(HDC hDC, LPRECT pRect, COLORREF clrBackGround = 0)
{
ATLASSERT(hDC!=NULL);
//保存屏幕的DC到m_dc中
m_dc = hDC;
if( pRect!=NULL )
{
m_rc = *pRect;
}
else
{
m_dc.GetClipBox(&m_rc);
}
//将被本窗口覆盖了的父窗口的区域填充至本窗口的背景
//调用CDC的CreateCompatibleDC(),它会给父类CDC的成员变量m_hDC赋值
CreateCompatibleDC(hDC);
::LPtoDP(hDC, (LPPOINT) &m_rc, sizeof(RECT)/sizeof(POINT));
//读取屏幕位图到m_bitmap
m_bitmap.CreateCompatibleBitmap(hDC, m_rc.right-m_rc.left, m_rc.bottom-m_rc.top);
//CDC选择新位图,并保存旧的位图
m_hOldBitmap = SelectBitmap(m_bitmap);
//在CDC中画图,将原先的屏幕复制到内存中
BitBlt(0, 0, m_rc.right-m_rc.left, m_rc.bottom-m_rc.top, hDC, m_rc.left, m_rc.top, SRCCOPY);
//设置CDC的背景色为#FF00FF
SetBkColor(RGB(0,255,0));
::DPtoLP(m_dc, (LPPOINT) &m_rc, sizeof(RECT)/sizeof(POINT));
SetWindowOrg(m_rc.left, m_rc.top);
//add by Robert
if(clrBackGround != 0)
{
CBrush brush;
brush.CreateSolidBrush(clrBackGround);
FillRect(pRect, (HBRUSH)brush);
}
//end add
}
~CMemDC()
{
// Copy the offscreen bitmap onto the screen.
//注意,是从父类的m_hDC中复制位图到屏幕
m_dc.BitBlt(m_rc.left, m_rc.top, m_rc.right-m_rc.left, m_rc.bottom-m_rc.top,
m_hDC, m_rc.left, m_rc.top, SRCCOPY);
// CDC恢复以前的位图 Swap back the original bitmap.
SelectBitmap(m_hOldBitmap);
}
};