How to build splash screen in windows forms application?
First, create your splash screen as a borderless, immovable form with your image on it, set to initially display at the center of the screen, colored the way you want. All of this can be set from within the designer; specifically, you want to:
- Set the form's ControlBox, MaximizeBox, MinimizeBox and ShowIcon properties to "False"
- Set the StartPosition property to "CenterScreen"
- Set the FormBorderStyle property to "None"
- Set the form's MinimumSize and MaximumSize to be the same as its initial Size.
Then, you need to decide where to show it and where to dismiss it. These two tasks need to occur on opposite sides of the main startup logic of your program. This could be in your application's main() routine, or possibly in your main application form's Load handler; wherever you're creating large expensive objects, reading settings from the hard drive, and generally taking a long time to do stuff behind the scenes before the main application screen displays.
Then, all you have to do is create an instance of your form, Show() it, and keep a reference to it while you do your startup initialization. Once your main form has loaded, Close() it.
If your splash screen will have an animated image on it, the window will need to be "double-buffered" as well, and you will need to be absolutely sure that all initialization logic happens outside the GUI thread (meaning you cannot have your main loading logic in the mainform's Load handler; you'll have to create a BackgroundWorker or some other threaded routine.
Windows Forms Splash Screen - Show a form while loading main form
There are different ways of creating splash screens:
You can rely on the splash screen feature of
WindowsFormsApplicationBase
You can show implement the feature yourself by showing a form on a different UI thread and hiding it after the main from loaded successfully.
In this post I'll show an example of both solutions.
Note: Those who are looking for showing a loading window or a loading
gif animation during loading of data, can take a look at this post: Show Loading animation during loading data in other thread
Option 1 - Use WindowsFormsApplicationBase Splash Screen feature
- Add a reference of
Microsoft.VisualBasic.dll
to your project. - Create a
MyApplication
class by deriving fromWindowsFormsApplicationBase
- override
OnCreateMainForm
and assign the from that you want to be the startup form toMainForm
property. Override
OnCreateSplashScreen
and assign the form that you want to show as splash screen toSplashScreen
property.In your
Main
method, create an instance ofMyApplication
and call itsRun
method.
Example
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
var app = new MyApplication();
app.Run(Environment.GetCommandLineArgs());
}
}
public class MyApplication : WindowsFormsApplicationBase
{
protected override void OnCreateMainForm()
{
MainForm = new YourMainForm();
}
protected override void OnCreateSplashScreen()
{
SplashScreen = new YourSplashForm();
}
}
Option 2 - Implement the feature using a different UI thread
You can implement the feature yourself by showing the splash screen in a different UI thread. To do so, you can subscribe to Load
event of the main form in Program
class, and show and close your splash screen there.
Example
using System;
using System.Threading;
using System.Windows.Forms;
static class Program
{
static Form SplashScreen;
static Form MainForm;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Show Splash Form
SplashScreen = new Form();
var splashThread = new Thread(new ThreadStart(
() => Application.Run(SplashScreen)));
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();
//Create and Show Main Form
MainForm = new Form8();
MainForm.Load += MainForm_LoadCompleted;
Application.Run(MainForm);
}
private static void MainForm_LoadCompleted(object sender, EventArgs e)
{
if (SplashScreen != null && !SplashScreen.Disposing && !SplashScreen.IsDisposed)
SplashScreen.Invoke(new Action(() => SplashScreen.Close()));
MainForm.TopMost = true;
MainForm.Activate();
MainForm.TopMost = false;
}
}
Note: To show a smooth edge custom shaped splash screen take a look at
this post: Windows Forms Transparent Background
Image.
Splash screen and main window
If you want to show splash screen while loading other controls on the Main form, you can use splash screen waiting . You can refer this link
In my application I use Devexpress control SplashScreen Manager.
private void FormMain_Load(object sender, EventArgs e)
{
sphManager.ShowWaitForm();
//Place your code which you need to execute while Splash screen is active
GenerateNavigationView();
this.WindowState = FormWindowState.Maximized;
//Once you finish all your requirement, close the Splash screen
sphManager.CloseWaitForm();
}
How do I build a splash screen into a Windows Forms application without setting timers etc?
using Microsoft.VisualBasic.ApplicationServices;
public class Startup : WindowsFormsApplicationBase
{
protected override void OnCreateSplashScreen()
{
SplashScreen = new SplashForm();
}
protected override void OnCreateMainForm()
{
MainForm = new MyMainForm();
}
}
static class Program
{
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Startup().Run(args);
}
}
Related Topics
How to Spawn a Process and Capture Its Stdout in .Net
Should I Avoid 'Async Void' Event Handlers
Replace Line Breaks in a String C#
What's the Strangest Corner Case You'Ve Seen in C# or .Net
Httpclient.Getasync(...) Never Returns When Using Await/Async
Get Installed Applications in a System
Conditional Operator Cannot Cast Implicitly
List≪T≫ or Ilist≪T≫
When Should the Volatile Keyword Be Used in C#
Mssql Error 'The Underlying Provider Failed on Open'
Ca2202, How to Solve This Case
Odbcconnection Returning Chinese Characters as ""
Cast Generic≪Derived≫ to Generic≪Base≫