How to Create a Splash Screen in C#
By FoxLearn 11/29/2024 9:48:48 AM 22.35K
This article will guide you through the steps of adding a splash screen to your C# Windows Forms application using threading.
How to create a Splash Screen in C#
Creating a simple loading screen c# by using the SplashScreen library. So, to play the demo you should install the SplashScreen.dll by right-clicking on your project, then selecting Manage NuGet Packages -> Search 'splash screen' -> Install
C# Splash Screen while loading
The SplashScreen library is an open-source tool designed to help you easily create a simple splash screen in a C# Windows Forms application. It provides a straightforward way to display a splash screen while your application is loading, allowing you to customize the appearance and duration. This library simplifies the process of adding a splash screen, making it easier to manage initialization tasks in the background while presenting a polished user interface during startup.
In the main form (frmMain
), we’ll modify the constructor to start a new thread that runs the splash screen.
At the same time, we’ll load data or initialize other resources on the main thread. Once the initialization is complete, we’ll close the splash screen.
//splash screen thread c# public frmMain() { // Start a new thread to show the splash screen Thread t = new Thread(new ThreadStart(Splash)); t.Start(); // Initialize components (this could take some time) InitializeComponent(); // Simulating some data loading/initialization string str = string.Empty; for (int i = 0; i < 100000; i++) { str += i.ToString();// Demo data loading process } // Once loading is complete, abort the splash screen thread t.Abort(); }
In the frmMain
constructor, we create a new thread (Thread t = new Thread(new ThreadStart(Splash));
) to run the splash screen form (Splash()
method). The thread is then started using t.Start()
.
While the splash screen is being displayed, the main form initializes its components (via InitializeComponent()
). Once the initialization is complete, the splash screen thread is aborted using t.Abort()
.
Add your handle code below "loading data" line, to simulate data processing i'm using a loop to delay the process.
Next, Create the Splash method allows you to open the flash screen form as the following c# code.
// Method to show the splash screen void Splash() { // Create and configure the splash screen form SplashScreen.SplashForm frm = new SplashScreen.SplashForm(); frm.Font = new Font("Time New Romans", 7); frm.AppName = "Demo"; frm.Icon = Properties.Resources.app;//Load icon from resource frm.ShowIcon = true; frm.ShowInTaskbar = true; // Run the splash form in a separate thread Application.Run(frm); }
The Splash()
method creates and configures a SplashScreen.SplashForm
. We set properties like the application name (AppName
), the font, and the icon for the splash screen. Application.Run(frm)
runs the splash form in a separate thread.
In this tutorial, we covered how to start a splash screen in a new thread, how to simulate data loading, and how to handle the display and closing of the splash screen.
You can also add icon to the splash screen form or change the font type or font size to create the loading screen like Microsoft Office loading screen above.
VIDEO TUTORIAL