Creating splash screen for WinForm application

A professional WinForm application should have the following short trick: an introduction screen for a few seconds. Some may use splash screen only for visual entertainment, others may load important data while splash screen is visible. This screen should tell some information about the starting application so users can tune themselves a bit to the software and the work to do.

First we should create a new Project and select Windows Forms Application. The automatically created form (Form1) should be our main form, so let’s add a new one by right clicking on the project name and selecting Add… Select Windows Form… from the context menu, give the name Splash and click OK.

This newly created form should have some property modifications done before coding. Select the form and on its Properties page set these:

  • select an image that you want to show to the users. Check its size and set the form size to this value. I suggest you not to use greater than 640*480 for the form.
  • BackgroundImage: browse for it
  • BackgroundImageLayout: Stretch is fine the image is greater than the splash screen form
  • DoubleBuffered: True – this is important so you won’t see any image flickering while it fades in and out
  • FormBorderStyle: None – we want to see only an image without borders and frames
  • Opacity: 0% – it’ll fade in first, so now we don’t want to see it
  • ShowInTaskbar: False
  • StartPosition: CenterScreen
  • TopMost: True – so it’ll overlay everything on the desktop

Now we have to add 3 Timer component to the form. Just drag’em from the Toolbox and drop them onto the form. The first name is timer1, the second name is INFade and the third name is OUTFade. Set some properties for these components too:

  • timer1
    • Enabled: False
    • Interval: 50
  • INFade
    • Enabled: True (so this going to be the starting timer)
    • Interval: 1
  • OUTFade
    • Enabled: False
    • Interval: 1

Create the Tick event for each timer on the Events panel by double clicking into the empty event box. The auto-generated event names are these: timer1_Tick, INFade_Tick, OUTFade_Tick

Now let’s create the Splash form code.

First add a class level variable:

private int count = 20;

As we start with the INFade timer, let’s create its Tick event. Remember: our form starting opacity value was set to 0.

We’re starting to fade-in our form, so increase the opacity to 100% (value 1).

private void INFade_Tick(object sender, EventArgs e)
        {
            if (this.Opacity == 1)
            {
                INFade.Enabled = false;
                timer1.Enabled = true;
                return;
            }
            this.Opacity += 0.01;
        }

Now we should keep the splash screen visible for a while before we fade it out. Our focus is on timer1.

private void timer1_Tick(object sender, EventArgs e)
        {
            if (count == 0)
            {
                timer1.Enabled = false;
                OUTFade.Enabled = true;
                return;
            }
            count -= 1;
        }

And finally we’re fading out:

private void OUTFade_Tick(object sender, EventArgs e)
        {
            if (this.Opacity == 0)
            {
                OUTFade.Enabled = false;
                this.Close();
                return;
            }
            this.Opacity -= 0.01;
        }

We are almost done, but our program will start with the Form1 default form (as can be seen in Program.cs file). This is ok, so let’s call our newly created Splash form from our Form1 before anything would happen there. So place this few lines before the InitializeComponent() method of Form1 constructor:

Snippet

public partial class Form1 : Form
    {
        public Form1()
        {
            using (Splash splashScreen = new Splash())
            {
                splashScreen.ShowDialog();
            }
 
            InitializeComponent();
        }
    }

That’s all. Memory freed, Form1 started.