//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 05/08/2001 // PROJECT: // SUBSYSTEM: // FILE: MemoryDC.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of MemoryDC.cpp //---------------------------------------------------------------------------- // MemDC.cpp: implementation of the CMemDC class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "MemoryDC.h" #include "Version.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CMemoryDC::CMemoryDC() { m_pBitmap = NULL; m_Size.cx = m_Size.cy = NULL; } CMemoryDC::~CMemoryDC() { if( GetSafeHdc() ) DeleteDC(); } BOOL CMemoryDC::Create( HBITMAP hBitmap, BOOL bCopy ) { DIBSECTION DibSection; if( !::GetObject( hBitmap, sizeof( DibSection ), &DibSection ) ) return FALSE; int cx = DibSection.dsBm.bmWidth; int cy = DibSection.dsBm.bmHeight; if( m_Size.cx != cx && m_Size.cy != cy ) { if( GetSafeHdc() ) DeleteDC(); m_Size.cx = cx; m_Size.cy = cy; CDC *pDC = CWnd::GetDesktopWindow()->GetDC(); if( pDC ) { CreateCompatibleDC( pDC ); if( bCopy ) m_Bitmap.CreateCompatibleBitmap( pDC, m_Size.cx, m_Size.cy ); else m_Bitmap.Attach( hBitmap ); m_pBitmap = SelectObject( &m_Bitmap ); CWnd::GetDesktopWindow()->ReleaseDC( pDC ); } if( bCopy ) { CDC *pDC = CWnd::GetDesktopWindow()->GetDC(); if( pDC ) { CDC dc; dc.CreateCompatibleDC( pDC ); CBitmap bmp; bmp.Attach( hBitmap ); CBitmap *pOldBmp = dc.SelectObject( &bmp ); BitBlt( 0, 0, m_Size.cx, m_Size.cy, &dc, 0, 0, SRCCOPY ); dc.SelectObject( pOldBmp ); bmp.Detach(); CWnd::GetDesktopWindow()->ReleaseDC( pDC ); } } } return TRUE; } BOOL CMemoryDC::Create( int cx, int cy, COLORREF color ) { if( m_Size.cx != cx || m_Size.cy != cy ) { if( GetSafeHdc() ) DeleteDC(); m_Size.cx = cx; m_Size.cy = cy; CDC *pDC = CWnd::GetDesktopWindow()->GetDC(); if( pDC ) { CreateCompatibleDC( pDC ); m_Bitmap.CreateCompatibleBitmap( pDC, m_Size.cx, m_Size.cy ); m_pBitmap = SelectObject( &m_Bitmap ); CWnd::GetDesktopWindow()->ReleaseDC( pDC ); } } if( GetSafeHdc() ) { CBrush brush( color ); CRect rect( 0, 0, m_Size.cx, m_Size.cy ); FillRect( &rect, &brush ); } return TRUE; } BOOL CMemoryDC::DeleteDC() { if( m_pBitmap ) { SelectObject( m_pBitmap ); m_pBitmap = NULL; } if( m_Bitmap.GetSafeHandle() ) m_Bitmap.DeleteObject(); return CDC::DeleteDC(); }