Skip to main content

C# WPF Tutorial - Using Splash Screens [Beginner]


Splash screens - everyone loves them, right? Actually, I'm not a big fan of them, but sometimes they are a necessary evil - especially in the world of WPF. A cold start up of a WPF application can be, well, slow - mostly because there are a lot of common WPF dlls that need to be loaded. Once they are loaded for the first time after boot up, WPF apps generally start pretty fast, but that first WPF app after a reboot can crunch away at your hard drive for quite a few seconds.

Adding a splash screen for WPF in the .NET 3.5 SP1 world was a huge pain. You had to pull in a couple native methods and essentially create your own window, and on top of all that there was a special build step that had to be executed. But now, it's so easy, it's almost not worth a tutorial (but I'm going to write it up anyway, cause hey, you never know).

Adding a splash screen for a WPF app has been turned into a three step process now that SP1 is out . The first step is to make the splash screen image. Break out Photoshop and do your worst! For the sample app here today, I just took a screen shot of the webpage and cropped out the top logo, so the splash screen image is going to look like this:

Image We Will Use As a Splash
Screen

OK, you have an image now? Good. Add it as an item to your WPF project. In our case, it is a png called SOTCSplash.png:

Visual Studio project listing with our image

And now for the final, horribly complicated step. Select that newly added image, go to the item properties, and for the Build Action property choose "SplashScreen":

Setting the build action on the image

Your done! That is all that you need to do now to create a splash screen for a WPF app. In the case of our example, the app looks like this when starting up:

A screenshot of the spash screen in action

The splash screen even does a nice automatic fade out.

Now, if you want a little more control over the splash screen, you can choose not to use that build action, and instead use the new SplashScreen class that comes in SP1. This is more complicated than the build action, but much less complicated than the old way. First, we have to add a Main method to App.xaml.cs. Generally, Visual Studio creates a main method automatically, but we don't want that to happen anymore, because we will be putting special code in that method. To do this, change the "Build Action" for App.xaml from "Application Definition" to "Page".

Now, in App.xaml.cs, we add a main method. The standard base main method for WPF looks something like this:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
public static void Main()
{
  app.InitializeComponent();
  app.Run();
}
 
Now we want to add the splash screen to that. First, we need to set the build action for the image to "Resource", so that we can reference it. Then we add a couple lines to the main method:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
public static void Main()
{
  SplashScreen splashScreen = new SplashScreen("SOTCSplash.png");
  splashScreen.Show(true);
  app.InitializeComponent();
  app.Run();
}
 
That will give the exact same behavior as using that "SplashScreen" build action from before. If we want to customize it a bit, we can do something like this:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
public static void Main()
{
  SplashScreen splashScreen = new SplashScreen("SOTCSplash.png");
  splashScreen.Show(false);
  SplashScreenTest.App app = new SplashScreenTest.App();
  app.InitializeComponent();
  splashScreen.Close(TimeSpan.FromMilliseconds(1000));
  app.Run();
}
 
The boolean passed into the splash screen Show function determines if the splash screen will close automatically (true is automatic, false is manual). If we choose false, we have to remember to call Close at some point, but we also get the ability to say how long it takes for the splash screen to fade out. In this case, we are saying the fade out should take 1000 milliseconds (instead of the default 300).

And that is about it for the new stuff on splash screens that came with SP1 for .NET 3.5 and VS2008. You can check out the new SplashScreen class at MSDN. And just to leave on an amusing note here - Microsoft went to all this work to make splash screens easy in SP1, but their own User ExperienceGuidelines forVista recommend against using splash screens in the first place.

Comments

Popular posts from this blog

C# Snippet - Shuffling a Dictionary [Beginner]

Randomizing something can be a daunting task, especially with all the algorithms out there. However, sometimes you just need to shuffle things up, in a simple, yet effective manner. Today we are going to take a quick look at an easy and simple way to randomize a dictionary, which is most likely something that you may be using in a complex application. The tricky thing about ordering dictionaries is that...well they are not ordered to begin with. Typically they are a chaotic collection of key/value pairs. There is no first element or last element, just elements. This is why it is a little tricky to randomize them. Before we get started, we need to build a quick dictionary. For this tutorial, we will be doing an extremely simple string/int dictionary, but rest assured the steps we take can be used for any kind of dictionary you can come up with, no matter what object types you use. Dictionary < String , int > origin = new Dictionary < string , int >();

C# Snippet - The Many Uses Of The Using Keyword [Beginner]

What is the first thing that pops into your mind when you think of the using keyword for C#? Probably those lines that always appear at the top of C# code files - the lines that import types from other namespaces into your code. But while that is the most common use of the using keyword, it is not the only one. Today we are going to take a look at the different uses of the using keyword and what they are useful for. The Using Directive There are two main categories of use for the using keyword - as a "Using Directive" and as a "Using Statement". The lines at the top of a C# file are directives, but that is not the only place they can go. They can also go inside of a namespace block, but they have to be before any other elements declared in the namespace (i.e., you can't add a using statement after a class declaration). Namespace Importing This is by far the most common use of the keyword - it is rare that you see a C# file that does not h

C# WPF Printing Part 2 - Pagination [Intermediate]

About two weeks ago, we had a tutorial here at SOTC on the basics of printing in WPF . It covered the standard stuff, like popping the print dialog, and what you needed to do to print visuals (both created in XAML and on the fly). But really, that's barely scratching the surface - any decent printing system in pretty much any application needs to be able to do a lot more than that. So today, we are going to take one more baby step forward into the world of printing - we are going to take a look at pagination. The main class that we will need to do pagination is the DocumentPaginator . I mentioned this class very briefly in the previous tutorial, but only in the context of the printing methods on PrintDialog , PrintVisual (which we focused on last time) and PrintDocument (which we will be focusing on today). This PrintDocument function takes a DocumentPaginator to print - and this is why we need to create one. Unfortunately, making a DocumentPaginator is not as easy as