This article shows how to implement a simple splash screen in Windows Forms.
Many Websites display a preloader splash screen before taking you to the actual
content or page of the site. In the same manner, a desktop Windows Forms
application can have a splash screen too that appears for some time before the
actual form is displayed.
In this article, we will use a round-robin image display as a splash. This means
that a series of images will keep appearing one after another on the splash
screen automatically.
To begin with, create a Windows Forms application named SplashScreenDemo.
Add the ImageList component, Timer component, and PictureBox controls from the
Toolbox on to the form. The ImageList component is used to store one or more
images. The Timer component enables to specify a recurring interval at which the
Elapsed event is raised in your application, or in other words, specifies the
interval at which ticks occur. The PictureBox control is used to display an
image.
Configure the properties of the ImageList as shown in Figure 1.
Figure 1
Add the images present in the Sample Pictures folder (eg: C:\Documents and
Settings\TEST\My Documents\My Pictures \Sample Pictures) to the ImageList.
Figure 2 shows the outcome of this.
Figure 2
Configure the properties of the Timer as shown in Figure 3.
Figure 3
The interval is set to 900 millseconds. This means that a tick of the timer will
occur every 900 milliseconds.
Set the event for the Timer as shown in Figure 4.
Figure 4
Configure the properties of the PictureBox as shown in Figure 5. Here, you set
the default picture to Blue Hills.jpg. This picture will be seen in the
picturebox on loading the form.
Figure 5
Ensure that the size specified in the PictureBox for the image is the same as
that specified in ImageList for ImageSize property.
Add code to the code-behind class as shown below (you need to add the bolded
sections):
public partial
class SplashForm
: Form
{
private
int index;
public
SplashForm()
{
InitializeComponent();
tmrSplash.Enabled = true;
}
private void
tmrSplash_Tick(object sender,
EventArgs e)
{
if (index > 3)
index = 0;
picBox.Image = imgPics.Images[index];
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.Refresh();
index++;
}
}
In this code, the timer component is enabled. An int variable is initialized to
store the index of the images when we later iterate through the images in the
Tick event handler. In this event handler, we check if the last image has been
reached, if yes, we revert to the first image, else we continue. Each image from
the ImageList is assigned to the picturebox one by one on each Tick of the
timer. The image is stretched through the PictureBoxSizeMode.StretchImage
property and the index is incremented.
Thus, the functionality to iterate through a group of images on each timer tick
is created here.
Build and execute the application.
You will see a series of images appearing one after the other. This is a little
similar to the Ad rotator functionality seen in many websites.
Conclusion: Thus, you saw how to create a simple splash screen in Windows
Forms.