分类: C/C++
2008-08-01 17:02:32
//类的声明
class CSplashWnd : public CWnd { CSplashWnd(); ~CSplashWnd(); virtual void PostNcDestroy(); static void EnableSplashScreen(BOOL bEnable = TRUE); static void ShowSplashScreen(CWnd* pParentWnd = NULL); static BOOL PreTranslateAppMessage(MSG* pMsg); BOOL Create(CWnd* pParentWnd = NULL); void HideSplashScreen(); afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnPaint(); afx_msg void OnTimer(UINT nIDEvent); CBitmap m_bitmap; //SplashScreen窗口显示用的位图对象 static BOOL c_bShowSplashWnd; //是否要显示SplashScreen的标志量 static CSplashWnd* c_pSplashWnd; };//是否使用SplashScreen
void CSplashWnd::EnableSplashScreen(BOOL bEnable) { c_bShowSplashWnd = bEnable; }//创建CsplashWnd对象,并调用Create()创建窗口
void CSplashWnd::ShowSplashScreen(CWnd* pParentWnd) { //如果不要显示SplashScreen或SplashWnd对象已经被创建则返回 if (!c_bShowSplashWnd || c_pSplashWnd != NULL) return; c_pSplashWnd = new CSplashWnd; if (!c_pSplashWnd->Create(pParentWnd)) delete c_pSplashWnd; else c_pSplashWnd->UpdateWindow(); }//装入SplashScreen欲显示位图,通过CreateEx()激发OnCreate()完成窗口创建与设置
BOOL CSplashWnd::Create(CWnd* pParentWnd) { if (!m_bitmap.LoadBitmap(IDB_SPLASH)) 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(); }//利用窗口创建结构创建窗口,并设置定时器在750ms后触发OnTimer()
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; CenterWindow(); //窗口居中显示 SetTimer(1, 750, NULL); //设置定时器 return 0; }//将键盘和鼠标消息传递给CSplashWnd对象,以销毁窗口
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::OnTimer(UINT nIDEvent) { HideSplashScreen(); }再看看CWinApp和CMainFrame类中发生了什么样的改变:
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);