Click here to Skip to main content
15,886,919 members
Articles / Desktop Programming / Win32

An Easy SplashScreen

Rate me:
Please Sign up or sign in to vote.
2.00/5 (3 votes)
31 Mar 2009CPOL3 min read 30.9K   590   17   8
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:

C#
using System.Threading;

The only functions we need in this form are these:

C#
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:

C#
using System.Threading;

Let's add the timer code:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Netherlands Netherlands
I'm a student computer engineering studying at the university of applied sciences Fontys in Eindhoven - the Netherlands.

Comments and Discussions

 
GeneralMy vote of 3 Pin
hulu152819-Oct-13 7:41
hulu152819-Oct-13 7:41 
GeneralMy vote of 1 Pin
Luc Pattyn31-Mar-09 5:53
sitebuilderLuc Pattyn31-Mar-09 5:53 
GeneralRe: My vote of 1 Pin
f r i s c h31-Mar-09 5:58
f r i s c h31-Mar-09 5:58 
GeneralRe: My vote of 1 Pin
wpte31-Mar-09 7:17
wpte31-Mar-09 7:17 
GeneralRe: My vote of 1 Pin
f r i s c h31-Mar-09 7:30
f r i s c h31-Mar-09 7:30 
GeneralRe: My vote of 1 Pin
wpte31-Mar-09 7:48
wpte31-Mar-09 7:48 
QuestionRe: My vote of 1 Pin
wpte31-Mar-09 7:26
wpte31-Mar-09 7:26 
AnswerRe: My vote of 1 Pin
Luc Pattyn31-Mar-09 16:22
sitebuilderLuc Pattyn31-Mar-09 16:22 

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.