Splash Screens and non-rectangular windows


I needed a splash screen for my music player app. I tried a couple of ideas that didn’t
work very well, but then I hit on the idea of using the image
of a CD
as the background of the splash screen – and doing a non-rectangular window.
I’ll start with the splash screen…

Conceptually, a splash screen is pretty simple – it’s just a form that you bring up
at the start of the program. The code is something like this:

SplashForm splashForm;
public MyForm()
{
    splashForm = new SplashForm();
    splashForm.Show();
}

and we’ll hook the Activated event to hide the form.

private void Form1_Activated(object sender, System.EventArgs e)
{
    splashForm.Close();
}

When we run this, however, something strange happens. The form comes up, but it never
renders – there’s just a black background. This is because we need the windows messages
to be processed to get the form to paint. There are several ways to do this, but my
choice is to simply call Application.DoEvents() after the form is shown. This processes
any messages that have yet to be processed, showing the form.

Next, we need to make a non-rectangular form. This is done by setting the region on
the form. This is surprisingly easy to do. Here’s the code I used for the CD:

public SplashForm()
{
    GraphicsPath p = new GraphicsPath();
    p.AddEllipse(2, 4, 340, 341);
    p.AddEllipse(150, 153, 43, 43);
    this.Region = new Region(p);

    …
}

The first ellipse defines the outside of the picture, and then the second one this
inside. This gives me a round window with a hole in the center.

Pretty cool!

Comments (8)

  1. Jacob says:

    I use a slightly different approach for my splash screens, but I think the end result is the same:

    Public Class ApplicationEntryPoint
    Private Shared m_splash As SplashScreen
    Private Shared m_context As ApplicationContext

    Private Sub New()
    End Sub

    <STAThread()> _
    Public Shared Sub Main(ByVal CmdArgs() As String)
    m_context = New ApplicationContext()
    If CmdArgs.Length > 0 AndAlso CmdArgs(0) = "/NoSplash" Then
    m_context.MainForm = New MainForm()
    Else
    AddHandler Application.Idle, AddressOf AppIdle
    m_splash = New SplashScreen()
    m_splash.TopMost = True
    m_splash.Show()
    End If
    Application.Run(m_context)
    End Sub

    Private Shared Sub AppIdle(ByVal sender As Object, ByVal e As EventArgs)
    If m_context.MainForm Is Nothing Then
    RemoveHandler Application.Idle, AddressOf AppIdle
    m_context.MainForm = New MainForm()
    m_context.MainForm.Show()
    m_splash.Close()
    m_splash = Nothing
    End If
    End Sub

    End Class

  2. Irfan Patel (MCSD) says:

    Nice code for the non-rectangular way but I created a new class for splash screen and frmMain in C#.

    There is a timer set in frmSplash whose tick event has databaseconnection code and this.Close(); to close the frmSplash

    :) The additional class is as follows

    using System;

    using System.Windows.Forms;

    namespace MyNamespace.Classes

    {

    /// <summary>

    /// Summary description for clsMain.

    /// </summary>

    public class clsMain

    {

    public clsMain()

    {

    }

    [STAThread]

    static void Main()

    {

    frmSplash objfrmSplash = new frmSplash();

    objfrmSplash.ShowDialog();

    clsGlobal.g_objfrmMain = new frmMain();

    //clsGlobal.g_objfrmMain.Show();

    Application.Run(clsGlobal.g_objfrmMain);

    }

    }

    }

    Have a nice time :)

    patelirfan786@yahoo.com

  3. MBA says:

    Helpful For MBA Fans.

  4. .Avery Blog says:

    Splash Screens in Windows Forms

  5. James Avery makes a point that Eric Gunnerson has a way of doing splash screens. Unfortunately…