Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / Windows Forms
Article

Splash Screens - The Easy Way

Rate me:
Please Sign up or sign in to vote.
1.59/5 (14 votes)
31 Dec 2008CPOL2 min read 25.2K   484   19   3
Create splash screens with just a small bit of programming experience.

Introduction

I was reading some articles on creating splash screens, and they all either required long codes, or had to load the application before the splash screen appeared. Therefore, I made my own method of creating a splash screen. Since I'm only a high school student, I lack programming experience, and this article should be easy to follow through. This is also my first article, so some parts might not be as good :).

Using the code

First, create a new solution with a Windows form. Add another form to the project called "Splash Screen.cs". Now, go to Program.cs (or where the main entry point is) and add:

C#
// Change the starting form from 
Application.Run(new Form1());
 
// To
Application.Run(new Splash_Screen());

Go to Splash Screen.cs in the Designer, set the FormBorderStyle to None, Topmost to true, StartPosition to CenterScreen, and ShowInTaskBar to false. Now, add a PictureBox. Set the SizeMode to StretchImage, and the Image to the screen you want. Set Dock to Fill, and resize the form at will.

Next, add the components to the splash screen. Add a FileSystemWatcher and two Timers. Name one timer_run and the other timer. For the watcher, add a Deleted event, and add Tick events for both timers. At the same time, add a Load event for the Form.

Switch to the code. After the call to InitializeComponent(), add:

C#
watcher.Path = Application.StartupPath;

Under the Load event, add:

C#
try
{
    // Create a .tmp file so it can be deleted when Form1 is done
    if (!System.IO.File.Exists(Application.StartupPath + "\\status.tmp"))
    {
        using (System.IO.StreamWriter sw = System.IO.File.CreateText(
               Application.StartupPath + "\\status.tmp"))
        {
        }
    }
}
catch
{
    // Hide this in case something goes wrong
    this.Hide();
}
// Run Form1 on a seperate thread
timer_run.Enabled = true;

This will create a file which the watcher will keep track of. In case something goes wrong, it will hide the splash screen. At the end, it enables timer_run to start the form.

Under timer_run_Tick, add the following code:

C#
timer_run.Enabled = false;
Form1 form1 = new Form1();
form1.Show();

This will run Form1.

Under watcher_Deleted, add:

C#
timer.Enabled = true;

This will close the form when Form1 has loaded.

Create a global variable called transparency:

C#
private double transparency = 1;

Under timer_Tick, add:

C#
// Go down on opacity
if (transparency > 0)
{
      this.Opacity = transparency;
      transparency -= 0.01;
}
else
{
      timer.Enabled = false;
      this.Hide();
      this.dispose(false);
}

This will make the splash screen fade away when loaded.

Switch to Form1 code view. Before InitializeComponent(), add some redundant code to slow it down and allow time for the splash screen.

C#
// Add some redundant code
int number = 0;
for (int i = 0; i < 2147483647; i++)
{
    number++;
}

Under Form1_Load, add:

C#
// Delete the file to raise event on splash screen
if (System.IO.File.Exists(Application.StartupPath + "\\status.tmp"))
{
    System.IO.File.Delete(Application.StartupPath + "\\status.tmp");
}

This will delete the file and raise the event on the splash screen.

Finally, under Form_Closed, add:

C#
Application.Exit();

Since this.Hide() was used earlier, Application.Exit() is needed, or else the splash screen will still be running.

That's it! Easy. isn't it :P?

History

  • June 8, 2008: First release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Gmust19-Jul-10 1:24
Gmust19-Jul-10 1:24 
GeneralGood attempt but.. Pin
N a v a n e e t h8-Jun-08 16:12
N a v a n e e t h8-Jun-08 16:12 
GeneralRe: Good attempt but.. Pin
That Asian Guy8-Jun-08 18:12
That Asian Guy8-Jun-08 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.