Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

Implement Splash Screen with WPF

Rate me:
Please Sign up or sign in to vote.
4.70/5 (44 votes)
23 Jul 2009CPOL3 min read 253.6K   19.3K   123   29
Show a custom splash screen with loading messages

Introduction

Now almost all applications have a splash screen. This screen will be shown when the application starts, after the main window of the application is shown, this screen will be closed.

Demo effect, the application is loading modules.

Background

WPF has provided an easy way to implement splash screen. Using this way, we should add an image as resource to the project, and set its BuildAction property to “SplashScreen”. This feature is only supported on .NET Framework 3.5. When the application starts, the image will be shown at first as the splash screen. I know this way is implemented via WinForm technology.

The above method is easy, but it is too simple and doesn’t meet my requirements. My requirements of splash screen are:

  • The way of show the screen is reusable, thus each application I made can use the same way to show a splash screen
  • The splash screen is easy to customize, thus each application I made can have different splash screens and I can make it as beautiful as I can
  • The splash screen can show messages to the user, for example show the loading progress of the application
  • The splash screen may not be a regular shape, it may be a circle and other not rectangle shape, but this is not necessary and important to me

Normally a splash screen has the below features:

  • The splash screen doesn’t show in the task bar
  • The splash screen is always shown at the center of the screen
  • The splash screen doesn’t have window title bar and border, it is not resizable

My Design

To meet the above requirements, my design is as below:

  • App

    App class inherits from Application class and provides the application entry.
    When entering the Main method, initialize the splash screen and show it by calling the Splasher class, and then run the application, show the main window.

    C#
    /// <summary>
        /// 
        /// </summary>
        class App: Application
        {
            /// <summary>
            /// 
            /// </summary>
            [STAThread ( )]
            static void Main ( )
            {
                Splasher.Splash = new SplashScreen ( );
                Splasher.ShowSplash ( ); 
    
                // Simulate application loading
                for ( int i = 0; i < 5000; i++ )
                {
                    MessageListener.Instance.ReceiveMessage 
    			( string.Format ( "Load module {0}", i ) );
                    Thread.Sleep ( 1 );
                } 
                new App ( );
            }
             /// <summary>
            /// 
            /// </summary>
            public App ( )
            {         
                StartupUri = new System.Uri ( "MainWindow.xaml", UriKind.Relative ); 
                Run ( );            
            }
        } 
  • MainWindow

    Application’s main window, it’s just a normal window.

  • SplashScreen

    Splash screen window, it’s just a normal window, this window has the special features that a splash screen should have. I have mentioned it above.

  • Splasher

    A helper class to show and close splash screen window, the splash screen window should be initialized by the application. With this class, I don't care which window is the splash screen, and it’s reusable.

    C#
    /// <summary>
    /// Helper to show or close given splash window
    /// </summary>
    public static class Splasher
    {
        /// <summary>
        /// 
        /// </summary>
        private static Window mSplash; 
        /// <summary>
        /// Get or set the splash screen window
        /// </summary>
        public static Window Splash
        {
            get
            {
                return mSplash;
            }
            set
            {
                mSplash = value;
            }
        } 
        /// <summary>
        /// Show splash screen
        /// </summary>
        public static void ShowSplash ( )
        {
            if ( mSplash != null )
            {
                mSplash.Show ( );
            }
        }
        /// <summary>
        /// Close splash screen
        /// </summary>
        public static void CloseSplash ( )
        {
            if ( mSplash != null )
            {
                mSplash.Close ( ); 
                if ( mSplash is IDisposable )
                    ( mSplash as IDisposable ).Dispose ( );
            }
        }
    } 
  • DispatcherHelper

    This is a helper class, implement DoEvents method like System.Windows.Forms.Application’s DoEvents method, if there is no such function, the loading message of the splash will not be refreshed correctly. The code is shown below:

    C#
    /// <summary>
    /// Simulate Application.DoEvents function of 
    /// <see cref=" System.Windows.Forms.Application"/> class.
    /// </summary>
    [SecurityPermissionAttribute ( SecurityAction.Demand, 
    	Flags = SecurityPermissionFlag.UnmanagedCode )]
    public static void DoEvents ( )
    {
        DispatcherFrame frame = new DispatcherFrame ( );
        Dispatcher.CurrentDispatcher.BeginInvoke ( DispatcherPriority.Background,
                    new DispatcherOperationCallback ( ExitFrames ), frame ); 
        try
        {
            Dispatcher.PushFrame ( frame );
        }
        catch ( InvalidOperationException )
        {
        }
    } 
  • MessageListener

    This class inherit from DependencyObject, WPF’s UI element can easily bind to its property and update correctly. This class just receives a string message. To show the loading progress message in the splash screen, I add a TextBlock element to the window, and bind its Text property to listener’s Message property, and when the application is loading, I write messages to the listener.

Final Effect

The final splash screen looks like below. I define the window with a WPF window, the Label at the bottom of the window shows the loading tip messages (progress) of the application.

A very simple main window.

History

  • Version 1.0 2009.07.20

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI wish it was possible Pin
Member 144722842-Aug-20 22:02
Member 144722842-Aug-20 22:02 
Questionsplash Pin
jamesklett27-Nov-18 7:05
jamesklett27-Nov-18 7:05 
SuggestionGreat job if following code segment could be added! Pin
Member 1334938627-Apr-18 22:02
Member 1334938627-Apr-18 22:02 
GeneralElegant! Pin
Member 1315644527-Apr-17 5:09
Member 1315644527-Apr-17 5:09 
GeneralImproved Version Pin
Member 1142806210-Sep-15 1:25
Member 1142806210-Sep-15 1:25 
Questionhow to start form1 Pin
Member 105381831-Feb-14 19:15
Member 105381831-Feb-14 19:15 
Dears,

I want to deploy this nice splash screen but I could not replace the mainwindow by my designed form1 I have added.

where exactly he call the main window so I can put my form1 instead.

waiting your kind reply
QuestionHow to bind modules? Pin
prachur22-Nov-13 3:09
prachur22-Nov-13 3:09 
GeneralVery helpful - thanks! Pin
Jon Richt4-Nov-13 10:18
Jon Richt4-Nov-13 10:18 
GeneralMy vote of 4 Pin
Sergey Orlov22-Nov-12 0:30
Sergey Orlov22-Nov-12 0:30 
QuestionAbout splash screen design Pin
avisal21-Sep-12 10:38
professionalavisal21-Sep-12 10:38 
GeneralMy vote of 4 Pin
NirdeshSaini15-Mar-12 4:44
NirdeshSaini15-Mar-12 4:44 
GeneralMy vote of 1 Pin
crusjiber31-Jan-12 23:50
crusjiber31-Jan-12 23:50 
GeneralMy vote of 1 Pin
Paul3221-Jan-12 2:55
Paul3221-Jan-12 2:55 
GeneralMy vote of 1 Pin
ian oakes2-Jun-11 18:43
ian oakes2-Jun-11 18:43 
Questiononly running at c#? Pin
yardloun9-Apr-11 2:57
yardloun9-Apr-11 2:57 
GeneralMy vote of 2 Pin
42882-Jan-11 8:31
42882-Jan-11 8:31 
GeneralSplach screen - how it should be done ... Pin
kadukf18-May-10 2:59
kadukf18-May-10 2:59 
QuestionRe: Splach screen - how it should be done ... Pin
prophetpp15-Aug-11 1:53
prophetpp15-Aug-11 1:53 
GeneralNot the best solution IMO Pin
Saragani214-Apr-10 3:48
Saragani214-Apr-10 3:48 
GeneralRe: Not the best solution IMO Pin
Andy Lang14-Apr-10 4:28
Andy Lang14-Apr-10 4:28 
GeneralRe: Not the best solution IMO Pin
kreutpet9-Aug-11 5:24
kreutpet9-Aug-11 5:24 
GeneralRe: Not the best solution IMO Pin
Leif Simon Goodwin22-Mar-19 6:34
Leif Simon Goodwin22-Mar-19 6:34 
Generalunload splash Pin
peug11-Jan-10 23:02
peug11-Jan-10 23:02 
GeneralRe: unload splash Pin
Andy Lang12-Jan-10 3:17
Andy Lang12-Jan-10 3:17 
GeneralRe: unload splash Pin
New-LeuKalm22-Jul-10 22:41
New-LeuKalm22-Jul-10 22:41 

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

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