Create Splash Screen in WPF

This article shows how to make a splash screen in a WPF application. Look at the following procedure.

Step 1

Create a WPF project and add a new window and name it SplashScreen.

WPF application

windows

Step 2

Let's define a few properties of the Splash Screen so as to ensure it looks different than the usual window (not visible in the Taskbar, no toolbar in the window, the default position is the center of the screen and also a few more properties to provide the appropriate look and feel to the window). The properties are the following:

  1. ResizeMode="NoResize" WindowStyle="None"  
  2. WindowStartupLocation="CenterScreen" Background="Blue" BorderThickness="5" BorderBrush="AliceBlue"  
The output window until now will look as in the following:

output

Step 3

We now apply a label on this screen. This label would be used to show the text.

It appears to be as in the following:

loading

Step 4

Now open the App.xaml.cs, we would write the code in this file that will make sure that when the application starts, the Splash Screen is loaded first and then the MainWindow appears.

Here's the code:
 
Method 1
  1. public partial class App : Application  
  2. {  
  3.     private const int MINIMUM_SPLASH_TIME = 1500; // Miliseconds  
  4.     
  5.     protected override void OnStartup(StartupEventArgs e)  
  6.     {  
  7.         SplashScreen splash = new SplashScreen();  
  8.         splash.Show();  
  9.         // Step 2 - Start a stop watch  
  10.         Stopwatch timer = new Stopwatch();  
  11.         timer.Start();  
  12.   
  13.         // Step 3 - Load your windows but don't show it yet  
  14.         base.OnStartup(e);  
  15.         MainWindow main = new MainWindow();  
  16.         timer.Stop();  
  17.   
  18.         int remainingTimeToShowSplash = MINIMUM_SPLASH_TIME - (int)timer.ElapsedMilliseconds;  
  19.         if (remainingTimeToShowSplash > 0)  
  20.             Thread.Sleep(remainingTimeToShowSplash);  
  21.   
  22.         splash.Close();  
  23.          
  24.     }  
  25.   
  26. }  
In the preceding code, we declare the Minimum time for which the Splash Screen should be visible. Then, we create an object of the Splash Screen.

Then start a stopwatch (you would need to include the System.Diagnostic DLL for this) and during the loading of the main window, we load the Splash Screen.

After that time period is over, the splash screen is automatically closed and the Main Window appears.

There is one more, yet simpler method to get this done. The code for that is as in the following:

Method 2
  1. SplashScreen splash = new SplashScreen();  
  2. splash.Show();  
  3.               
  4. MainWindow main = new MainWindow();  
  5.   
  6.   
  7. for (int i = 0; i < 100; i++)  
  8. {  
  9.     Thread.Sleep(i);  
  10. }  
  11.   
  12. splash.Close();  
  13.   
  14. main.Show();  
In this method, we simply created the object of the Splash Window and displayed it.

Then we made sure that it remains open for a certain amount of time by using the Thread method.

After the loop is over, we simply close the splash screen and start the main window.

The article contains the solution with the attached code.

I hope it helps!

Up Next
    Ebook Download
    View all
    Learn
    View all