Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C#
Tip/Trick

Simple Easy C# SplashScreen with Minimal Coding

Rate me:
Please Sign up or sign in to vote.
4.76/5 (24 votes)
29 Mar 2016CPOL3 min read 50.2K   37   6
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:

C#
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.

C#
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).

C#
// 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.

C#
// 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.

C#
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.

C#
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.

C#
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead The Ohio State University
United States United States
I have been developing code in various languages for a long, long time in a plethora of languages. Currently I have been working in C# with Microsoft's Speech Technologies for Text-to-speech (TTS) and Automatic speech recognition (ASR).

Comments and Discussions

 
Questionthanks this is very helpfull for me Pin
Zack Mathew22-May-20 23:42
Zack Mathew22-May-20 23:42 
BugTried it mine don't show content of splash screen Pin
AndyHo30-Mar-16 5:43
professionalAndyHo30-Mar-16 5:43 
GeneralRe: Tried it mine don't show content of splash screen Pin
Humberto Roche30-Mar-16 7:03
Humberto Roche30-Mar-16 7:03 
QuestionQuestion Pin
Member 1115988129-Mar-16 3:49
Member 1115988129-Mar-16 3:49 
AnswerRe: Question Pin
Bob Stoom31-Mar-16 2:37
professionalBob Stoom31-Mar-16 2:37 
GeneralMy vote of 5 Pin
Farhad Reza3-Nov-15 18:53
Farhad Reza3-Nov-15 18:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.