www.pudn.com > PressMonitor_q.zip > 3DtempDiBimap.cpp


// 3DtempDiBimap.cpp: implementation of the C3DtempDiBimap class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "monitor.h" 
#include "3DtempDiBimap.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
C3DtempDiBimap::C3DtempDiBimap() 
{ 
	m_nWidth = m_nHeight = 0; 
} 
 
C3DtempDiBimap::~C3DtempDiBimap() 
{ 
 
} 
 
 
bool C3DtempDiBimap::Load(UINT nBitmapId /*= NULL*/) 
{ 
	bool bRet; 
 
	// Detach any previuos bitmap 
	DeleteObject(); 
 
	// Default return value 
	bRet = true; 
 
	// Load new bitmap 
	if (nBitmapId != NULL) 
	{ 
		bRet = GetBitmapAndPalette(nBitmapId); 
		// If all ok 
		if (bRet) 
		{ 
			BITMAP bm; 
			// Get dimension 
			GetBitmap(&bm); 
			// Width of the bitmap 
			m_nWidth = bm.bmWidth; 
			// Height of the bitmap 
			m_nHeight = bm.bmHeight; 
		} 
		else 
			m_nWidth = m_nHeight = 0; 
	} 
 
	return bRet; 
} 
 
 
bool C3DtempDiBimap::GetBitmapAndPalette(UINT nIDResource) 
{ 
	LPCTSTR lpszResourceName = (LPCTSTR)nIDResource; 
 
	m_Palette.DeleteObject(); 
 
	HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetResourceHandle(),  
		lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION); 
 
	CClientDC dc(NULL); // Desktop DC 
 
	if (hBmp == NULL) 
	{ 
		// Try to recover if 'LoadImage' failed 
		if (dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE) 
		{ 
			int nSysPalSize = dc.GetDeviceCaps(SIZEPALETTE); 
			// Make a copy of the system palette 
			LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nSysPalSize)]; 
			PALETTEENTRY *pPE = (PALETTEENTRY*)(((BYTE *)pLP) + sizeof(LOGPALETTE)); 
			pLP->palVersion = 0x300; 
			pLP->palNumEntries = nSysPalSize; 
			GetSystemPaletteEntries(dc, 0, nSysPalSize, pPE); 
			m_Palette.CreatePalette( pLP ); 
			delete[] pLP; 
 
			hBmp = ::LoadBitmap(AfxGetResourceHandle(), lpszResourceName); 
		} 
		if (hBmp == NULL) 
			return false; 
	} 
	else 
	{ 
		Attach(hBmp); 
 
		// Create a logical palette for the bitmap 
		DIBSECTION ds; 
		BITMAPINFOHEADER &bmInfo = ds.dsBmih; 
		GetObject(sizeof(ds), &ds); 
 
		int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount; 
 
		// Create a halftone palette if colors > 256.  
 
		if(nColors > 256) 
			m_Palette.CreateHalftonePalette(&dc); 
		else 
		{ 
			// Create the palette 
			RGBQUAD *pRGB = new RGBQUAD[nColors]; 
			CDC memDC; 
			memDC.CreateCompatibleDC(&dc); 
			memDC.SelectObject( this ); 
			::GetDIBColorTable( memDC, 0, nColors, pRGB ); 
			UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors); 
			LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; 
			pLP->palVersion = 0x300; 
			pLP->palNumEntries = nColors; 
			for (int i=0; i < nColors; i++) 
			{ 
				pLP->palPalEntry[i].peRed = pRGB[i].rgbRed; 
				pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen; 
				pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue; 
				pLP->palPalEntry[i].peFlags = 0; 
			} 
			m_Palette.CreatePalette( pLP ); 
			delete[] pLP; 
			delete[] pRGB; 
		} 
	} 
	return true; 
} 
 
 
void C3DtempDiBimap::RealizePalette(CDC *pDC, BOOL bBackground) 
{ 
	if(pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE && m_Palette.m_hObject != NULL) 
	{ 
		pDC->SelectPalette(&m_Palette, bBackground); 
		pDC->RealizePalette(); 
	} 
} 
 
 
void C3DtempDiBimap::Blit(CDC * pDestDC, const CRect &rect, DIStyle Style /*= DI_TOPLEFT*/) 
{ 
	int nHTiles; 
	int nVTiles; 
 
	CDC SrcDC; 
	SrcDC.CreateCompatibleDC(pDestDC); 
	SrcDC.SelectObject(this); 
 
	RealizePalette(pDestDC, FALSE); 
 
	switch (Style) 
	{ 
	default: 
	case DI_TOPLEFT: 
		nHTiles = 1; 
		nVTiles = 1; 
		pDestDC->BitBlt(0, 0, m_nWidth, m_nHeight, &SrcDC, 0, 0, SRCCOPY); 
		break; 
 
	case DI_CENTER: 
		nHTiles = 1; 
		nVTiles = 1; 
		pDestDC->BitBlt((rect.Width()-m_nWidth)/2, (rect.Height()-m_nHeight)/2, m_nWidth, m_nHeight, &SrcDC, 0, 0, SRCCOPY); 
		break; 
 
	case DI_TILE: 
		{ 
			int nHLoop; 
			int nVLoop; 
			// Calc number of horizontal tiles 
			nHTiles = (rect.Width() / m_nWidth); 
			if (rect.Width() % m_nWidth != 0) 
				nHTiles++; 
			// Calc number of vertical tiles 
			nVTiles = (rect.Height() / m_nHeight); 
			if (rect.Height() % m_nHeight != 0) 
				nVTiles++; 
 
			// Tile bitmap horizontally 
			for (nHLoop = 0; nHLoop < nHTiles; nHLoop++) 
			{ 
				// Tile bitmap vertically 
				for (nVLoop = 0; nVLoop < nVTiles; nVLoop++) 
				{ 
					pDestDC->BitBlt((nHLoop*m_nWidth), (nVLoop*m_nHeight), m_nWidth, m_nHeight, &SrcDC, 0, 0, SRCCOPY); 
				} 
			} 
		} 
		break; 
	} 
 
} // End of TileBitmap