天天看点

在基于SDI或MDI的Visual C++ 2005应用程序中添加启动画面

本文介绍如何创建以及如何插入初始屏幕,在 Visual C++.NET 中或 Visual C++2005 中。 在 Microsoft Visual Studio 6.0 您可能会在使用 Visual C++ 组件库将 MFC 单文档界面 (SDI) 应用程序中或多文档界面 (MDI) 应用程序的初始屏幕。 因为在 Visual C++.NET 中或 Visual C++2005 创建初始屏幕不直接方式使用应用程序向导生成默认 MDI 应用程序或默认 SDI 应用程序,然后中添加从 CDialog (是例如 CSplashDlg) 派生的类。 修改此代码,以将初始屏幕。 

创建并插入初始屏幕的 SDI 中的 MDI 应用程序

创建一个默认 SDI 或 MDI 应用程序

  1. 启动 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。
  2. 在 文件 菜单上指向 新建 ,然后单击 项目 。
  3. 单击 项目类型 下的 Visual C++ 项目 ,然后单击 模板 下的 MFC 应用程序 。

    请注意 在 Visual 的 Studio 2005 Visual C++ 项目 将被更改为 Visual C++ 。

  4. MyApp ,该项目命名,然后单击 确定 。
  5. 在 MFC 应用程序向导中,单击 完成 。

创建资源编辑器中的一个对话框

  1. 在 视图 菜单中上, 单击 资源视图 。
  2. 在资源视图中展开 MyApp 、 MyApp.rc ,和右键单击 。
  3. 单击 插入 。 情况默认,创建 IDD_DIALOG1。
  4. 删除 确定 按钮和 取消 按钮。 为此,右键单击每个按钮,,然后单击 删除 。
  5. 在属性窗格在 杂项 ,下双击 ID 。
  6. 在 ID 属性中单击 IDD_DIALOG1 ,然后键入 IDD_SPLASH 。
  7. 外观 下, 单击 标题栏 ,然后单击 False 下拉列表框中。
  8. 单击 边框 ,然后单击 Thin 下拉列表框中。

将文本添加到您的初始屏幕

  1. 从工具箱, 对话框编辑器 选项卡的 静态文本 控件拖放到对话框中。
  2. 在属性窗格中单击 标题 ,然后键入标题的新名称。

创建新 CDialog 派生类

  1. 双击对话框框。 在 MFC 类向导 对话框。
  2. 在 类名称 文本框中,键入 CSplashDlg ,然后单击 CDialog 从 基础类 下拉列表框。
  3. 单击 完成 。 两个文件 (SplashDlg.cpp) 和 (SplashDlg.h) 添加到您的项目。

向项目中添加函数

  1. 在 视图 菜单中上, 单击 类视图 。
  2. 在类视图窗格中展开 MyAPP ,右键单击 CSplashDlg ,指向 添加 ,然后再单击 添加函数 。
  3. 在添加成员函数向导,按照下列步骤添加以下三个函数:
    • ShowSplashScreen(CWnd* pParentWnd) : 用于显示初始屏幕的方法。
      1. 在 返回类型 列表中,单击 void。
      2. 在 函数名称 文本框中,键入 ShowSplashScreen 。
      3. 在 参数 文本框中,键入 CWnd * pParentWnd 。
      4. 单击以选中 静态 复选框。
    • HideSplashScreen() : 方法,用以销毁初始屏幕。
      1. 在 返回类型 列表中,单击 void。
      2. 在 函数名称 文本框中,键入 HideSplashScreen 。
    • PreTranslateAppMessage(MSG* pMsg) : 若要隐藏初始屏幕,每当收到键盘或鼠标消息所使用的静态方法。
      1. 在 返回类型 列表中,单击 BOOL 。
      2. 在 函数名称 文本框中,键入 PreTranslateAppMessage 。
      3. 在 参数 文本框中,键入 MSG * pMsg 。
      4. 单击以选中 静态 复选框。
  4. 若要修改在步骤 3 中创建的三种方法使用以下代码:
    void CSplashDlg::ShowSplashScreen(CWnd* pParentWnd)
    {
    
     // Allocate a new splash screen, and create the window.
      c_pSplashDlg = new CSplashDlg;
     if (!c_pSplashDlg->Create(CSplashDlg::IDD, pParentWnd))
       delete c_pSplashDlg;
     else
     c_pSplashDlg->ShowWindow(SW_SHOW);
     c_pSplashDlg->UpdateWindow();
    
     c_pSplashDlg->SetTimer(1,2000, NULL);
    }
    
    void CSplashDlg::HideSplashScreen()
    {
     // Destroy the window, and update the mainframe.
     c_pSplashDlg->KillTimer(1);
     DestroyWindow();
     AfxGetMainWnd()->UpdateWindow();
     delete c_pSplashDlg;
     c_pSplashDlg = NULL;
    }
    
    BOOL CSplashDlg::PreTranslateAppMessage(MSG* pMsg)
    {
     
     if (c_pSplashDlg == NULL)
      return FALSE;
    
       // If you receive a keyboard or a mouse message, hide the splash screen.
       if (c_pSplashDlg->m_hWnd != NULL && 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_pSplashDlg->HideSplashScreen();
       return TRUE; // message handled here
      }
    
     return FALSE; // message not handled
    
    }      

添加 OnTimer 事件

  1. 在类视图右键单击 CSplashDlg ,然后单击 属性 。
  2. 在属性窗格中单击 邮件 图标,以查看邮件。
  3. 单击 WM _ TIMER ,然后单击 <add> OnTimer.
  4. 使用下面的代码如下所示修改 OnTimer 方法:
    void CSplashDlg::OnTimer(UINT nIDEvent)
    {
     // Destroy the splash screen window.
     HideSplashScreen();
    }      
    HideSplashScreen();}

重写 CSplashDlg 类的 OnInitDialog 方法

  1. 在 视图 菜单中上, 单击 类视图 。
  2. 在类视图右键单击 CSplashDlg ,然后单击 属性 。
  3. 在属性窗格中单击 覆盖 图标以显示列表的替代方法。
  4. 单击 OnInitDialog ,然后单击 <add> OnInitDialog在下拉列表框中。
  5. 使用以下代码以修改 OnInitDialog 覆盖刚创建的方法:
    BOOL CSplashDlg::OnInitDialog()
    {
     CDialog::OnInitDialog();
     CenterWindow();
     
      SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0,
          SWP_NOMOVE|SWP_NOSIZE);
    
     return TRUE;  // return TRUE  unless you set the focus to a control
    }      

显示初始屏幕

要查看,启动画面,请按照下列步骤操作:

  1. 打开应用程序源代码文件 MyApp.cpp,,然后执行以下步骤:
    1. 在该 InitInstance() 方法的行"pMainFrame > ShowWindow(m_nCmdShow),"之前添加下面的代码:
      CSplashDlg::ShowSplashScreen(NULL);      
      请注意 在 SDI 的应用程序添加此代码,下面的行之前:"m_pMainWnd ShowWindow(SW_SHOW) >"。
    2. 重写 PreTranslateMessage 方法 CMyAppApp 的类。 要这样做,请按下列步骤操作:
      1. 在类视图右键单击 CMyAppApp ,然后单击 属性 。
      2. 在属性窗格中单击 覆盖 图标以显示列表的替代方法。
      3. 在 PreTranslateMessage 域中,单击 <add> PreTranslateMessage在下拉列表框中。
    3. 使用以下代码以修改 PreTranslateMessage 覆盖刚创建的方法:
      BOOL CMyAppApp::PreTranslateMessage(MSG* pMsg)
      {
       // TODO: Add your specialized code here, and call the base class, or both.
       if(CSplashDlg::PreTranslateAppMessage (pMsg))
        return TRUE;
      
       return CWinApp::PreTranslateMessage(pMsg);
      }      
      如果 (CSplashDlg::PreTranslateAppMessage (pMsg)) 返回 TRUE ; 返回 CWinApp::PreTranslateMessage(pMsg);}
  2. 通过在 SplashDlg.h 文件添加下面一行声明 c_pSplashDlg 静态变量:
    static CSplashDlg* c_pSplashDlg;      
  3. 在应用程序源代码文件 MyApp.cpp 的开头添加以下语句:
    #include "SplashDlg.h"
    
    CSplashDlg* CSplashDlg::c_pSplashDlg; //This is required ( in one of the files ) because the static variable 'c_pSplashDlg' is declared outside this file      
  4. 编译,然后运行应用程序。 您可能会看到的显示初始屏幕。

转载地址:http://hi.baidu.com/%B7%A2%B9%E2%B5%C4%B7%BF%D7%D3/blog/item/fe8c3acaee385d81c81768e8.html