Microsoft KB Archive/815376

From BetaArchive Wiki

Article ID: 815376

Article Last Modified on 1/16/2006



APPLIES TO

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition




SUMMARY

This article describes how to create and how to insert a splash screen in Visual C++ .NET or in Visual C++ 2005. In Microsoft Visual Studio 6.0, you may insert a splash screen in an MFC Single-Document Interface (SDI) application or in a Multiple-Document Interface (MDI) application by using the Visual C++ Component Gallery. Because there is no direct way to create a splash screen in Visual C++ .NET or in Visual C++ 2005, build a default MDI application or a default SDI application by using the Application Wizard, and then add a class that derives from CDialog (for example, CSplashDlg). Modify this code to be a splash screen.

back to the top

Create and Insert a Splash Screen in an SDI or an MDI Application

Create a Default SDI or MDI Application

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C++ Projects under Project Types, and then click MFC Application under Templates.


Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++.

  1. Name the project MyApp, and then click OK.
  2. In the MFC Application Wizard, click Finish.

back to the top

Create a Dialog Box in the Resource Editor

  1. On the View menu, click Resource View.
  2. In Resource View, expand MyApp, expand MyApp.rc, and then right-click Dialog.
  3. Click Insert Dialog. By default, IDD_DIALOG1 is created.
  4. Remove the OK button and the Cancel button. To do this, right-click each button, and then click Delete.
  5. In the Properties pane, under Misc, double-click ID.
  6. In the ID property, click IDD_DIALOG1, and then type IDD_SPLASH.
  7. Under Appearance, click Title Bar, and then click False in the drop-down list box.
  8. Click Border, and then click Thin in the drop-down list box.

back to the top

Add Text to Your Splash Screen

  1. Drag a Static Text control from the Dialog Editor tab of the Toolbox to the dialog box.
  2. In the Properties pane, click Caption, and then type a new name for the caption.

back to the top

Create a New CDialog Derived Class

  1. Double-click the dialog box. The MFC Class Wizard dialog box appears.
  2. In the Class name text box, type CSplashDlg, and then click CDialog from the Base class drop-down list box.
  3. Click Finish. Two files (SplashDlg.cpp) and (SplashDlg.h) are added to your project.

back to the top

Add Functions to Your Project

  1. On the View menu, click Class View.
  2. In the Class View pane, expand MyAPP, right-click CSplashDlg, point to Add, and then click Add Function.
  3. In the Add Member Function Wizard, follow these steps to add the following three functions:
    • ShowSplashScreen(CWnd* pParentWnd): Method that is used to display the splash screen.
      1. In the Return type list, click void.
      2. In the Function Name text box, type ShowSplashScreen.
      3. In the Parameter text box, type CWnd* pParentWnd.
      4. Click to select the Static check box.
    • HideSplashScreen(): Method that is used to destroy the splash screen.
      1. In the Return type list, click void.
      2. In the Function Name text box, type ShowSplashScreen.
    • PreTranslateAppMessage(MSG* pMsg): Static method that is used to hide the splash screen whenever keyboard or mouse messages are received.
      1. In the Return type list, click BOOL.
      2. In the Function Name text box, type PreTranslateAppMessage.
      3. In the Parameter text box, type MSG* pMsg.
      4. Click to select the Static check box.
  4. Use the following code to modify the three methods that you created in step 3:

    void CSplashDlg::ShowSplashScreen(CWnd* pParentWnd /*= NULL*/)
    {
    
        // 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
    
    }

    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:

    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

    These steps apply to the whole article.

back to the top

Add the OnTimer Event

  1. In Class View, right-click CSplashDlg, and then click Properties.
  2. In the Properties pane, click the Messages icon to view the messages.
  3. Click WM_TIMER, and then click <Add> OnTimer.
  4. Use the following code to modify the OnTimer method as follows:

    void CSplashDlg::OnTimer(UINT nIDEvent)
    {
        // Destroy the splash screen window.
        HideSplashScreen();
    }

back to the top

Override the OnInitDialog Method of the CSplashDlg Class

  1. On the View menu, click Class View.
  2. In Class View, right-click CSplashDlg, and then click Properties.
  3. In the Properties pane, click the Overrides icon to display the list of override methods.
  4. Click OnInitDialog, and then click <Add> OnInitDialog in the drop-down list box.
  5. Use the following code to modify the OnInitDialog override method that you just created:

    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
    }

back to the top

Display the Splash Screen

To view the splash screen, follow these steps:

  1. Open your application source file MyApp.cpp, and then follow these steps:
    1. In the InitInstance() method, before the line "pMainFrame->ShowWindow(m_nCmdShow)", add the following code:

      CSplashDlg::ShowSplashScreen(NULL);

      Note In SDI applications, add this code before the following line: "m_pMainWnd->ShowWindow(SW_SHOW)".

    2. Override the PreTranslateMessage method of the CMyAppApp class. To do this, follow these steps:
      1. In Class View, right-click CMyAppApp, and then click Properties.
      2. In the Properties pane, click the Overrides icon to display the list of override methods.
      3. In the PreTranslateMessage field, click <Add> PreTranslateMessage in the drop-down list box.
    3. Use the following code to modify the PreTranslateMessage override method that you just created:

      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);
      }
  2. Declare the c_pSplashDlg static variable by adding the following line in the SplashDlg.h file:

    static CSplashDlg* c_pSplashDlg;
  3. Add the following statement at the start of the application source file 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. Compile, and then run the application. You may see that the splash screen is displayed.

back to the top

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

190684 HOW TO: Insert a Splash Screen into a Dialog-Based Application


back to the top

Keywords: kbhowtomaster kbmdi kbdlg KB815376