Click here to Skip to main content
Click here to Skip to main content

Splash Screen C++ Class using MFC

, 23 Apr 2009
Rate this:
Please Sign up or sign in to vote.
A MFC C++ class to generate a splash screen and about box from a bitmap and version string resources.

Introduction

Every application needs an “about box” and some need a “splash screen” while loading. I created the class, CSplashWindow, to handle both. Most developers use the boring “about dialog” that is created automatically. As a consultant, I strive to create a professional product that looks good, is free from errors and is customized to the client. CSplashWindow satisfies these requirements by having a visually appealing client graphic and all the information drawn from the version strings. Gone are the problems of mismatched version numbers and forgetting to fill in the version strings. I use this class for programs from tiny dialog applets to complex applications that take a couple of minutes to load and initialize.

The class consists of two files, SplashWindow.h and SplashWindow.cpp. The class requires MFC. I wrote another article, Splash Screen C++ Class without MFC or .NET, that doesn't use MFC.

The class gets all the information to display the splash screen from a bitmap and the version strings in the resources. Therefore, you don't have to modify the splash screen every time the version changes.

The splash screen disappears when clicked on, a key is pressed, or the desired time has elapsed.

SplashMFCSource

Splash Screen Example

Using the Code

  • Include SplashWindow.h and SplashWindow.cpp.
  • Add version strings to the resources.
  • Add the IDB_SPLASH bitmap to the resources.
  • Add version.lib to the link libraries.

ShowSplashScreen(HWND pParentWnd, LPCTSTR statusMessage, int millisecondsToDisplay) can have zero to three parameters.

  • pParentWnd - Parent window of splash screen
  • statusMessage - String to display in status area of splash screen
  • millisecondsToDisplay - Number of milliseconds before hiding splash screen

To show the splash screen during initialization, add CSplashWindow::ShowSplashScreen();:

#include SplashWindow.h

BOOL CSplashMfcDialogApp::InitInstance()
{
    AfxEnableControlContainer();

    CSplashWindow::ShowSplashScreen( NULL, NULL, 3000);

    // Standard initialization
    CSplashMfcDialogDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with Cancel
    }
    return FALSE;
}

Mouse clicks and keyboard presses have to be captured to dismiss the splash screen. I do this by using the class wizard to add the message, PreTranslateMessage, to the main application class. Insert CSplashWindow::PreTranslateAppMessage(pMsg) to let the class detect messages that dismiss the splash window.

BOOL CSplashMfcDialogApp::PreTranslateMessage(MSG* pMsg) 
{
    if (CSplashWindow::PreTranslateAppMessage(pMsg))
        return TRUE;
  
    return CWinApp::PreTranslateMessage(pMsg);
}

To show the about box, replace dlgAbout.DoModal() with CSplashWindow::ShowSplashScreen():

#include SplashWindow.h

void CSplashMfcDialogDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        // replace About Dialog with Splash window
        CSplashWindow::ShowSplashScreen( this, "http://applehome.com/", 10000);
        //  CAboutDlg dlgAbout;
        //  dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

Under the Hood

Since I'm a consultant, I create a specialized bitmap for each client. I typically place the company logo and the application icon in the IDB_SPLASH bitmap. Depending on your artistic talent, you can make a very professional splash screen. The version strings are written on top of the bitmap.

There are 3 groupings of strings: Product Name, Body, and Status. The Body consists of one or more of the company name, version, copyright, and comment strings. I prefer the product name to be larger, with the multiple lines of the body smaller. I use Status only when the application is loading.

Each group has static variables that specify how the strings in that group are drawn:

  • m_xxxVerticalOffset - empty space between the top of the bitmap and the first string
  • m_xxxVerticalHeight - the maximum height of a group of strings
  • m_xxxLeftMargin - the distance from the left side to the place strings
  • m_xxxRightMargin - the distance from the right side to strings
  • m_xxxFontName - the name of the font for strings
  • m_xxxPointSize - the point size used for strings, (-1,-1) ==> Calculate point size
  • m_xxxTextColor - the color used for strings

The body can have from 0 to 4 strings. Set the static display variables:

  • m_displayCompanyName - true if displaying company name
  • m_displayVersion - true if displaying version
  • m_displayCopyright - true if displaying copyright
  • m_displayComments - true if displaying comments

The CSplashScreen class is instantiated when CSplashScreen::ShowSplashScreen() is called. It is deleted when the timeout elapses, a key is pressed, or the mouse is clicked.

Points of Interest

I wrote this class a long time ago and have scarcely modified it since. Recently I created a very lightweight applet that didn't use MFC, and I wanted the splash screen. I bit the bullet and rewrote the class without using MFC, Splash Screen C++ Class without MFC or .NET. It is interesting to see how the code changes when using MFC. Download the source and you can examine how the identical functionality is handled with and without MFC.

History

  • Date posted: April 23, 2009

License

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

About the Author

Chris Apple
Software Developer (Senior) Apple Enterprises - http://applehome.com/
United States United States
I have been a software consultant at Apple Enterprises, http://applehome.com/, since 1981, a paid programmer since 1975 and a programmer since 1969 when I took my first programming class at U.C. Berkeley. I graduated with an EECS degree in 1975 after a 2 year hiatus in the US Marine Corps as a infantry machine gunner.
 
I started working when the original Intel 8080 microprocessor was announced. My first product was a wire bonder that had four 8080 microprocessors controlling high speed motors. Lately I have been using the TI MSP430, NXP LPC2300 and the Renesas SH4 microcontrollers.
 
I write code in assembly, C, C++ and Java and occasionally design a board when needed. Most of the time the code is system level, low level and drivers, but I do write applications. Most embedded devices usually have a PC controller application that I write. The code is used in Windows, Windows CE and proprietary operating systems. I use Visual Studio 2008, IAR and Keil IDEs. I even create single click installations, especially complex ones that require third party installs.
 
I have developed over 50 products.
 
I host the UC Berkeley Engineering Alumni Society (South Bay):
http://www.linkedin.com/groups?gid=1851655
http://www.facebook.com/reqs.php#/group.php?gid=57293762711
 
I live in Silicon Valley, play full court basketball once a week, work out at the gym during the week and hike with my wife on the weekend.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Questioncan't use in VC2005,why Pinmemberlangxj6-Aug-13 21:39 
Suggestionscanf tried replacing with wscanf_t PinmemberMember 345539825-Jul-12 12:15 
GeneralRe: scanf tried replacing with wscanf_t PinmemberChris Apple26-Jul-12 4:48 
GeneralMy vote of 5 PinmemberGeorge Dawes22-Jun-11 0:31 
QuestionUNICODE?? Pinmembermluri30-Apr-09 7:10 
AnswerRe: UNICODE?? PinmemberChris Apple30-Apr-09 7:18 
GeneralDisplay splash screen while loading data PinmemberSnakefoot28-Apr-09 10:34 
GeneralRe: Display splash screen while loading data PinmemberChris Apple28-Apr-09 11:58 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 23 Apr 2009
Article Copyright 2009 by Chris Apple
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid