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.
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
{
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)
.
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
.
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)
{
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)
{
Form form = sender as Form;
form.Hide();
splashscreen.Show();
splashscreen.SayGoodBye();
System.Threading.Thread.Sleep(1000);
}
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.
using System;
using System.Windows.Forms;
namespace WindowsAppWithSplashScreen
{
static class Program
{
static SplashScreen splashscreen;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
splashscreen = new SplashScreen();
splashscreen.Show();
splashscreen.SayWelcome();
MainForm main = new MainForm();
mainform.Shown += main_Shown;
mainform.FormClosed += main_FormClosed;
Application.Run(main);
}
static void main_FormClosed(object sender, FormClosedEventArgs e)
{
Form form = sender as Form;
form.Hide();
splashscreen.Show();
splashscreen.SayGoodBye();
System.Threading.Thread.Sleep(1000);
}
static void main_Shown(object sender, EventArgs e)
{
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.
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).