Adding a Splash Screen to Your Applications

Environment: VC6, Win32

Introduction

Seemingly, every application I create has some lengthy processing in the WM_CREATE section of code. Sometimes, this delay before my main window is displayed causes users to click again on the application icon, thus starting yet another instance of the application. This SPLASH C++ class allows me to easily display a startup splash screen or other information before the main application window is displayed.

Before developing this class, I had tried to use Dialog boxes and timers to simulate a splash screen with limited success. But that method always had less than acceptable results. This class uses a bitmap created in your resource editor and its Resource ID to define the splash screen.

This code uses the bare Win32 API. MFC is NOT REQUIRED!

Using the SPLASH Class

The first thing to do is create the splash screen bitmap. This can be done externally, and then imported into your resource editor, or created within the editor. The resource editor will assign it an ID such as IDB_BITMAP1. This bitmap can be any size. The splash screen window will size to fit it automatically.

In your C++ code, include the SPLASH.H header file; then, create a splash class instance as follows:


#include “splash.h”
//global variables
SPLASH mysplash;

Of course, you’ll also have to include your project SPLASH.CPP. Alternatively, you can compile SPLASH.CPP and SPLASH.H to a LIB file and include that in your project.

In your WM_CREATE section of your WndProc, initialize the splash screen with the SPLASH::Init() method as follows:

  mysplash.Init(hWnd,hInst,IDB_BIMAP1);

The Init() method takes a window handle of the parent window of the splash screen, in this case hWnd. The second parameter is the instance handle of the parent Window. Of course, the third parameter is the resource ID of the splash screen bitmap.

After initializing the splash screen, two other methods are used to show or hide the splash screen, Those methods are coincidently Hide() and Show(); both take no parameters.

To display the splash screen, you would do this:

   mysplash.Show();

To hide the splash screen, oddly enough, you would do this:

   mysplash.Hide();

One member variable, (BOOL) SHOWING, is used to programmatically determine whether the splash screen is currently displayed. This allows you to set a timer and hide the splash screen after a predetermined length of time, or hide it upon a mouse click or any other window event.

Example code for this is as follows:


case WM_LBUTTONDOWN:
if(mysplash.SHOWING)
{
mysplash.Hide();
}
break;

The code below shows the SPLASH class being used in an application.


#include “splash.h”
//global variables
SPLASH mysplash;
.
.
.
WndProc(…)
.
.
.
case WM_LBUTTONDOWN:
if(mysplash.SHOWING)
{
mysplash.Hide();
}
break;
case WM_CREATE:
mysplash.Init(hWnd,hInst,IDB_BITMAP1);
mysplash.Show();
//simulate lengthy window initialization
Sleep(4000);
//hide the splash screen as the main window appears
mysplash.Hide();
break;

I hope you find this class useful in your application development. I would appreciable feedback via e-mail or the comments section of this article.

Downloads


Download source – 2 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read