www.pudn.com > HReportTest.rar > HMemDC.cpp


// HMemDC.cpp: implementation of the CHMemDC class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "HMemDC.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
IMPLEMENT_DYNAMIC(CHMemDC, CDC); 
 
////////////////////////////////////////////////////////////////////// 
// CHMemDC - implementation 
 
const COLORREF CHMemDC::m_clr3DFace = ::GetSysColor(COLOR_3DFACE); 
 
CHMemDC::CHMemDC(CDC* pDC, const CRect& rect, COLORREF clrColor) 
{ 
	ASSERT(pDC != NULL); 
	m_pDC = pDC; 
	 
	// If rect is NULL, use the device context's clip box. 
	if (rect.IsRectEmpty()) 
		m_pDC->GetClipBox(&m_rc); 
	else 
		m_rc.CopyRect(&rect); 
 
    // Create the memory DC, set Map Mode 
	if (CreateCompatibleDC(m_pDC)) 
	{ 
		SetMapMode(m_pDC->GetMapMode()); 
		 
		// Create a bitmap big enough to hold the window's image 
		m_bitmap.CreateCompatibleBitmap(m_pDC, m_rc.Width(), m_rc.Height()); 
		 
		// Select the bitmap into the memory DC 
		m_pOldBitmap = SelectObject(&m_bitmap); 
		 
		// Repaint the background, this takes the place of WM_ERASEBKGND. 
		if (clrColor != -1)  
			FillSolidRect(m_rc, clrColor); 
		 
		m_bValid = TRUE; 
	} 
 
	// Something bad happened, could be GDI leak, didn't create device context. 
	else 
	{ 
		m_bValid = FALSE; 
		m_pOldBitmap = NULL; 
	} 
} 
 
CHMemDC::~CHMemDC() 
{ 
	if (m_bValid) 
	{ 
		// Blt it 
		m_pDC->BitBlt(m_rc.left, m_rc.top, m_rc.Width(), m_rc.Height(), 
			this, 0, 0, SRCCOPY);             
	} 
 
	// Select the original bitmap back in 
	if (m_pOldBitmap != NULL) 
		SelectObject(m_pOldBitmap); 
 
	DeleteDC(); 
}