Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

An Easy SplashScreen

0.00/5 (No votes)
31 Mar 2009 1  
The way to get your splashscreen going in a few lines of code.

Introduction

This little piece of code can provide you with a very nice splash screen! It's easy to do and lightweight, so it fits on every system.

Background

I've been searching a long time to find a proper splashscreen method, but all of those I found seemed to be too short, too complicated, or not even working at all. I decided to find my own sneaky way to get myself a splashscreen, which ended up quite well! You can download my code for Visual Studio 2008, or read the article where I explain how to do it yourself quickly and easily.

Using the Code

First of all, we start with a new project (Windows Forms application). After creating the project, we have a default form, called "Form1". I'll rename this form to "MainForm".

We now change a few properties of MainForm: Set "Opacity" to "0%" and "ShowInTaskbar" to "False". If you want, you can also change "StartPosition" to "CenterScreen".

Now add another form, called "SplashScreen". Just right click your project in the Solution Explorer, go to "Add" and then to "Windows Form...". With this new form, we change a few properties: "BackgroundImage" -> put your splash screen image (choose local resource) in here (with a black background for transparency). "FormBorderStyle" is set to "None", "Opacity" is set to "0%", "ShowInTaskbar" is set to "False", and "TransparancyKey" is set to (in this case) "Black"

Adjust the splashscreen size to get the entire splashscreen in the form.

Now we're going to add the code to the splashscreen: add threading possibilities to the program with:

using System.Threading;

The only functions we need in this form are these:

public SplashScreen()
{
    InitializeComponent();
    System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
}
public void OpacityUP()
{
    for (double i = 0; i <= 100; i++)
    {
        this.Opacity = i / 100;
        Thread.Sleep(20);
    }
}

Not checking for illegal cross thread calls will enable you to control a form from a thread, just the thing we need. The function "OpacityUP" will be the actual thread that's going to run and add a smooth fade in!

Next, we are going to add a timer into MainForm, and make sure it's enabled. After creating the timer, just double click it to automatically create the event code. Now we're editing the code of the MainForm, and we have to add threading support as well, with:

using System.Threading;

Let's add the timer code:

private void ATimer_Tick(object sender, EventArgs e)
{
    ATimer.Enabled = false;
    SplashScreen splash = new SplashScreen();
    splash.Show();
    System.OperatingSystem osInfo = System.Environment.OSVersion;
    if (!(osInfo.Version.Major <= 5))
    {
        Thread th = new Thread(new ThreadStart(splash.OpacityUP));
        th.Start();
        Thread.Sleep(4200);
        th.Abort();
    }
    else
    {
        splash.Opacity = 1;
        Thread.Sleep(4200);
    }
    splash.Close();
    this.ShowInTaskbar = true;
    this.Opacity = 1;
}

A quick explanation:

  1. The timer is disabled directly after starting it with the main form (since this is the only way I can think of a self destructing thread (not entirely true)). The timer needs to run only once after all. Kinda timer abuse!
  2. We create an object called "splash" and open it.
  3. Due to a problem with Windows XP, it is not possible to do the fade in with transparency. It supposedly works well with Vista and later versions. To test this, I get the system information about which version it is. Version 5 is Windows XP or 2000; in that case, we'll just leave the fade in and pop it up instantly.
  4. After doing the splash (when we already break off the thread), the splashscreen will close and suddenly the main form will be visible and show up in the taskbar.

Points of Interest

I didn't learned much really... just trying to use C# and find a sneaky way to create an easy splashscreen :) (I tried over 20 different ideas).

History

Version 1 is the latest version!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here