Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Easy C# SplashScreen with Minimal Coding

0.00/5 (No votes)
29 Mar 2016 1  
Fast and simple way to display a splashscreen while your first Windows form loads.

Introduction

Many applications show a splashscreen to let the user know that the program is loading and to add some flair to their program. The splashscreen typically shows the application's name, version number as well as copyright information. This approach allows the developer to use any Windows form as the splashscreen. It automatically hides the screen when the first Windows Form of the application finishes loading and is shown. Also it displays the splashscreen on exit of the application. The end result is shown below.

Animated image showing splashscreen in action.

This method does not use a timer. It only shows the splashscreen while the system is loading without stalling (delaying) the loading thread and once it is loaded, the splashscreen is hidden.

Background

The approach is to use the normal startup code with only minor modifications. The splashscreen is just a plain Windows form without any special coding. There is no change to the first form of your project. The only program that is changed is the launch program named Program.cs in your project. Program.cs is a Windows generated program that is only changed when you change your top form of your project.

Using the Code

First, create a new Windows form to be your splashscreen. Set the background image of the form to be your splashscreen image. You can add labels to dynamically hold information like version, etc. Under properties for the form, set the borders to None. Its location to CenterScreen and its size to AutoSize so if you change the image, the form size is still correct. I also include a SayWelcome() and SayGoodbye() methods to show a welcome and goodbye message.

The original code in Program.cs had just a few lines and ended with:

Application.Run(new MainForm);

Comment out that line and add the new splashscreen code. First, create a static instance of the splashscreen as an attribute of the static class Program.

static class Program
{
    // Create an instance of the splashscreen 
    static SplashScreen splashscreen;

Initialize the splashscreen instance in the body of the Main() method after the original Application.Run() line that we commented out. It will give you an error it you try to do it before the calls to Application.EnableVisualStyles() and Application.SetCompatibleTextRenderingDefault(false).

// Initialize and show splashscreen. Say the welcome message. 
splashscreen = new SplashScreen();
splashscreen.Show();
splashscreen.SayWelcome();

Then create an instance of the top windows form. Hook into the shown and closed events. Then do the Application.Run() but this time just use the instance in place of the new MainForm.

// Create an instance of MainForm and hook into shown and closed events.
MainForm mainform = new MainForm();
mainform.Shown += main_Shown;
mainform.FormClosed += main_FormClosed;

Application.Run(mainform); 

For the main_Shown() method, hide the splashscreen.

static void main_Shown(object sender, EventArgs e)
{
    // Hide the splashscreen. 
    splashscreen.Hide();
}

For the main_FormClosed() method, hide the form being closed and show the splashscreen. I added a System.Threading.Thread.Sleep(1000) so the screen shows for a whole second before the application exits.

static void main_FormClosed(object sender, FormClosedEventArgs e)
{
    // Hide the calling form  
    Form form = sender as Form;
    form.Hide();

    // Show the splash screen and say goodbye. 
    splashscreen.Show();
    splashscreen.SayGoodBye();
    System.Threading.Thread.Sleep(1000);   // Pause for a second.
}

That is all there is to it. The comments in the code should answer any further questions you have. Here is the complete Program.cs with the splashscreen code revisions.

// Program.cs
// Modified to display a SplashScreen
//
using System;
using System.Windows.Forms;
namespace WindowsAppWithSplashScreen 
{
    static class Program
    {
        // Create an instance of the splashscreen 
        static SplashScreen splashscreen;

        /// <summary> 
        /// The main entry point for the application. 
        /// </summary> 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new MainForm()); 
            //-------------- End of original lines. --------------// 

            // Initialize and show splashscreen. Say the welcome message. 
            splashscreen = new SplashScreen();
            splashscreen.Show();
            splashscreen.SayWelcome();

            // Create an instance of MainForm and hook into shown and closed events. 
            MainForm main = new MainForm();
            mainform.Shown += main_Shown;
            mainform.FormClosed += main_FormClosed;

            Application.Run(main);
        }

        static void main_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Hide the calling form  
            Form form = sender as Form;
            form.Hide();

            // Show the splash screen and say goodbye. 
            splashscreen.Show();
            splashscreen.SayGoodBye();
            System.Threading.Thread.Sleep(1000);   // Pause for a second.
        }

        static void main_Shown(object sender, EventArgs e)
        {
            // Hide the splashscreen. 
            splashscreen.Hide();
        }
    }
}

Points of Interest

One final note: if you change the initial form name, you may have to manually edit Program.cs again. The SayWelcome() and SayGoodbye() methods are optional and can be omitted. For my application, we use Text-to-speech to have the computer say hello and goodbye for the Welcome and Goodbye. Hopefully, this is simple enough that everyone can add splash to their C# app.

History

  • Version 1.0 is the latest version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here