Microsoft KB Archive/817372

From BetaArchive Wiki

Article ID: 817372

Article Last Modified on 1/6/2006



APPLIES TO

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



SUMMARY

In Microsoft Visual Studio 6.0, you can insert a splash screen in an MFC Single-Document Interface (SDI) application or a Multiple-Document Interface (MDI) application by using the Visual C++ Component Gallery. However, you cannot insert a splash screen in a dialog-based application in the Visual C++ Component Gallery. For additional information about how to add a splash screen to an MFC dialog-based application in Microsoft Visual Studio 6.0, click the following article number to view the article in the Microsoft Knowledge Base:

190684 HOWTO: Insert a Splash Screen into a Dialog-Based Application


Because there is no direct way to create a splash screen in Visual C++ .NET or in Visual C++ 2005, you must build a dialog-based application by using the AppWizard, and then add a class that derives from CDialog (for example, CSplashDlg). Modify the code to be a splash screen.

back to the top

Create and Insert a Splash Screen in a Dialog-based Application

To create and insert a splash screen in dialog-based applications, follow these steps:

  1. Start 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, click Visual C++ under Project Types, and then click MFC Application under Templates.

  1. Name the project MyApp, and then click OK.
  2. The MFC Application Wizard dialog box appears.
  3. Click Application Type, click Dialog based, and then click Finish.

back to the top

Create a Dialog Box in the Resource Editor

To create a dialog box in the resource editor that is both visible and centered, with a thin border and no caption, follow these steps:

  1. In Resource View, expand MyApp.rc, right-click the Dialog folder, and then click Insert Dialog. By default, IDD_DIALOG1 is created.
  2. Remove the OK button and the Cancel button from IDD_DIALOG1. To do this, right-click each button, and then click Delete.
  3. Right-click in IDD_DIALOG1, and then click Properties.
  4. Under Misc, double-click ID.


Note In Visual Studio 2005, double-click ID under Horizontal Scrollbar.

  1. In the ID property, click IDD_DIALOG1, and then type IDD_SPLASH.
  2. Under Appearance, set the Title Bar property to False, and then set the Border property to Thin.

back to the top

Add Text to Your Splash Screen

  1. Add a Static Text control to IDD_SPLASH.
  2. Edit the text in Caption property.

back to the top

Create a New CDialog Derived Class

  1. Double-click the dialog box. The MFC Class Wizard dialog box appears.
  2. Type CSplashDlg in the Class name text box, and then click CDialog in the Base class list.
  3. Click Finish. SplashDlg.cpp and SplashDlg.h are added to your project.

back to the top

Add Functions to Your Project

  1. Declare the c_pSplashDlg static variable by adding the following line to the SplashDlg.h file:

    static CSplashDlg* c_pSplashDlg;
  2. In Class View, right-click CSplashDlg, point to Add, and then click Add Function to add the following functions:
    • ShowSplashScreen(CWnd* pParentWnd): Static method that is used to display the splash dialog.
      1. In the Return Type list, click void.
      2. In the Function Name text box, type ShowSplashScreen.
      3. In the Parameter Type list, click CWnd* .
      4. In the Parameter Name text box, type pParentWnd.
      5. Click to select the Static check box.
    • HideSplashScreen(): Method that is used to destroy the splash dialog.
      1. In the Return Type list, click void.
      2. In the Function Name text box, type HideSplashScreen.
    • PreTranslateAppMessage(MSG* pMsg): 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 Type list, click MSG*.
      4. In the Parameter Name text box, type pMsg.
      5. Click to select the Static check box.
  3. Modify the three methods that you just created as follows:

    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 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 this code sample. To do this, follow these steps:

    1. Click Project, and then click ProjectName Properties.

      Note ProjectName represents 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 on the right pane, click Apply, and then click OK.

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

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

  4. Add the OnTimer event. To do this, follow these steps:
    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. In the WM_TIMER field, click OnTimer.
    4. Modify the method as follows:

      void CSplashDlg::OnTimer(UINT nIDEvent)
      {
          // Destroy the splash screen window.
          HideSplashScreen();
      }
  5. Override the OnInitDialog method of the CSplashDlg class. To do this, follow these steps:
    1. In Class View, right-click CSplashDlg, and then click Properties.
    2. In the Properties pane, click the Overrides icon to display the list of overridable methods. In the OnInitDialog field, click <Add> OnInitDialog.
    3. Modify the OnInitDialog override method that you created in step b. To do this, follow these steps:

      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, open your application source file, MyAppDlg.cpp, and then follow these steps:

  1. Add the following lines of code at the beginning of the file:

    #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
  2. In the OnInitDialog() method, add the following code after the line CDialog::OnInitDialog():

    CSplashDlg::ShowSplashScreen(NULL);
    
  3. Override the PreTranslate Message method of the CMyAppDlg class. To do this, follow these steps:
    1. In Class View, right-click CMyAppDlg, and then click Properties
    2. In the Properties pane, click the Overrides icon to display the list of overridable methods.
    3. In the PreTranslateMessage field, click <Add> PreTranslateMessage.
  4. Modify the PreTranslateMessage override method that you created in step c as follows:

    BOOL CMyAppDlg::PreTranslateMessage(MSG* pMsg)
    {
        // TODO: Add your specialized code here and call the base class, or both.
         if (CSplashDlg::PreTranslateAppMessage(pMsg))
               return TRUE;
    
        return CDialog::PreTranslateMessage(pMsg);
    }

back to the top

Compile the Solution

Press F5 to compile the solution and to run the application. You may see that the splash screen is displayed.

back to the top

REFERENCES

For additional information about how to add a splash screen to an MFC dialog-based application in Microsoft Visual Studio 6.0, click the following article number to view the article in the Microsoft Knowledge Base:

190684 HOWTO: Insert a Splash Screen into a Dialog-Based Application


back to the top

Keywords: kbhowto kbhowtomaster kbinfo kblangcpp kbdlg KB817372