Introduction
I had written an article CSplash - A Splash Window Class about displaying splash screens using Win32 APIs. This article is about how the same thing is accomplished in .NET framework. This form can also be used for About dialogs.
Preparing the Image
First, create the image you would like to show as the splash, in your favorite image editor. Then choose the part of the image you would not like to be rendered on the screen, and give it a particular color that does not occur anywhere else on the image. For an example, if your image is a red disc with some logo or text inside it in blue, then fill all the region outside the circle with a color like green (anything other than red and blue).
Using SplashForm
The interface of the class is very simple. At the start of your application, call the static method StartSplash
and specify the bitmap file path and the color on it to be made transparent. This creates and shows the splash form in a new thread so that control immediately returns to your application and it can proceed with other initialization.
using System;
using Abhinaba.Splasher;
using System.Drawing;
SplashForm.StartSplash(@".\splash.bmp",
Color.FromArgb(128, 128, 128));
Once all initialization is complete, call the static method CloseSplash
to close the splash form.
SplashForm.CloseSplash();
To show the splash screen as an About dialog, use the ShowModal
method.
SplashForm.ShowModal(@".\splash.bmp",
Color.FromArgb(128, 128, 128));
Under the hood
In StartSplash
, a new thread is created which calls the method MySplashThreadFunc
.
Thread InstanceCaller = new Thread(new ThreadStart(MySplashThreadFunc));
InstanceCaller.Start();
This method creates the splash form and shows it in a new thread.
private static void MySplashThreadFunc()
{
m_instance = new SplashForm(m_imageFile, m_transColor);
m_instance.TopMost = false;
m_instance.ShowDialog();
}
The splash form constructor then prepares the form for transparency and then loads the image:
public SplashForm(String imageFile, Color col)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowInTaskbar = false;
this.TopMost = true;
this.TransparencyKey = this.BackColor;
m_bmp = new Bitmap(imageFile);
m_bmp.MakeTransparent(col);
this.Width = m_bmp.Width;
this.Height = m_bmp.Height;
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);}
On the Paint
event, the loaded image is displayed.
private void SplashForm_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawImage(m_bmp, 0,0);
}
To close the form, the CloseSplash
method must be called. The splash form has been created and is running in a different thread. So it is not possible to directly call Close
on it. For this, the following code is used:
public delegate void DelegateCloseSplash();
private DelegateCloseSplash m_delegateClose;
public SplashForm(String imageFile, Color col)
{
m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);
}
public static void CloseSplash()
{
m_instance.Invoke(m_instance.m_delegateClose);
}