Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

SplashForm - A Splash Window Form

0.00/5 (No votes)
17 Feb 2005 1  
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