分类: C/C++
2008-09-14 20:55:13
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 with my needs, I developed my own Splash Screen class :- CSplashScreenEx
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)
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:
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.
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:
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();
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
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 mouseBOOL 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
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 void Show();
Show the splash screen
void Hide();
Hide and destroy the splash screen
void SetText(LPCTSTR szText);
Change the text displayed in the splash screen
void SetTextFont(LPCTSTR szFont,int nSize,int nStyle);
Change the Font of the text
nStyle
can be one or a combination of the following values:
CSS_TEXT_NORMAL
CSS_TEXT_BOLD
CSS_TEXT_ITALIC
CSS_TEXT_UNDERLINE
void SetTextDefaultFont();
Set the system default font for the text displayed in the splash screen
void SetTextColor(COLORREF crTextColor);
Change the text color
void SetTextRect(CRect& rcText);
Change the text rect boundaries
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
)
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.
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 :)