3. 添加现有项SplashWnd.h和SplashWnd.cpp。
SplashWnd.h源代码
#pragma once
// CSplashWnd
class CSplashWnd : public CWnd
{
DECLARE_DYNAMIC(CSplashWnd)
public:
CSplashWnd();
virtual ~CSplashWnd();
protected:
virtual void PostNcDestroy();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnPaint();
BOOL Create(CWnd* pParentWnd=NULL);
void HideSplashScreen(void);
DECLARE_MESSAGE_MAP()
protected:
CBitmap m_bitmap;
static CSplashWnd* c_pSplashWnd;
static BOOL c_bShowSplashWnd;
public:
static void EnableSplashScreen(BOOL bEnable=TRUE);
static void ShowSplashScreen(CWnd* pParentWnd=NULL);
static BOOL PreTranslateAppMessage(MSG* pMsg);
};
// SplashWnd.cpp : 实现文件
//
#include "stdafx.h"
#include "SplashWindow.h"
#include "SplashWnd.h"
#include "resource.h"
// CSplashWnd
CSplashWnd* CSplashWnd::c_pSplashWnd;
BOOL CSplashWnd::c_bShowSplashWnd;
IMPLEMENT_DYNAMIC(CSplashWnd, CWnd)
CSplashWnd::CSplashWnd()
{
}
CSplashWnd::~CSplashWnd()
{
ASSERT(c_pSplashWnd == this);
c_pSplashWnd = NULL;
}
BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_PAINT()
END_MESSAGE_MAP()
// CSplashWnd 消息处理程序
void CSplashWnd::EnableSplashScreen(BOOL bEnable)
{
c_bShowSplashWnd = bEnable;
}
void CSplashWnd::ShowSplashScreen(CWnd* pParentWnd)
{
if ( !c_bShowSplashWnd || c_pSplashWnd != NULL )
{
return;
}
c_pSplashWnd = new CSplashWnd;
if ( !c_pSplashWnd->Create(pParentWnd) )
{
delete c_pSplashWnd;
}
else
{
c_pSplashWnd->UpdateWindow();
}
}
BOOL CSplashWnd::PreTranslateAppMessage(MSG* pMsg)
{
if ( c_pSplashWnd == NULL )
{
return FALSE;
}
if ( pMsg->message == WM_KEYDOWN
|| pMsg->message == WM_SYSKEYDOWN
|| pMsg->message == WM_LBUTTONDOWN
|| pMsg->message == WM_RBUTTONDOWN
|| pMsg->message == WM_MBUTTONDOWN
|| pMsg->message == WM_NCLBUTTONDOWN
|| pMsg->message == WM_NCRBUTTONDOWN
|| pMsg->message == WM_NCMBUTTONDOWN)
{
c_pSplashWnd->HideSplashScreen();
return TRUE;
}
return FALSE;
}
void CSplashWnd::PostNcDestroy()
{
delete this;
}
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if ( CWnd::OnCreate(lpCreateStruct) == -1 )
{
return -1;
}
CenterWindow();
SetTimer(1, 3000, NULL);
return 0;
}
void CSplashWnd::OnTimer(UINT_PTR nIDEvent)
{
if ( nIDEvent == 1 )
{
HideSplashScreen();
}
}
void CSplashWnd::OnPaint()
{
CPaintDC dc(this);
CDC dcImg;
if ( !dcImg.CreateCompatibleDC(&dc) )
{
return;
}
BITMAP bm;
m_bitmap.GetBitmap(&bm);
CBitmap* pOldBit = dcImg.SelectObject(&m_bitmap);
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImg, 0, 0, SRCCOPY);
dcImg.SelectObject(pOldBit);
}
BOOL CSplashWnd::Create(CWnd* pParentWnd)
{
if ( !m_bitmap.LoadBitmap(IDB_BITMAP1) )
{
return FALSE;
}
BITMAP bm;
m_bitmap.GetBitmap(&bm);
return CreateEx(0,
AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
NULL,
WS_POPUP | WS_VISIBLE,
0, 0,
bm.bmWidth,
bm.bmHeight,
pParentWnd->GetSafeHwnd(),
NULL);
}
void CSplashWnd::HideSplashScreen()
{
DestroyWindow();
AfxGetMainWnd()->UpdateWindow();
}
{
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
}
//显示Splash窗口
CSplashWnd::ShowSplashScreen(this);
其实质就是由CDialog类完成SDI工程中CMainFrame类的工作,实现步骤如下:
(1)利用ClassWizard为CMyDialog添加WM_CREATE消息的处理函数OnCreate();(这里使用CMyDialog是为了与函数内的基类名CDialog区别。)
int CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
CSplashWnd::ShowSplashScreen(this);
return 0;
}
(2)利用ClassWizard为CWinApp添加消息转发处理函数PreTranslateMessage();
BOOL CWinApp::PreTranslateMessage(MSG* pMsg)
{
if (CSplashWnd::PreTranslateAppMessage(pMsg))
return TRUE;
return CWinApp::PreTranslateMessage(pMsg);
}
(3)CWinApp::InitInstance()中加入如下调用: CSplashWnd::EnableSplashScreen(TRUE);