Click here to Skip to main content
15,890,282 members
Articles / Desktop Programming / MFC

CSplashScreenEx: A Non Rectangular Splash Screen Class with Alpha Blending

Rate me:
Please Sign up or sign in to vote.
4.90/5 (65 votes)
1 Oct 20023 min read 396.3K   12.5K   194   153
CSplashScreenEx allows to display a non rectangular bitmap with information about the init of your app

Sample Image - SplashScreenEx.jpg

Introduction

A little time ago, I was looking for a Splash Screen class which would allow me to display a non rectangular bitmap without masking the background. Since I didn't find a class that matched my needs, I developed my own Splash Screen class: CSplashScreenEx.

Compatibility

This class needs MFC to be either statically or dynamically linked in your project; it has been written, compiled and tested under Visual Studio .NET (but should work well with VC6).

How to Use the Class

The class is very easy to use: Just add SplashScreenEx.h and SplashScreenEx.cpp into your project, then include SplashScreenEx.h into your application. To display the splash screen, just include these lines in your OnInitDialog() function:

C++
CSplashScreenEx *pSplash=new CSplashScreenEx();
pSplash->Create(this,NULL,2000,CSS_FADE | CSS_CENTERSCREEN | CSS_SHADOW);
pSplash->SetBitmap(IDB_SPLASH,255,0,255);
pSplash->Show();

And that's it, the splash screen appears for 2000 ms and then disappears and frees the memory allocated.

Important note: The class must be allocated on the heap since it is automatically destroyed when it disappears.

C++
CSplashScreenEx *pSplash=new CSplashScreenEx()

The splash screen can also be used to display the loading progression of your app, as seen in the Sample number 2:

C++
CSplashScreenEx *pSplash=new CSplashScreenEx();
pSplash->Create(this,"CSplashScreenEx dynamic text:",0,CSS_FADE | 
                        CSS_CENTERSCREEN | CSS_SHADOW);
pSplash->SetBitmap(IDB_SPLASH,255,0,255);
pSplash->SetTextFont("Impact",100,CSS_TEXT_NORMAL);
pSplash->SetTextRect(CRect(125,60,291,104));
pSplash->SetTextColor(RGB(255,255,255));
pSplash->SetTextFormat(DT_SINGLELINE | DT_CENTER | DT_VCENTER);
pSplash->Show();

Sleep(1000);
pSplash->SetText("You can display infos");

Sleep(1000);
pSplash->SetText("While your app is loading");

Sleep(1000);
pSplash->SetText("Just call Hide() when loading");
	
Sleep(1000);
pSplash->SetText("is finished");
Sleep(1500);

pSplash->Hide();

Class Documentation

CSplashScreenEx::Create

C++
BOOL Create(CWnd *pWndParent,LPCTSTR szText=NULL,DWORD dwTimeout=2000,
DWORD dwStyle=CSS_FADE | CSS_CENTERSCREEN | CSS_SHADOW);

Create the Splash Screen which stays hidden until Show() is called.

Arguments

  • pWndParent: The parent window (this)
  • szText: The text displayed in your splash screen
  • dwTimeout: The time in milliseconds before the splash screen disappears. If 0, the splash screen stays forever :)
  • dwStyle: Can be a combination of the following values:
    • CSS_FADEIN : Fade In animation while appearing
    • CSS_FADEOOUT: Fade Out animation while disappearing
    • CSS_FADE=CSS_FADEIN | CSS_FADEOUT : Fade in + Fade out
    • CSS_SHADOW : Displays a shadow under the splash screen
    • CSS_CENTERSCREEN : The splash screen is centered on the screen
    • CSS_CENTERAPP: The splash screen is centered on the parent window
    • CSS_HIDEONCLICK: The splash screen disappears and is destroyed when a key is pressed or it is clicked with the mouse

CSplashScreenEx::SetBitmap

C++
BOOL SetBitmap(UINT nBitmapID,short red=-1,short green=-1,short blue=-1);

BOOL SetBitmap(LPCTSTR szFileName,short red=-1,short green=-1,short blue=-1);

Associate the splash screen with a bitmap.

Arguments

  • nBitmapId: Resource ID for the bitmap
  • szFileName: Path for the bitmap on the hard drive
  • red,green,blue: Transparency color for the bitmap (RGB color). Set these to -1 to disable bitmap transparency

CSplashScreenEx::Show

C++
void Show();

Show the splash screen.

CSplashScreenEx::Hide

C++
void Hide();

Hide and destroy the splash screen.

CSplashScreenEx::SetText

C++
void SetText(LPCTSTR szText);

Change the text displayed in the splash screen.

CSplashScreenEx::SetTextFont

C++
void SetTextFont(LPCTSTR szFont,int nSize,int nStyle);

Change the Font of the text.

Arguments

nStyle can be one or a combination of the following values:

  • CSS_TEXT_NORMAL
  • CSS_TEXT_BOLD
  • CSS_TEXT_ITALIC
  • CSS_TEXT_UNDERLINE

CSplashScreenEx::SetTextDefaultFont

C++
void SetTextDefaultFont();

Set the system default font for the text displayed in the splash screen

CSplashScreenEx::SetTextColor

C++
void SetTextColor(COLORREF crTextColor);

Change the text color.

CSplashScreenEx::SetTextRect

C++
void SetTextRect(CRect& rcText);

Change the text rect boundaries.

CSplashScreenEx::SetTextFormat

C++
void SetTextFormat(UINT uTextFormat);

Change the text format; see the DrawText function in the MSDN for the different values (default is DT_CENTER | DT_VCENTER | DT_WORDBREAK).

Technical Issues

When SetBitmap() is called, if a transparency color has been specified, a GDI region is generated (thanks to Davide Pizzolato code) and this region is applied to the splash screen window (SetWindowRgn(...)). Thanks to this, the window is not anymore rectangular. The rest of the code is basic stuff, in the WM_PAINT handler, the background bitmap is blit on the window. Please refer to the code for more details about the implementation.

Conclusion

I hope this class will be useful to you, if you find any memory or GDI leaks or if you have suggestions to enhance this class, please post a comment. If you use this class in a freeware, shareware or commercial app, please let me know, I'll be happy :)

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
United States United States
I live in Santa Clara CA and work as a software engineer for SAP Business Objects.

My areas of expertise are user interface developments in Eclipse RCP / SWT / Draw 2D and C#

Comments and Discussions

 
GeneralRe: Precompiled header problems!! Pin
Stephane Rodriguez.2-Oct-02 22:47
Stephane Rodriguez.2-Oct-02 22:47 
GeneralRe: Precompiled header problems!! Pin
John O'Byrne1-Oct-02 20:07
John O'Byrne1-Oct-02 20:07 
GeneralRe: Splash screens are the scourge... Pin
ColinDavies1-Oct-02 15:02
ColinDavies1-Oct-02 15:02 
GeneralRe: Splash screens are the scourge... Pin
Shog91-Oct-02 16:21
sitebuilderShog91-Oct-02 16:21 
GeneralRe: Splash screens are the scourge... Pin
ColinDavies1-Oct-02 18:27
ColinDavies1-Oct-02 18:27 
GeneralRe: Splash screens are the scourge... Pin
Shog91-Oct-02 18:30
sitebuilderShog91-Oct-02 18:30 
GeneralRe: Splash screens are the scourge... Pin
Shog92-Oct-02 8:24
sitebuilderShog92-Oct-02 8:24 
GeneralRe: Splash screens are the scourge... Pin
Shog92-Oct-02 9:13
sitebuilderShog92-Oct-02 9:13 
Russ Freeman wrote:
Better to have the time-consuming-something working in the background and allow the customer to continue with their work. Slowness is a bug and it can be fixed.

This is true. In a perfect world, all operations would be under program control, all engineers would be happy to spend time optimizing their models for execution speed, and all users would be content to begin work on a new job while their old one was processing. In fact, two of these are possible, given time and training.

When software is able to respond instantly, any fluff that gets in the way is pointless. But that wasn't my point. For various reasons, some software must at times make the user wait. It is best to try and avoid these situations, but sometimes the cost (in programmer time) of accomplishing this is prohibitive. In such cases, it is best to do as much as is easily possible to try and distract the user from your software's problems.

Take as an example The GIMP. If you've ever used it, you'll know it takes a ridiculously long time to start up. Most of this time is spent loading plugins and extension modules. A more polished program would, no doubt, implement some sort of cache system that could be loaded quickly and indicate which modules need to be loaded, and when. But apparently there have been more important things for the developers to work on; when it comes right down to it, until every conceivable feature has been implemented there will most likely *always* be more important things for the developers to work on, as users care more about how the app behaves once it has loaded than the time it takes to load. After all, if you plan on spending 4 hours editing an image, who cares if it takes another 15-20 seconds to start up. In this situation, a splash screen is an appropriate work-around, as it provides immediate feedback as to what the application is doing (loading plugins) while still being trivial to implement.

Shog9
------
Crazy lady with the shiny shoes, where are you?
Kick your feet and calm the space that makes you hollow

Live, Insomnia And The Hole In The Universe

GeneralRe: Splash screens are the scourge... Pin
Maverik2-Oct-02 13:47
Maverik2-Oct-02 13:47 
GeneralRe: Splash screens are the scourge... Pin
Shog92-Oct-02 13:58
sitebuilderShog92-Oct-02 13:58 
GeneralRe: Splash screens are the scourge... Pin
Shog92-Oct-02 15:18
sitebuilderShog92-Oct-02 15:18 
GeneralRe: Splash screens are the scourge... Pin
John Aldrich25-Oct-02 5:00
John Aldrich25-Oct-02 5:00 
GeneralRe: Splash screens are the scourge... Pin
David Pritchard7-Oct-02 0:29
David Pritchard7-Oct-02 0:29 
GeneralRe: Splash screens are the scourge... Pin
Tallguy2-Oct-02 9:03
Tallguy2-Oct-02 9:03 
GeneralGreat Pin
Thomas Freudenberg1-Oct-02 12:57
Thomas Freudenberg1-Oct-02 12:57 
GeneralRe: Great Pin
John O'Byrne1-Oct-02 13:14
John O'Byrne1-Oct-02 13:14 
GeneralRe: Great Pin
Thomas Freudenberg1-Oct-02 13:25
Thomas Freudenberg1-Oct-02 13:25 
GeneralRe: Great Pin
John O'Byrne1-Oct-02 13:54
John O'Byrne1-Oct-02 13:54 
GeneralRe: Great Pin
Thomas Freudenberg1-Oct-02 13:58
Thomas Freudenberg1-Oct-02 13:58 
GeneralRe: Great Pin
John O'Byrne1-Oct-02 14:00
John O'Byrne1-Oct-02 14:00 

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.