How to create splash screen for WinForms application?

In development of my Index.dat File Viewer application, one of the thing that I wanted to do was put together a quick splash screen during start up of the application. I was not looking for a very elaborate splash screen with any fancy features. So I came up with a solution that I will share in this post.

Here are steps that you can follow to create a simple spalsh screen for Winforms application.

Setting up Splash Screen Form

  • Add a new form in your application.
  • On this form drop a Picture control.
  • Size this Picture control to size of the image you would like to show as splash screen
  • Add splash screen image to your project.
  • Set source of Image for Picture control to image that you added to your project.
  • Make sure that you set FormBorderStyle property of this form to None because we do not need to show any title bars or things like that for our splash screen. Also give some appropriate name to your form as well because this name will show up in taskbar. You definitely do not want to look naive if name like Form1 is showing up in taskbar.

    c# splash screen

Displaying Splash Screen

Now that you have set up form for your splash screen, you just need few lines of code to display the splash screen when application load and then hide it as soon as loading is complete.

In your constructor or form Load event handler of your main form, you will need to spawn a thread that will invoke display of splash screen form. For example in my application, I did it from constructor of main form as shown below.


public MainForm()
{
 InitializeComponent();
 _splashThread = new Thread(new ThreadStart(DoSplash));
 _splashThread.Start();
 Initialize();
}

void DoSplash()
{
 _splash = new DatViewerSplash();
 _splash.ShowDialog();
}

Hide Splash Screen

Now that your splash screen is showing when application is loading, at some point after load completes you will need to hide this splash screen as well. It will depend on your application to decide when it thinks that load has completed. For example in my application, I had to do some work in OnLoad event handler and right after that I decided to hide the splash screen. The following code snippet shows how I am hiding splash screen.


private void OnLoad(object sender, EventArgs e)
{
LoadTempInternetFiles();
CloseSplashScreen();
_splashThread.Abort();
}

delegate void CloseSplashScreenCallback();

private void CloseSplashScreen()
{
 if (_splash.InvokeRequired)
 {
  var callBack = new CloseSplashScreenCallback(CloseSplashScreen);
  _splash.Invoke(callBack);
 }
 else
 {
  _splash.Close();
 }
}

You may be wondering why didn't I simply called Close method on the splash screen object in OnLoad. If you did that you will get infamous exception telling you that you can not call any method on this windows object because it was created in another thread. If you look at how the window was shown, we spawned a new thread from main thread of the application. So your splash screen object was invoked from another thread. To take care of this thread context you will need to call Invoke method on that object and provide it a call back in the form of a delegate. In that delegate you can call Close method on form object.

Another question that may come in your mind is calling Abort on thread should take care of destroying or closing splash screen. I also thought that way but it did not seem to work that way. After calling Abort, splash screen was still visible. So doing it the right way is the way to go.

comments powered by Disqus

Blog Tags