65.9K
CodeProject 正在变化。 阅读更多。
Home

向基于对话框的应用程序添加启动画面

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (23投票s)

1999年12月16日

viewsIcon

241266

downloadIcon

8108

启动画面不仅适用于文档/视图型应用程序

本文包括源代码,版权 © 2001 Codejock Software,保留所有权利。  您可以自由使用本文中找到的任何源代码,不受限制。

引言

如果您曾经创建过一个对话框应用程序,然后尝试使用启动画面组件添加启动画面,您可能会发现它不允许您这样做。  本文提供了一个快速技巧,使用 CSplashWnd 类为您的对话框应用程序添加启动画面。  该类是通常由启动画面组件为文档视图项目生成的增强版本。

为了在我们的基于对话框的应用程序中使用 CSplashWnd,我们需要重写三个函数,CDialog::OnInitDialog() CWinApp::InitInstance() CWinApp::PreTranslateMessage(MSG* pMsg)。  如果您在创建项目时使用了应用程序向导,OnInitDialog InitInstance 应该已经添加到您的项目中,但是您可能需要将 PreTranslateMessage 添加到您的 CWinApp 派生类中。

步骤 1

将以下代码行添加到您的 CDialog::OnInitDialog 方法,就在它失去作用域之前。  ShowSplashScreen 的第一个参数是超时值,以毫秒为单位,这是启动画面显示的时间长度,之后它将关闭。  第二个参数是作为启动画面使用的位图图像的资源标识符。最后一个参数是父窗口,此参数可以为 NULL

// Create and show the splash screen.
CSplashWnd::ShowSplashScreen(3000, IDB_SPLASH24, this);

第二步

将以下代码行添加到 CWinApp::InitInstance 的非常开头,就在 ParseCommandLine(...) 调用之后。  如果尚未包含,则需要添加 ParseCommandLine

// Enable the splash screen component based on the command line info.
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);

步骤 3
使用类向导重写 CWinApp::PreTranslateMessage(MSG* pMsg),并添加以下代码行

BOOL CMyApp::PreTranslateMessage(MSG* pMsg) 
{
    // Route messages to the splash screen while it is visible
    if (CSplashWnd::PreTranslateAppMessage(pMsg)) {
        return TRUE;
    }
    
    return CWinApp::PreTranslateMessage(pMsg);
}

历史

2002 年 8 月 9 日 - 大幅重写。

© . All rights reserved.