Splash Screen C++ Class without MFC or .NET






4.71/5 (13 votes)
A 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. Most developers use the boring “about dialog” that is created automatically. I created the class, CSplashScreen
, to handle both, to make them less error prone and to make them more interesting. 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, SplashScreen.h and SplashScreen.cpp. The class does not require MFC or .NET. I have another class that uses 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.
Splash Screen Example
|
|
Using the code
- Include SplashScreen.h and SplashScreen.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 screenstatusMessage
- string to display in status area of splash screenmillisecondsToDisplay
- number of milliseconds before hiding splash screen
To show the splash screen during initialization, add CSplashScreen::ShowSplashScreen();
.
#include "SplashScreen.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
// display splash screen and have it turn off after 10 seconds
CSplashScreen::ShowSplashScreen( hWnd, "http://applehome.com/", 10000);
.
.
.
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
To show the about box, add CSplashScreen::ShowSplashScreen()
to WndProc(…)
:
#include "SplashScreen.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_COMMAND:
// Parse the menu selections:
switch (LOWORD(wParam)) {
case IDM_ABOUT:
// display about box
CSplashScreen::ShowSplashScreen( hWnd );
break;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
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 stringm_xxxVerticalHeight
- the maximum height of a group of stringsm_xxxLeftMargin
- the distance from the left side to the place stringsm_xxxRightMargin
- the distance from the right side to stringsm_xxxFontName
- the name of the font for stringsm_xxxPointSize
- the point size used for strings, (-1,-1) ==> Calculate point sizem_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 namem_displayVersion
- true if displaying versionm_displayCopyright
- true if displaying copyrightm_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 the mouse is clicked.
History
- Date posted: April 21, 2009