www.pudn.com > VC写的MP3播放器源代码.zip > GWExtTransSplashWnd.cpp


// GWExtTransSplashWnd.cpp: implementation of the GWExtTransSplashWnd class. 
// 
////////////////////////////////////////////////////////////////////// 
//******************************************************************************* 
// 版权声明 
// ------------------------------------------------------------------------------ 
// 此段代码是AppBuilder扩展类库GWC的一部分. 
// 你可以使用、重新编译、或者编译成你的发行软件的一部分。 
// 但是在没有经过我们书面同意的情况下,你不能直接发行此代码文件. 
//------------------------------------------------------------------------------- 
// 版权所有. 
// 作者:张修勇 
// 拷贝日期:2000年10月05日 
//------------------------------------------------------------------------------- 
// 主页:   http://www.ucancode.com 
// 技术支持E-Mail: AppBuilder@hotmail.com 
//******************************************************************************* 
 
 
#include "stdafx.h" 
#include "GWExtDIBSectionLite.h" 
#include "GWExtTransSplashWnd.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
///////////////////////////////////////////////////////////////////////////// 
// GWExtTransSplashWnd 
 
GWExtTransSplashWnd::GWExtTransSplashWnd(UINT nBitmapID, BOOL bUseColor,COLORREF cr,UINT nDuration) 
{ 
	m_nBitmapID = nBitmapID; 
	m_nDuration = nDuration; 
 
	m_be = FALSE; 
	bUseColor = bUseColor; 
	crTransColor = cr; 
 
 
} 
 
BEGIN_MESSAGE_MAP(GWExtTransSplashWnd, CWnd) 
	//{{AFX_MSG_MAP(GWExtTransSplashWnd) 
	ON_WM_PAINT() 
	ON_WM_TIMER() 
	ON_WM_CREATE() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
 
 
BOOL GWExtTransSplashWnd::Create() 
{ 
	if( !GetBitmapAndPalette(m_nBitmapID, m_bitmap, m_pal) ) 
	{ 
		TRACE1( "Could not load bitmap resource - %d\n", m_nBitmapID ); 
		return FALSE; 
	} 
 
 
	BITMAP bm; 
	m_bitmap.GetObject(sizeof(BITMAP), &bm); 
	 
	// First create an invisible window 
	m_wndInvisible.CreateEx(WS_EX_TOPMOST,  
			AfxRegisterWndClass(CS_CLASSDC),  
			_T(""), WS_POPUP, 0, 0,  
			bm.bmWidth, bm.bmHeight, NULL, NULL); 
 
	// Create the the splash window with invisible parent as parent 
	BOOL bRetVal = CWnd::CreateEx(WS_EX_TOPMOST,  
			AfxRegisterWndClass(CS_CLASSDC),  
			_T(""), WS_POPUP, 0, 0,  
			bm.bmWidth, bm.bmHeight, m_wndInvisible.m_hWnd, NULL); 
 
	CenterWindow(); 
	ShowWindow(SW_SHOW); 
	UpdateWindow(); 
	 
	//Create the timer. 
	m_nTimerID = SetTimer(1, m_nDuration, NULL); 
	ASSERT(m_nTimerID); 
 
	if (m_be) 
		SetTimer(m_nTimerID, 5000, NULL); 
	 
	return bRetVal; 
} 
 
 
BOOL GWExtTransSplashWnd::GetBitmapAndPalette(UINT nIDResource, CBitmap &bitmap, CPalette &pal) 
{ 
	LPCTSTR lpszResourceName = (LPCTSTR)nIDResource; 
 
	HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(),  
			lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION ); 
 
	if( hBmp == NULL )  
		return FALSE; 
 
	bitmap.Attach( hBmp ); 
 
	// Create a logical palette for the bitmap 
	DIBSECTION ds; 
	BITMAPINFOHEADER &bmInfo = ds.dsBmih; 
	bitmap.GetObject( sizeof(ds), &ds ); 
 
	int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount; 
 
	// Create a halftone palette if colors > 256.  
	CClientDC dc(NULL);			// Desktop DC 
	if( nColors > 256 ) 
		pal.CreateHalftonePalette( &dc ); 
	else 
	{ 
		// Create the palette 
 
		RGBQUAD *pRGB = new RGBQUAD[nColors]; 
		CDC memDC; 
		memDC.CreateCompatibleDC(&dc); 
 
		memDC.SelectObject( &bitmap ); 
		::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; 
		} 
 
		pal.CreatePalette( pLP ); 
 
		delete[] pLP; 
		delete[] pRGB; 
	} 
 
	return TRUE; 
} 
 
void GWExtTransSplashWnd::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	 
	// Create a memory DC compatible with the paint DC 
	CDC memDC; 
	memDC.CreateCompatibleDC( &dc ); 
 
	CBitmap *pBmpOld = memDC.SelectObject( &m_bitmap ); 
 
	// Select and realize the palette 
	if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && m_pal.m_hObject != NULL ) 
	{ 
		dc.SelectPalette( &m_pal, FALSE ); 
		dc.RealizePalette(); 
	} 
 
 
	// Window is same size as bitmap 
	CRect rcWnd; 
	GetWindowRect( &rcWnd ); 
	dc.BitBlt(0, 0, rcWnd.Width(), rcWnd.Height(), &memDC, 0, 0,SRCCOPY); 
 
	 
		//You can add your control code here. 
	memDC.SelectObject( pBmpOld ); 
 
 
	// Do not call CWnd::OnPaint() for painting messages 
} 
 
void GWExtTransSplashWnd::OnTimer(UINT nIDEvent)  
{ 
	if (m_nTimerID == nIDEvent) 
	{	 
		//Destroy the timer and splash window 
		KillTimer(m_nTimerID); 
		m_wndInvisible.DestroyWindow(); 	 
		delete this; 
		return; 
	}	  
	 
	CWnd::OnTimer(nIDEvent); 
} 
 
BOOL GWExtTransSplashWnd::PreTranslateMessage(MSG* pMsg)  
{ 
	ASSERT(pMsg != NULL); 
	 
	if (pMsg->message == WM_KEYDOWN || 
			pMsg->message == WM_SYSKEYDOWN || 
			pMsg->message == WM_LBUTTONDOWN || 
			pMsg->message == WM_RBUTTONDOWN || 
			pMsg->message == WM_MBUTTONDOWN ) 
	{ 
		//Destroy the timer and splash window 
//		KillTimer(m_nTimerID); 
//		m_wndInvisible.DestroyWindow(); 	 
//		delete this; 
//		return 1; 
	}	  
	 
	return CWnd::PreTranslateMessage(pMsg); 
} 
 
int GWExtTransSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CWnd::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// 
	// Set the region. 
	// 
	CBitmap bmp; 
	bmp.LoadBitmap(m_nBitmapID); 
	CPoint pt(0,0); 
	if(bUseColor) 
	{ 
		HRGN hRgn = CreateRegionByColor(bmp, crTransColor); 
		SetWindowRgn(hRgn, TRUE); 
	} 
	else 
	{ 
		HRGN hRgn = CreateRegion(bmp, &pt); 
		SetWindowRgn(hRgn, TRUE); 
	} 
	return 0; 
} 
 
HRGN GWExtTransSplashWnd::CreateRegion(HBITMAP hBmp, LPPOINT pPoint) 
{ 
	pPoint; 
	GWExtDIBSectionLite bmp; 
	bmp.SetBitmap(hBmp); 
 
	HRGN mainRgn; 
	mainRgn = NULL; 
	HRGN tmpRgn; 
	COLORREF tc; 
	tc = RGB(0,0,0); 
	int x, y, l = 0; 
	BOOL bFirstTime = TRUE;  
	BOOL bEndofLine = FALSE; 
	 
	CDC *pMemDC = bmp.GetMemoryDC(); 
	 
	// Scan the bitmap pixel by pixel looking for transparencies 
	// and then creating a region by ORing the transparencies to remove them 
	for(y = 0; y < bmp.GetHeight(); y++)  
	{ 
		for(x = 0; x <= bmp.GetWidth(); x++)  
		{ 
			//If we are in the top left corner 
			//get the colour of this pixel 
			//This will be used as our transparency color 
			if ((x == 0 || y == 0)) 
				tc = pMemDC->GetPixel(0,0); 
			 
			//If this pixel is the same color as our tansparency 
			// or we are on the right hand side of the image 
			if((pMemDC->GetPixel(x, y) == tc) || (x == bmp.GetWidth())) 
			{ 
				//If this boolean value is true 
				if(bEndofLine)  
				{ 
					bEndofLine = FALSE; 
					 
					//Create a region which is as big as our image so far 
					tmpRgn = CreateRectRgn(l, y, x, y+1); 
					 
					//If this is the first time round 
					if(bFirstTime)  
					{ 
						mainRgn = tmpRgn; 
						bFirstTime = FALSE; 
					}  
					else  
						CombineRgn(mainRgn, mainRgn, tmpRgn, RGN_OR); 
				} 
			}  
			else  
			{ 
				if(!bEndofLine)  
				{ 
					bEndofLine = TRUE; 
					l = x; 
				} 
			} 
		} 
	} 
	 
	return mainRgn; 
} 
HRGN GWExtTransSplashWnd::CreateRegionByColor(HBITMAP hBmp, COLORREF color) 
{ 
	GWExtDIBSectionLite bmp; 
	bmp.SetBitmap(hBmp); 
 
	HRGN mainRgn; 
	mainRgn = NULL; 
	HRGN tmpRgn; 
	COLORREF tc; 
	tc = color; 
	int x, y, l = 0; 
	BOOL bFirstTime = TRUE;  
	BOOL bEndofLine = FALSE; 
	 
	CDC *pMemDC = bmp.GetMemoryDC(); 
	 
	// Scan the bitmap pixel by pixel looking for transparencies 
	// and then creating a region by ORing the transparencies to remove them 
	for(y = 0; y < bmp.GetHeight(); y++)  
	{ 
		for(x = 0; x <= bmp.GetWidth(); x++)  
		{ 
			//If we are in the top left corner 
			//get the colour of this pixel 
			//This will be used as our transparency color 
	//		if ((x == 0 || y == 0)) 
	//			tc = pMemDC->GetPixel(0,0); 
			 
			//If this pixel is the same color as our tansparency 
			// or we are on the right hand side of the image 
			if((pMemDC->GetPixel(x, y) == tc) || (x == bmp.GetWidth())) 
			{ 
				//If this boolean value is true 
				if(bEndofLine)  
				{ 
					bEndofLine = FALSE; 
					 
					//Create a region which is as big as our image so far 
					tmpRgn = CreateRectRgn(l, y, x, y+1); 
					 
					//If this is the first time round 
					if(bFirstTime)  
					{ 
						mainRgn = tmpRgn; 
						bFirstTime = FALSE; 
					}  
					else  
						CombineRgn(mainRgn, mainRgn, tmpRgn, RGN_OR); 
				} 
			}  
			else  
			{ 
				if(!bEndofLine)  
				{ 
					bEndofLine = TRUE; 
					l = x; 
				} 
			} 
		} 
	} 
	 
	return mainRgn; 
}