Creating a splash screen
To create a splash screen simply add a Windows Form to the project. Open the form in designer mode, right click on it and
open its properties. Scroll down to the Appearance properties of the form and set the FormBorderStyle
property
to none. Next, scroll down to Window Style properties, and set the ShowInTaskbar
property to false
.
Now choose an image that you’d like to use as your splash screen and add it as the background image of the form.
I usually add two labels to my splash screens and they are as follows:
1. Application Title
This label will show the application title on the splash screen. We can set its value on the Load
event of
the splash screen. The best way to do this is to get the application title from the assembly information using:
My.Application.Info.Title
But there is a good chance that the application title is empty, unless we specify it beforehand. To take care of this,
we can use the following check:
My.Application.Info.Title <> ""
If its true then use this value else we can get the file name of the application without the extension by calling the following
method:
System.IO.Path.GetFileNameWithoutExtension()
Parameter: My.Application.Info.AssemblyName
2. Version
Another thing a user might want to see on a splash screen is the version information. I am using the String.Format()
function as specified in the default splash screen template. I will not explain
this function here but will recommend using MSDN help on String.Format()
for better understanding.
The next thing to do is to have the splash screen show up before any other form in the application. The fastest and easiest
way is to use the Project Designer’s splash screen property (Figure 4). But the problem with this is that it is hard
to control how long the splash screen stays opened, when it loses focus, how it appears, and to add additional functionality
to it.
My splash screens
I'll jump directly to the splash screen that I am using and how I am using it. The splash screen is shown in Figure 5 .
As my application’s startup form is myMainForm, I have added the following code to its OnLoad
event:
mySplashScreen.ShowDialog()
Confused!!! I know. First I was using the following syntax:
Dim tempForm As New mySplashScreen
tempForm.ShowDialog()
Why did I change it? Why did I not create an object of mySplashScreen
in the later version? I’ll answer
these questions, but first I need to explain the functionality of the splash screen. For now consider that I am using the
splash screen version 0.01.
Let’s add the following text to mySplahScreen
's onLoad
event call.
If My.Application.Info.Title <> "" Then
ApplicationTitle.Text = My.Application.Info.Title
Else
ApplicationTitle.Text =
System.IO.Path.GetFileNameWithoutExtension(_
My.Application.Info.AssemblyName)
End If
Version.Text = System.String.Format(Version.Text, _
My.Application.Info.Version.Major, _
My.Application.Info.Version.Minor)
Copyright.Text = My.Application.Info.Copyright
As you can guess, this is the code from the default splash screen template. What it does is set the values for the labels
on the splash screen.
Next add a timer control to the splash screen. We will initialize this control in the same onLoad
event call
of the mySplahScreen
. Let’s name our timer control 'timerSplash
'. I am also declaring the
following shared variables:
Public Shared
opacityRate As Double = 0.0
Public Shared maximizeRate As Boolean = True
Public Shared minimizeRate As
Boolean = False
Public Shared killApplication As
Boolean = False
Now let’s add code to our OnLoad
event procedure, right after setting the Copyright text:
Me.Opacity = 0.0
timerSplash.Interval = 50
timerSplash.Enabled = True
timerSplash.Start()
In the first line, I am setting the form’s opacity, i.e. the splash screen’s opacity equal to zero… Thus
in the initial stage it will not be visible. Then I am setting the timerSplash
’s interval equal to 50,
making it enabled, and then telling it to start execution.
With this ends our splash screen’s onLoad
procedure. What remains is the timer tick event handler, i.e.
the function which handles the event timerSplash.Tick
.
The timerSplash_Tick
function will get called after every interval. An interval of 1000 is equal to 1 second.
The interval we set is a small fraction of that second. Let’s add the code for this event handler:
CollapseIf opacityRate >= 1.0
Then
opacityRate = opacityRate + 1.0
If opacityRate >= 20.0 Then
opacityRate = 0.99
Me.Opacity = opacityRate
End If
ElseIf maximizeRate Then
opacityRate = opacityRate + 0.025
Me.Opacity = opacityRate
If opacityRate >= 1.0 Then
maximizeRate = False
minimizeRate = True
End If
ElseIf minimizeRate Then
opacityRate = opacityRate - 0.025
If opacityRate < 0 Then
opacityRate = 0
End If
Me.Opacity = opacityRate
If Opacity <= 0.0 Then
minimizeRate = False
maximizeRate = False
End If
Else
timerSplash.Stop()
timerSplash.Enabled = False
timerSplash.Dispose()
Me.Close()
End If
What the above code does is: when the splash screen is called, it materializes into view stays solid for a second and then
materializes back. During initialization I am setting four variables. Out of which three; opacityRate
, minimizeRate
,
and maximizeRate
, are used in this splashTimer_Tick
event handler.
By default, on initialization, the minimizeRate
is set to false
,
the maximizeRate
is set to true
, and the opacityRate
is set to 0.0. Furthermore, the opacity property of the form/splash-screen is also set to zero.
One can easily grasp how the splashTimer_Tick
function works, so I won’t be going into details of that.
What is important to note here is this: After the first 10 ticks, i.e. the first ½ second, the form achieves maximum
opacity, after the next 20 ticks (1 second) the minimizeRate
is set to true
so that the form loses opacity in the next 10 ticks (½ second). The splash screen is thus displayed for a total of 2 seconds.