Click here to Skip to main content
11,568,522 members (47,331 online)
Click here to Skip to main content

Yet Another Splash Screen in C#

, 8 Jul 2009 CPOL 124.5K 19.1K 145
Rate this:
Please Sign up or sign in to vote.
Simple splash screen using C#

Introduction

The article describes creating a simple splash screen in C#. The intent of this article is to create a simple quick splash screen in a separate thread.

Lot of code is available to build fancy looking splash screens with lot of animation. I have not spent time adding animation, etc. I just took a simple picture for splash screen over which a status message is displayed along with the progress bar in marquee mode. A good thing which I learnt was to make the label’s background transparent when overlaying over the picture box control.

Additionally I have created the splash screen in a separate thread and calls for updating the splash screen status are thread safe using BeginInvoke. I have seen people preferring splash screen in a separate thread and some in the main thread. In my opinion, it's better to create it in a separate thread and expose some functions which can be called by the main thread to show/hide/update the splash screen.

Background

The code is simple using threading concepts in C#. The code has been compiled using Visual Studio Express edition 2008 compiled with C# 2.0. One can use these files in Visual Studio 2005 by creating a blank solution and then selecting Windows application in application type.

Using the Code

I have created the following functions for exposing the Splash Screen functionality in the main form. All these functions are static functions, meaning you would not require an instance of the class to be initiated for using it:

  • public static void ShowSplashScreen()
  • public static void CloseSplashScreen()
  • public static void UdpateStatusText(string Text)
  • public static void UdpateStatusTextWithStatus(string Text,TypeOfMessage tom)

I made a static class named SplashScreen which is basically a wrapper around the Splash Screen form. To modify the look and feel of the splash screen, the user can edit SplashScreenForm form and add more functionality to it. The SplashScreenForm exposes functions to update the status text on the form.

The code below is used in the constructor of the MainForm where I initiate the Splash screen and hide the form.

this.Hide(); 
Thread splashthread = new Thread(new ThreadStart(SplashScreen.ShowSplashScreen));
splashthread.IsBackground = true;
splashthread.Start(); 

The code below is used to make the label’s background transparent on a picturebox control. This is used in the SplashScreenForm form.

this.label1.Parent = this.pictureBox1;
this.label1.BackColor = Color.Transparent; 

I made two functions to update the status on the splash screen. I have set the default color for label text to green and the user can call update using the function:

public static void UdpateStatusText(string Text)

I also extended the update function to change the font color based on type of status message.

  • Green for success
  • Yellow for warning
  • Red for error

So I create an enum structure as below for the public static void UdpateStatusTextWithStatus(string Text,TypeOfMessage tom) where I change the color of the label.

public enum TypeOfMessage
{
    Success,
    Warning,
    Error,
} 

Another important thing to keep in mind is that the user should not be able to close the application when splash screen is active. So I create a flag (bool CloseSplashScreenFlag) which is checked whenever the splash screen is sent a message to close by trapping FormClosing event. Hence a simple call like this.close() would not close the form. One would require public static void CloseSplashScreen() function to be called which sets the flag and then close the splashscreen.  

The code below is for handling the FormClosing event where if the flag is set to false, it bypasses the close event.

private void SplashForm_FormClosing(object sender, FormClosingEventArgs e)
{
   if (CloseSplashScreenFlag == false)
   e.Cancel = true;
}

Points of Interest

  1. Making label’s background transparent over a picture box control
  2. Handing close event on the splash screen
  3. Exposing static functions to call splashscreen

History

  • 6th July, 2009: First release
  • 7th July, 2009: Updated demo, source code and screenshot

License

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

Share

About the Author

kavitesh
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1115967416-Oct-14 16:05
memberMember 1115967416-Oct-14 16:05 
GeneralMy vote of 5 Pin
Abinash Bishoyi13-Apr-13 13:27
memberAbinash Bishoyi13-Apr-13 13:27 
QuestionThe following code line in MainForm will be needless? Pin
BlackMilan29-Oct-12 1:25
memberBlackMilan29-Oct-12 1:25 
QuestionProblem on XP SP3 Pin
Peteringermany11-Aug-12 5:52
memberPeteringermany11-Aug-12 5:52 
AnswerRe: Problem on XP SP3 Pin
Peteringermany11-Aug-12 6:21
memberPeteringermany11-Aug-12 6:21 
BugPossible threading problem Pin
Muir10-Aug-12 5:08
memberMuir10-Aug-12 5:08 
GeneralRe: Possible threading problem Pin
Peteringermany11-Aug-12 5:51
memberPeteringermany11-Aug-12 5:51 
GeneralRe: Possible threading problem Pin
Muir11-Aug-12 7:20
memberMuir11-Aug-12 7:20 
QuestionMy vote of 5 too Pin
Peteringermany28-Jul-12 6:41
memberPeteringermany28-Jul-12 6:41 
GeneralMy vote of 5 Pin
AJMAL SHAHZAD28-Jun-12 2:35
memberAJMAL SHAHZAD28-Jun-12 2:35 
GeneralMy vote of 5 Pin
Md. Marufuzzaman2-Jun-12 23:45
mentorMd. Marufuzzaman2-Jun-12 23:45 
GeneralExactly what I needed. That must be a 5'er Pin
ZeedijkMike10-Apr-12 8:43
memberZeedijkMike10-Apr-12 8:43 
Questiongood Pin
flyicing22-Mar-12 15:07
memberflyicing22-Mar-12 15:07 
QuestionMdiParent Pin
Member 824571817-Jan-12 18:58
memberMember 824571817-Jan-12 18:58 
AnswerRe: MdiParent Pin
kavitesh17-Jan-12 19:58
memberkavitesh17-Jan-12 19:58 
GeneralReally good code example Pin
John Mealing12-Jan-12 8:13
memberJohn Mealing12-Jan-12 8:13 
QuestionGood efforts Pin
Uma Shankar Patel6-Jan-12 22:54
memberUma Shankar Patel6-Jan-12 22:54 
GeneralMy vote of 5 Pin
Uma Shankar Patel6-Jan-12 22:54
memberUma Shankar Patel6-Jan-12 22:54 
GeneralMy vote of 4 Pin
Member 808650415-Jul-11 14:09
memberMember 808650415-Jul-11 14:09 
GeneralRe: My vote of 4 Pin
kavitesh25-Jul-11 9:51
memberkavitesh25-Jul-11 9:51 
GeneralMy Vote of 5 Pin
RaviRanjankr17-May-11 1:10
memberRaviRanjankr17-May-11 1:10 
GeneralRe: My Vote of 5 Pin
kavitesh25-Jul-11 9:51
memberkavitesh25-Jul-11 9:51 
GeneralMy vote of 4 Pin
Sharique uddin Ahmed Farooqui18-Jan-11 0:42
memberSharique uddin Ahmed Farooqui18-Jan-11 0:42 
GeneralRe: My vote of 4 Pin
kavitesh25-Jul-11 9:51
memberkavitesh25-Jul-11 9:51 
GeneralA wierd issue Pin
Sharique uddin Ahmed Farooqui18-Jan-11 0:41
memberSharique uddin Ahmed Farooqui18-Jan-11 0:41 
GeneralMy vote of 5 Pin
Sharique uddin Ahmed Farooqui14-Dec-10 1:43
memberSharique uddin Ahmed Farooqui14-Dec-10 1:43 
GeneralVary easy to implement Pin
dev4ever9-Dec-10 22:33
memberdev4ever9-Dec-10 22:33 
GeneralMy vote of 5 Pin
dev4ever9-Dec-10 22:32
memberdev4ever9-Dec-10 22:32 
GeneralMy vote of 5 Pin
_clem30-Nov-10 7:54
member_clem30-Nov-10 7:54 
GeneralMy vote of 10 Pin
_clem30-Nov-10 7:53
member_clem30-Nov-10 7:53 
GeneralRe: My vote of 10 Pin
kavitesh30-Nov-10 10:03
memberkavitesh30-Nov-10 10:03 
GeneralMy vote of 5 Pin
DonaldKnuth9-Nov-10 15:52
memberDonaldKnuth9-Nov-10 15:52 
GeneralMy vote of 4 Pin
AlexB475-Nov-10 6:19
memberAlexB475-Nov-10 6:19 
Generalnice & easy Pin
bguyb20-Jun-10 21:08
memberbguyb20-Jun-10 21:08 
GeneralSupplied project code didn't work in Visual Studio 2005 Pin
DariusRoch1-May-10 5:14
memberDariusRoch1-May-10 5:14 
Generalneed help Pin
AshishGupta030518-Mar-10 5:44
memberAshishGupta030518-Mar-10 5:44 
GeneralRe: need help Pin
kavitesh19-Mar-10 6:31
memberkavitesh19-Mar-10 6:31 
GeneralRe: need help Pin
AshishGupta030519-Mar-10 20:51
memberAshishGupta030519-Mar-10 20:51 
GeneralRe: need help Pin
kavitesh29-Mar-10 20:04
memberkavitesh29-Mar-10 20:04 
GeneralNice one.. !. Pin
jafarmlp19-Oct-09 3:27
memberjafarmlp19-Oct-09 3:27 
GeneralMy vote of 1 Pin
Dima Pasko7-Sep-09 11:01
memberDima Pasko7-Sep-09 11:01 
GeneralRe: My vote of 1 Pin
Cyber_8626-Oct-09 11:02
memberCyber_8626-Oct-09 11:02 
GeneralRe: My vote of 1 Pin
kavitesh27-Oct-09 7:20
memberkavitesh27-Oct-09 7:20 
GeneralRe: My vote of 1 [modified] Pin
kavitesh28-Oct-09 4:01
memberkavitesh28-Oct-09 4:01 
GeneralGood! Pin
Gianni Gardini18-Aug-09 0:54
memberGianni Gardini18-Aug-09 0:54 
GeneralMore clarification Pin
Md. Marufuzzaman6-Jul-09 19:38
memberMd. Marufuzzaman6-Jul-09 19:38 

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 | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 8 Jul 2009
Article Copyright 2009 by kavitesh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid