Click here to Skip to main content
Licence 
First Posted 17 Feb 2005
Views 164,054
Bookmarked 114 times

SplashForm - A Splash Window Form

By | 17 Feb 2005 | Article
This article is about a class that extends Windows Forms to create splash screens and About dialogs.

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.

// Create and Start the splash thread
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;

    // make form transparent
    this.TransparencyKey = this.BackColor;

    // tie up the events
    //...
    
    // load and make the bitmap transparent
    m_bmp = new Bitmap(imageFile);
    m_bmp.MakeTransparent(col);

    // resize the form to the size of the iamge
    this.Width = m_bmp.Width;
    this.Height = m_bmp.Height;

    // center the form
    this.StartPosition = 
       System.Windows.Forms.FormStartPosition.CenterScreen;

    // thread handling. Used because the window
    // will be closed from a different thread
    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);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

abhinaba

Web Developer

United States United States

Member

I just love coding. I started programming in 1995 with BASIC and then moved through Cobol, Pascal, Prolog, C, C++, VB, VC++ and now C#/.NET.
 
I received a Bachelor of Technology degree in Computer Science from University of Calcutta in 2001.
 
I worked for some time in Texas Instruments, Adobe Systems and now in Microsoft India Development Center in the Visual Studio Team Systems.
 
I am from the City of Joy, Kolkata in India, but now live and code Hyderabad.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralHmmm... Pinmemberdamir_tk10:48 1 Jun '08  
GeneralTried ShowModal() Pinmemberlmdarif11:00 11 Oct '07  
GeneralMinor simplification - MethodInvoker delegate PinmemberZoodor22:22 20 Sep '07  
GeneralRe: Minor simplification - MethodInvoker delegate PinmemberCharlesLentz6:05 24 Oct '08  
GeneralSolving a small bug in the code... Pinmemberboodi_811:36 3 Aug '07  
GeneralReally Nice PinmemberRicardo Casquete5:47 26 Jan '07  
GeneralRe: Really Nice PinmemberCharlesLentz7:10 24 Oct '08  
Generalfocus on main Pinmemberdahumada779:48 3 Feb '06  
GeneralRe: focus on main Pinmemberabhinaba5:53 4 Feb '06  
GeneralRe: focus on main Pinmemberdahumada775:08 23 Feb '06  
AnswerRe: focus on main Pinmembergfkeogh18:29 24 Jul '06  
AnswerRe: focus on main PinmemberCharlesLentz7:09 24 Oct '08  
GeneralUsing application ressources Pinmembermgroeschel5:15 13 Jan '06  
Questionhow to connect with my project PinmemberMuhnad13:54 11 Jan '06  
GeneralTransparency doesn't work under W98 PinmemberDave Midgley5:42 31 Dec '05  
GeneralRe: Transparency doesn't work under W98 PinmemberColin Angus Mackay7:25 31 Dec '05  
If I remember correctly, Windows 98 doesn't support transparency. Windows XP does, but only through trickery (it asks the control below what it would have drawn had the transparent control not been there without reference to controls below that as well. This means that if you have two transparent controls stacked on top of each other the result is a bit of a mess). Windows Vista will be the first OS that supports transparency properly.
 
My: Blog | Photos
 
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
GeneralRe: Transparency doesn't work under W98 Pinmemberabhinaba9:11 31 Dec '05  
GeneralConstructor should be private PinmemberDave Midgley2:10 31 Dec '05  
GeneralRe: Constructor should be private Pinmemberabhinaba9:07 31 Dec '05  
GeneralRe: Constructor should be private PinmemberDave Midgley23:08 2 Jan '06  
GeneralRe: Constructor should be private Pinmemberabhinaba2:50 4 Jan '06  
Questionworks on other VC++ ? Pinmemberfabteston23:36 26 Feb '05  
AnswerRe: works on other VC++ ? Pinmemberabhinaba4:21 27 Feb '05  
Generalnice, but... PinsussAnonymous2:26 17 Feb '05  
GeneralRe: nice, but... PinmemberMikael Wiberg20:25 17 Feb '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 17 Feb 2005
Article Copyright 2005 by abhinaba
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid