Windows Forms application with Splash Screen

Windows Forms application with Splash Screen

Today I will describe how to add simple yet effective splash screen functionality using Visual Basic assembly goodness. To show exactly how to do that I will create a simple project describing step by step what you have to do to create splash screen for your win forms application.
First of all, lets create a simple windows forms application, open up Visual Studio and select File -> New -> Project.

screen1

Select ‘Windows Forms Application’, set the name for your project and click ‘OK’.
This creates simple windows forms application which you can start up by clicking ‘F5’.

Lets say that my Form1.cs is the main form of the application I want to start up. Before launching Form1 I want to create a splash screen to load some additional resources, connect to the database or remote server. The project structure for now should look like this:

screen2

Now, when we have our application created and ready to be launched we create a splash screen for it. We create the splash screen itself first and than modify a bit the code to launch our splash before the main application starts.
Our splash itself would be another form which we modify a bit to look like, well, like a splash screen :).
Lets add another form to our project:

screen3

and name it ‘SplashForm.cs’. This form will be launched until our main form loads. To make it look like a proper splash screen we have to find the property named ‘FormBorderStyle’ and set it to ‘None’, as well as set the ‘StartPostion’ to ‘CenterScreen’.
You can prettify the splash form by adding picture boxes, labels etc to add your custom graphics etc. I’m going to add a simple label and animated gif loader to my form. You can use animated gifs generated on this website which I found very cool.

screen4

Ok, our splash screen is ready, now we need to modify the code which starts our application to show the splash screen and destroy it when the main form is ready.
Originally, when you create the windows forms type of project a simple class called ‘Program’ gets created. You can find the source for this class inside ‘Program.cs’ file. It should look somthing like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TestFormApplication
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

In order to use our splash form created earlier correctly we need to modify this code a bit. First, add a reference to Microsoft.VisualBasic assembly found under .NET tab. Go to you project references and find the following:
screen5
Next, add the folowing piece of code among other ‘using’ statements on top of the ‘Program’ class file:

using Microsoft.VisualBasic;
using Microsoft.VisualBasic.ApplicationServices;

Once this is done you are ready to use the VB goodness. Add the following code below the Program class, inside the same namespace as the Program class itself:

class SplashApp : WindowsFormsApplicationBase
        {
            protected override void OnCreateSplashScreen()
            {
                this.SplashScreen = new SplashForm();
            }
            protected override void OnCreateMainForm()
            {
                //Connect to db, remote server or anything you like in here
                System.Threading.Thread.Sleep(2000);
                //create your main form and the SplashForm will close here automatically
                this.MainForm = new Form1();
            }
        }

You have to modify the main method a bit as well, use the main with arguments option and start you application using our newly created SplashApp class, the full source code for the Program.cs will look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.ApplicationServices;

namespace TestFormApplication
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            new SplashApp().Run(args);
        }
        class SplashApp : WindowsFormsApplicationBase
        {
            protected override void OnCreateSplashScreen()
            {
                this.SplashScreen = new SplashForm();
            }
            protected override void OnCreateMainForm()
            {
                //Connect to db, remote server or anything you like in here
                System.Threading.Thread.Sleep(2000);
                //create your main form and the SplashForm will close here automatically
                this.MainForm = new Form1();
            }
        }
    }
}

You can download the whole VS 2008 solution file here.

Leave a Reply