Click here to Skip to main content
15,887,683 members
Articles / Desktop Programming / MFC

Easy Splash in Five Minutes

Rate me:
Please Sign up or sign in to vote.
3.53/5 (16 votes)
27 Aug 2006CPOL3 min read 50.8K   735   30   5
An easy way to create a splash for your MFC program in less than five minutes
Sample Image - EasySplash.jpg

Introduction

Have you ever wanted a quick and easy way to add a splash screen to your MFC programs?

I'm going to show you how to add a Splash to your program in less than five minutes.
All you need is a bitmap picture you want to use as your Splash.

This article is designed for Visual Studio 2005, but will probably work in earlier versions.

  • Open your projects resource view.
  • Expand the tree, right click on the rc file and choose add resource.
  • From the pop up, choose bitmap and press the import button.
Image 2
  • Browse to the location of the BMP you want to use as your splash and click open.
    Expand the bitmap leaf in the resource view and change the id TO IDB_SPLASH.
Image 3
  • Now right click on the Dialog leaf and choose "Insert Dialog".
  • Expand the dialog leaf and if the new dialog is not already open in the resource editor, double click the new IDD_DIALOG1 to open the new dialog.
  • Delete the OK and Cancel button.
  • Change the following properties for the dialog:
    • ID to IDD_SPLASH
    • Border to thin
    • Title bar to false
    • Topmost to true
    • Center to true
Image 4

Image 5

  • See the thin blue line? Drag it with the mouse until it is even with the sides of the dialog.
  • From the toolbox, drag a picture control into the dialog and place it in the upper left corner.
Image 6
  • Depending on the size of the picture you want for your splash, resize the dialog to be larger than the picture you want to use. You may need to resize the thin blue line again.
    Select the picture control.
  • Change the picture control ID to IDC_SPLASH and the type to Bitmap.
    You will notice that now that the type is bitmap the image property is no longer grayed out.
  • Change the image property with the dropdown to IDB_SPLASH.
Image 7

Image 8

  • Resize the dialog so that the picture just fits.
Image 9
  • Right click on the dialog and choose "Add Class".
  • Set the class name to CDialogSplash and hit finish.
Image 10
  • In the class view, select CDialogSplash and press the "messages" button in the properties area and scroll down to WM_TIMER.
  • Use the drop down to add an OnTimer function to the CDialogSplash class.
  • Replace the code in the OnTimer function with CDialog::OnOK();
Image 11
  • In the class view, select CDialogSplash and press the "overrides" button and scroll down to OnInitDialog.
  • Use the drop down to add an OnInitDialog function. Update the code as follows:
C++
BOOL CDialogSplash::OnInitDialog()
{
    CDialog::OnInitDialog();
    CRect rectPic, rectWin;
    GetWindowRect(&rectWin);
    m_pic.GetWindowRect(&rectPic);
    MoveWindow(rectWin.left, rectWin.top,rectPic.Width(),rectPic.Height());

    return TRUE; 
}

Ok now let's use the splash we created.

Since most people would like their splash to display before the main program finishes loading, we are going to place our splash call near the top of the InitInstance function in the Application class.

In the class view, click on your application class. For this example, we will use the demo project I created and select CEasySplashApp.

Right click CEasySplashApp and choose Add Variable. Change the access to protected, the type to CDialogSplash and the name to m_splash.

Image 12

In the class view Double click on the InitInstance function in the CEasySplashApp class and add the following code to the top of the function right after the CWinApp::InitInstance();

C++
m_splash.Create(IDD_SPLASH, this->m_pMainWnd); 
m_splash.ShowWindow(SW_SHOW);
m_splash.SetTimer(1,2000, NULL);

You should have something like this now:

Image 13

Compile your project and run it. You should see your splash display when your program starts and close after the timer runs out.

In the demo project, I added a button to show the splash again so you don't have to keep restarting the program just to see the splash again.

I hope you found this article useful.

History

  • 26th August, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a contract developer of windows based software written in C++.

Comments and Discussions

 
GeneralMessage! Pin
cristitomi19-Mar-07 5:26
cristitomi19-Mar-07 5:26 
GeneralWell Written Pin
Elias Bachaalany4-Sep-06 21:44
Elias Bachaalany4-Sep-06 21:44 
QuestionWhat is new/what is better? Pin
VictorN28-Aug-06 22:03
VictorN28-Aug-06 22:03 
GeneralTwo problems Pin
Todd Smith28-Aug-06 5:15
Todd Smith28-Aug-06 5:15 
GeneralNow, that's... Pin
Rage27-Aug-06 21:19
professionalRage27-Aug-06 21:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.