Click here to Skip to main content
Email Password   helpLost your password?

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 with 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:

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();

Class Documentation

CSplashScreenEx::Create

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

CSplashScreenEx::SetBitmap

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

CSplashScreenEx::Show

void Show();

Show the splash screen

CSplashScreenEx::Hide

void Hide();

Hide and destroy the splash screen

CSplashScreenEx::SetText

void SetText(LPCTSTR szText);

Change the text displayed in the splash screen

CSplashScreenEx::SetTextFont

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:

CSplashScreenEx::SetTextDefaultFont

void SetTextDefaultFont();

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

CSplashScreenEx::SetTextColor

void SetTextColor(COLORREF crTextColor);

Change the text color

CSplashScreenEx::SetTextRect

void SetTextRect(CRect& rcText);

Change the text rect boundaries

CSplashScreenEx::SetTextFormat

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 :)

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionVS2008 Unicode MDI gives assert
Divya Rathore
17:34 25 Aug '08  
if (!CreateEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,_T("SplashScreenExClass"),NULL,WS_POPUP,0,0,0,0,hParentWnd,NULL))
gives following on debugging:

Warning: Window creation failed: GetLastError returns 0x0000057F

as 'wincore.cpp' fails here:

HWND hWnd = ::AfxCtxCreateWindowEx(cs.dwExStyle, cs.lpszClass,
cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);

#ifdef _DEBUG
if (hWnd == NULL)
{
TRACE(traceAppMsg, 0, "Warning: Window creation failed: GetLastError returns 0x%8.8X\n",
GetLastError());
}

I tried calling the class in InitInstance() of MDI app after creation of MainFrame and ParseCommandLine(cmdInfo); like this:

//Splash Screen
CSplashScreenEx *pSplash=new CSplashScreenEx();
pSplash->Create(GetMainWnd(),NULL,2000,CSS_FADE | CSS_CENTERSCREEN | CSS_SHADOW);
pSplash->SetBitmap(IDB_SPLASH,255,0,255);
pSplash->Show();

Am using VS2008, WinXP, UNICODE settings.
Generalhow to show splash screen during loading dlls?
yph20040107
16:04 27 Nov '07  
The program of my company contains many dlls, and it costs much time to load all of them. So I want to show a splash screen during loading time. Could you tell me how to do? Thanks.

GeneralProbably a bug?
min_2_max
17:01 16 Aug '07  
I think this line is to be inserted into CSplashScreenEx::~CSplashScreenEx():

m_myFont.DeleteObject(); // Max: 2007-8-17 bugfix

Max



General(Small ?) Problem on Windows Vista [modified]
Fran-No
6:39 8 Aug '07  
Hi!

There is a small problem that I don't succeed in resolving with Windows Vista:

For example, with the project sample with this article, when I compile it (with Visual2003 or Visual2005) and run it on Vista, the bitmap appears first during one second approximatively with its "pink" border... OMG Then, this color disappears during the rest of the bitmap view

Is anybody had an idea to solve this problem please?


-- modified at 6:30 Friday 10th August, 2007
GeneralRe: (Small ?) Problem on Windows Vista
Fran-No
1:30 10 Aug '07  
I found a solution for my problem: In the Show function, I changed the second parameter of the called function m_fnAnimateWindow:
m_fnAnimateWindow(m_hWnd,1,AW_BLEND);

It seems that the pink border of the bitmap stays only during the apparition animation of the splash screen, and disappears at the end of this animation. So, if this animation became instantaneous, the pink border won't appear...

I think it could possible to correct this bug in the fnAnimateWindow function... If somebody has an idea...

Thanks! Smile
GeneralRe: (Small ?) Problem on Windows Vista
Fran-No
3:25 30 Aug '07  
After some tests, I found that this bug only appears when the Aero mode is activated...
When the Aero mode is desactivated, it runs normally.

Is somebody had an idea?

Somebody knows if it is possible to desactivate automatically the Aero mode for an application when it is started?

Thank you in advance Smile
GeneralRe: (Small ?) Problem on Windows Vista
kelvin_gl
22:10 13 Jan '08  
I found that this bug could be avoid if CSS_FADE was not set in flag.

It is a great class in general! Many Thanks to the author.
GeneralRe: (Small ?) Problem on Windows Vista
Fran-No
1:20 14 Jan '08  
I've already tested your suggestion: Indeed, the problem is avoid if CSS_FADE is not set in flag, but this avoid transition effects too! Cry
GeneralPixel format Generic Implementation only
mitesh velani
17:11 20 Sep '06  
Fantastic splashscreen. However after the splash screen comes up I also get a dialog titled "Pixel Format" and reads "Generic Implementation Only!" What does that mean, and how can I fix this?

Thanks a million

mitesh
QuestionSetWindowPos() in Show() causes problem
bitkidoku
6:09 21 Dec '05  
I am trying to show the splash screen when user clicks a button. But when I call Show() it fails. But strange thing is it works well sometimes. Why can this happen? (I am a MFC newbie, so if you want more information please ask)

Thanks for your answers Smile
GeneralImprovement: Multiple Text Fields
Michael Stammberger
13:37 17 Nov '05  
Hi,

I have created an extended version with multiple text fields.
With this it's possible to display more than one text on the splashscreen. Each text can have its own color, font & position. In addition I added the improvements of kintz & yogurt which they posted here.
It's completely backward compatible with the original version.

I would like to accord John these files, but unfortunately there is no email address available (or I've not understood how to contact the author of an article at codeproject Confused ).

Anyway, if someone is interested at this piece of code, get in touch with me.

Michael

GeneralRe: Improvement: Multiple Text Fields
min_2_max
17:16 16 Aug '07  
It's just what I need.

Would you please send to me a copy? (loadcom@sina.com)

Thank in advance.

Max
GeneralRe: Improvement: Multiple Text Fields
min_2_max
17:25 16 Aug '07  
And a suggest further, or feature request:

I hope the effect could be similar to that of the splash of Photoshop. The main features includes:

The app could to the data initializing work while the splash is showing, with a prompt text on the splash
dynamically change showing the progress of the initializition. And, preferably, a progress bar showing this
process.

Anybody has any hints?

Thanks

Max
GeneralRe: Improvement: Multiple Text Fields
Michael Stammberger
22:01 16 Aug '07  
Hi!

Yes, it's possible to change the text data while processing something in the background (I use it this way), but a progress bar is not available. Indeed this feature would be nice and if you would implement it, I would be appreciated to get a copy of your improvements...

Best Regards,
Michael

GeneralGreat work
jefflewis
2:59 19 Sep '05  
Much appreciated - looks great.
thanks
J
GeneralSound
sicks
12:22 21 Jul '05  
Thanks Smile

I'v quickly added a function of CSplashScreenEx to have a "Splash-song".

void SetSound(LPCTSTR szPath, HMODULE hMod, DWORD fdwSound);

void CSplashScreenEx::SetSound(LPCTSTR szPath, HMODULE hMod, DWORD fdwSound)
{
if(PathFileExists(szPath))
{
PlaySound(szPath, hMod, fdwSound);
}
}

GeneralSome errors when using in UNICODE project
bitmensch
2:59 15 Jul '05  
nice, but there are some errors when using in a UNICODE project.
GeneralHow to use the class in SDI or MDI project?
sibang jang
17:30 11 Jul '05  
bottom reply compile error.....

Create function error...

please fast reply?:Confused

sdfwefw
GeneralRe: How to use the class in SDI or MDI project?
sibang jang
18:38 11 Jul '05  
use the APP Class in MDI project? Confused

GeneralWhy parent window is needed?
Yogurt
10:47 23 Jun '05  
I've seen some questions and replies about the necessity of the parent window. However, in the environment I'd use CSplashScreenEx, there is simply no parent window yet. (I'd show the splash screen as one of my DLLs is loaded, and hide when the application is loaded and the main window is created and shown.)

Now I've made the following modifications to CSplashScreenEx::Create
BOOL CSplashScreenEx::Create(CWnd *pWndParent,LPCTSTR szText,DWORD dwTimeout,DWORD dwStyle)
{
m_pWndParent = pWndParent;
HWND hParentWnd = NULL;
if (m_pWndParent != NULL)
hParentWnd = m_pWndParent->m_hWnd;
(and got rid of the assert). And at the end of this function:
    if (!CreateEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,"SplashScreenExClass",NULL,WS_POPUP,0,0,0,0,hParentWnd,NULL))
return FALSE;
The only restriction is that CSS_CENTERAPP cannot be used when pWndParent == NULL but that is obvious: if there's no parent window, where to center it?
GeneralRe: Why parent window is needed?
kelvin_gl
18:18 14 Jul '05  
I second this modification. I used it in InitInstance( ) when the main window had not been created.
Anyway the codes are just great, so is the modification.Laugh



My 2 Cents
GeneralRe: Why parent window is needed?
Brad Bruce
11:35 28 Jan '06  
PERFECT!!!!

Thanks for the "fix"


GeneralWindows 2000 SP4
Johnnyfartpants
21:28 30 May '05  
Forgive me if this appears twice - but I posted this comment yesterday and it seems to have vanished this morning!?!

I've been using CSplashScreenEx very successfully for an app that runs under both Windows 2000 and XP. It's a great utility and has never caused any problems. However, a week or two ago I upgraded my copy of Win2K to Service Pack 4.

My splash screen implements a short "fade-in" via the CSS_FADE style (which, in turn, invokes the AW_BLEND property of AnimateWindow() ). This seems to have stopped working since the upgrade to SP4. I just wondered if anyone else had encountered this? The splash screen still displays - but not with the fade-in. Oddly enough, it does fade out though...??? I've tried the original application (SplashScreenExDemo) and that suffers from the same problem.

The same apps (i.e. the very same physical exe files) still work perfectly under Windows XP. Does this ring a bell or make sense to anyone else?
GeneralRe: Windows 2000 SP4
Johnnyfartpants
2:44 28 Jul '05  
Well, it's now about 3 months since my upgrade and CSplashScreenEx continues to have problems with Win2k SP4. I also upgraded my copy of XP to Service Pack 2, since when, I've had intermittent problems with the code crashing and even locking up my entire machine. This is also happening on other peoples' PC's too so I've had to abandon the splash screen for the moment.

However, it would be nice to get to the bottom of this and get it fixed sometime.
GeneralA better CreateRgnFromBitmap function
kintz
1:34 1 Mar '05  
Below is my re-worked code for the CreateRgnFromBitmap method. This fixes the last transparent pixel row issue and also fixes a weird random crash when building regions with certain images.

Anyhow here is the code:

HRGN SplashScreenI::CreateRgnFromBitmap(HBITMAP hBmp, COLORREF color)
{
if (!hBmp) return NULL;

BITMAP bm;
GetObject( hBmp, sizeof(BITMAP), &bm ); // get bitmap attributes
CDC dcBmp;
dcBmp.CreateCompatibleDC(GetDC()); //Creates a memory device context for the bitmap
dcBmp.SelectObject(hBmp); //selects the bitmap in the device context
CArray<CRect,CRect&> Regions;

int nState=-1;
int nStart=0;
bool bIsMask;

// scan the image to find areas of solid regions to draw
for ( int y = 0; y < bm.bmHeight; ++y )
{
nState = -1;
nStart = 0;
for ( int x = 0; x < bm.bmWidth; ++x )
{
// get color
bIsMask=(dcBmp.GetPixel(x,bm.bmHeight-y-1)==color);

switch( nState )
{
case -1: // first pixel
if( bIsMask )
nState = 0;
else
nState = 1;

nStart = 0;
break;

case 1: // solid
if( bIsMask )
{
// create region
Regions.Add( CRect( nStart, bm.bmHeight - y - 1, x, bm.bmHeight - y) );
nState = 0;
}
break;

case 0: // transaparent

if( !bIsMask )
{
// start region
nStart = x;
nState = 1;
}
break;
}
}

// solid last pixel?
if( nStart != x-1 && nState==1)
{
// add region
Regions.Add( CRect( nStart, bm.bmHeight - y - 1, bm.bmWidth, bm.bmHeight - y) );
}
}

dcBmp.DeleteDC(); //release the bitmap


// create region
// Under WinNT the ExtCreateRegion returns NULL (by Fable@aramszu.net)
// HRGN hRgn = ExtCreateRegion( NULL, RDHDR + pRgnData->nCount * sizeof(RECT), (LPRGNDATA)pRgnData );
// ExtCreateRegion replacement {
HRGN hRgn=CreateRectRgn(0, 0, 0, 0);
ASSERT( hRgn!=NULL );

for( int i=0;i<(int)Regions.GetSize();++i )
{
const CRect& rt = Regions[i];
HRGN hr=CreateRectRgn(rt.left, rt.top, rt.right, rt.bottom);
VERIFY(CombineRgn(hRgn, hRgn, hr, RGN_OR)!=ERROR);
if (hr) DeleteObject(hr);
}

ASSERT( hRgn!=NULL );

return hRgn;
}


[edit] fixed angle brackets on template!


Last Updated 2 Oct 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010