A good splash screen will:
- Run on a separate thread
- Fade in as it appears, and fade out as it disappears
- Display a running status message that is updated using a static method
- Display and update a predictive self-calibrating owner-drawn smooth-gradient progress bar
- Display the number of seconds remaining before load is complete
Start out by creating a Windows Forms project. Name it SplashScreen. Rename Form1.cs to SplashScreen.cs.
Because the splash screen will only need a single instance, you can simplify your code by using static methods to access it. By just referencing the SplashScreen project, a component can launch, update or close the splash screen without needing an object reference. Add the following code to SplashScreen.cs:
static SplashScreen ms_frmSplash = null;
// A static entry point to launch SplashScreen.
static public void ShowForm()
{
ms_frmSplash = new SplashScreen();
Application.Run(ms_frmSplash);
}
// A static method to close the SplashScreen
static public void CloseForm()
{
ms_frmSplash.Close();
}
A splash screen displays information about your application while it
is loading and initializing its components. If you are going to display
any dynamic information during that time, you should put it on a
separate thread to prevent it from freezing when initialization is
hogging the main thread.Start by using the Threading namespace:
using System.Threading;
Declare a static variable to hold the thread:static Thread ms_oThread = null;
Now add a method to create and launch the splash screen on its own
thread. Wait before returning to ensure that the static methods aren't
called before the form exists: static public void ShowSplashScreen()
{
// Make sure it is only launched once.
if( ms_frmSplash != null )
return;
ms_oThread = new Thread( new ThreadStart(SplashScreen.ShowForm));
ms_oThread.IsBackground = true;
ms_oThread.SetApartmentState(ApartmentState.STA);
ms_oThread.Start();
while (ms_frmSplash == null || ms_frmSplash.IsHandleCreated == false)
{
System.Threading.Thread.Sleep(TIMER_INTERVAL);
}
}Now ShowForm() can be made private, since the form will now be shown using ShowSplashScreen().
// A static entry point to launch SplashScreen.
static private void ShowForm()
Download source and demo - 34.5 KB
No comments:
Post a Comment