⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 splashwnd.h

📁 这是一本学习 window编程的很好的参考教材
💻 H
字号:
#pragma once

#include <atlmisc.h>

class CSplashWnd :
	public CWindowImpl<CSplashWnd, CWindow, CWinTraits<WS_POPUP|WS_VISIBLE, WS_EX_TOOLWINDOW> >
{
private:
	enum
	{
		DEF_TIMER_ID		= 1001,
		DEF_TIMER_ELAPSE	= 3000,
	};
private:
	CBitmap m_bmp;
	int m_nTimeout;
	HWND m_hParent;
	BOOL m_bNoBitmap;
public:
	CSplashWnd(UINT nBitmapID, int nTimeout = DEF_TIMER_ELAPSE, HWND hParent = NULL)
		: m_nTimeout(nTimeout)
		, m_hParent(hParent)
	{
		// Load the bitmap
		m_bNoBitmap=FALSE;
		if (!m_bmp.LoadBitmap(nBitmapID))
		{
			ATLTRACE(_T("Failed to load spash bitmap\n"));
			return;
		}
		// Get the bitmap size
		CSize size;
		if (!m_bmp.GetSize(size))
		{
			ATLTRACE(_T("Failed to get spash bitmap size\n"));
			return;
		}
		// Create the window rect (we will centre the window later)
		CRect rect(0, 0, size.cx, size.cy);
		// Create the window
		if (!Create(NULL, rect))
		{
			ATLTRACE(_T("Failed to create splash window\n"));
			return;
		}
		UpdateWindow();
	}


	BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP *phBitmap,HPALETTE *phPalette )
{
		m_bNoBitmap=false;
        if (stristrA(szFileName,".bmp")==NULL) {
			AddPicture(szFileName,0,0);
			m_bNoBitmap=TRUE;
			return true;
        }

		BITMAP  bm;

		*phBitmap = NULL;
		*phPalette = NULL;

		// Use LoadImage() to get the image loaded into a DIBSection
		*phBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
					LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
		if( *phBitmap == NULL )
			return FALSE;

		// Get the color depth of the DIBSection
		GetObject(*phBitmap, sizeof(BITMAP), &bm );
		// If the DIBSection is 256 color or less, it has a color table
		if( ( bm.bmBitsPixel * bm.bmPlanes ) <= 8 )
		{
		HDC           hMemDC;
		HBITMAP       hOldBitmap;
		RGBQUAD       rgb[256];
		LPLOGPALETTE  pLogPal;
		WORD          i;

		// Create a memory DC and select the DIBSection into it
		hMemDC = CreateCompatibleDC( NULL );
		hOldBitmap = (HBITMAP)SelectObject( hMemDC, *phBitmap );
		// Get the DIBSection's color table
		GetDIBColorTable( hMemDC, 0, 256, rgb );
		// Create a palette from the color tabl
		pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) );
		pLogPal->palVersion = 0x300;
		pLogPal->palNumEntries = 256;
		for(i=0;i<256;i++)
		{
			pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
			pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
			pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
			pLogPal->palPalEntry[i].peFlags = 0;
		}
		*phPalette = CreatePalette( pLogPal );
		// Clean up
		free( pLogPal );
		SelectObject( hMemDC, hOldBitmap );
		DeleteDC( hMemDC );
		}
		else   // It has no color table, so use a halftone palette
		{
		HDC    hRefDC;

		hRefDC = ::GetDC( NULL );
		*phPalette = CreateHalftonePalette( hRefDC );
		::ReleaseDC( NULL, hRefDC );
		}
		return TRUE;

   }

	CSplashWnd(LPTSTR szFileName, int nTimeout = DEF_TIMER_ELAPSE, HWND hParent = NULL)
		: m_nTimeout(nTimeout)
		, m_hParent(hParent)
	{
		// Load the bitmap
		HBITMAP hBmp;
		HPALETTE hPal;
		LoadBitmapFromBMPFile(szFileName,&hBmp,&hPal);
		if (m_bNoBitmap) {
			return;
		}
		m_bmp.Attach(hBmp);
		
		// Get the bitmap size
		CSize size;
		if (!m_bmp.GetSize(size))
		{
			ATLTRACE(_T("Failed to get spash bitmap size\n"));
			return;
		}
		// Create the window rect (we will centre the window later)
		CRect rect(0, 0, size.cx, size.cy);
		// Create the window
		if (!Create(NULL, rect))
		{
			ATLTRACE(_T("Failed to create splash window\n"));
			return;
		}
		UpdateWindow();
	}

	/// Called when the window is destroyed
	virtual void OnFinalMessage(HWND /*hWnd*/)
	{   if(m_bNoBitmap)RemovePictures();
		delete this;
	}

	BEGIN_MSG_MAP(CSplashWnd)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_TIMER, OnTimer)
		MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
		MESSAGE_HANDLER(WM_LBUTTONDOWN, OnButtonDown)
		MESSAGE_HANDLER(WM_RBUTTONDOWN, OnButtonDown)
		MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
	END_MSG_MAP()
	
	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		CenterWindow(m_hParent);
		// Set the timer
		SetTimer(DEF_TIMER_ID, m_nTimeout);
		return 0;
	}

	LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// Draw the bmp
		CPaintDC dc(m_hWnd);
		if (m_bNoBitmap) {
			RepaintPictures(m_hWnd,dc.m_hDC);
			return 0;
		}
		CDC dcImage;
		if (dcImage.CreateCompatibleDC(dc.m_hDC))
		{
			CSize size;
			if (m_bmp.GetSize(size))
			{
				HBITMAP hBmpOld = dcImage.SelectBitmap(m_bmp);
				dc.BitBlt(0, 0, size.cx, size.cy, dcImage, 0, 0, SRCCOPY);
				dcImage.SelectBitmap(hBmpOld);
			}
		}

		return 0;
	}

	LRESULT OnTimer(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		KillTimer(DEF_TIMER_ID);
		PostMessage(WM_CLOSE);
		return 0;
	}
	
	LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// No need to paint a background
		return TRUE;
	}

	LRESULT OnButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		PostMessage(WM_CLOSE);
		return 0;
	}

	LRESULT OnKeyDown(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		if (wParam == VK_ESCAPE)
			PostMessage(WM_CLOSE);
		return 0;
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -