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# 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...

C# WPF Tutorial - Implementing IScrollInfo [Advanced]

The ScrollViewer in WPF is pretty handy (and quite flexible) - especially when compared to what you had to work with in WinForms ( ScrollableControl ). 98% of the time, I can make the ScrollViewer do what I need it to for the given situation. Those other 2 percent, though, can get kind of hairy. Fortunately, WPF provides the IScrollInfo interface - which is what we will be talking about today. So what is IScrollInfo ? Well, it is a way to take over the logic behind scrolling, while still maintaining the look and feel of the standard ScrollViewer . Now, first off, why in the world would we want to do that? To answer that question, I'm going to take a an example from a tutorial that is over a year old now - Creating a Custom Panel Control . In that tutorial, we created our own custom WPF panel (that animated!). One of the issues with that panel though (and the WPF WrapPanel in general) is that you have to disable the horizontal scrollbar if you put the panel in a ScrollV...