/** * @file XTMemDC.cpp * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ #include "stdafx.h" #include "Common/XTPTypeId.h" #include "Common/XTPFramework.h" #include "Common/XTPSystemHelpers.h" #include "Common/XTPSynchro.h" #include "Common/XTPApplication.h" #include "Common/XTPSingleton.h" #include "Common/XTPGdiObjects.h" #include "Common/XTPColorManager.h" #include "Common/XTPDrawHelpers.h" #include "XTMemDC.h" #include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" #ifdef _DEBUG # undef THIS_FILE static char THIS_FILE[] = __FILE__; # define new DEBUG_NEW #endif IMPLEMENT_DYNAMIC(CXTMemDC, CDC) //=========================================================================== // CXTMemDC class //=========================================================================== CXTMemDC::CXTMemDC(CDC* pDC, const CRect& rect, CXTPPaintManagerColorGradient crBack /*=GetXtremeColor(COLOR_3DFACE)*/, BOOL bHorz /*=FALSE*/) { _ASSERTE(pDC != NULL); m_pDC = pDC; if (!m_pDC) return; // 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_hOldBitmap = (HBITMAP)SelectObject(&m_bitmap)->GetSafeHandle(); if (crBack != COLORREF_NULL) { // fill background XTPDrawHelpers()->GradientFill(this, CRect(0, 0, m_rc.Width(), m_rc.Height()), crBack, bHorz); } m_bValid = TRUE; } // Something bad happened, could be GDI leak, didn't create device context. else { m_bValid = FALSE; m_hOldBitmap = NULL; } } CXTMemDC::~CXTMemDC() { if (IsValid()) { // 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_hOldBitmap != NULL) { SelectObject(m_hOldBitmap); m_bitmap.DeleteObject(); } DeleteDC(); } CBitmap& CXTMemDC::GetBitmap() { return m_bitmap; } void CXTMemDC::Discard() { m_bValid = FALSE; } BOOL CXTMemDC::IsValid() const { return m_bValid; } void CXTMemDC::FromDC() { BitBlt(0, 0, m_rc.Width(), m_rc.Height(), m_pDC, m_rc.left, m_rc.top, SRCCOPY); }