19

Dupe: WPF animated splash screen

I would like to show a splash screen for my WPF application. what I want to do is to show it while I load dictionary from a file (it takes about 5-6 seconds to load). Is there a way to achieve this in WPF? I would appreciate some tutorial, since this is a little bit more complicated then other questions I posted.

Community
  • 1
  • 1
sokolovic
  • 263
  • 1
  • 3
  • 13
  • possible duplicate of [WPF animated splash screen](http://stackoverflow.com/questions/3677653/wpf-animated-splash-screen) – Preet Sangha Oct 12 '10 at 11:44

3 Answers3

31

A SplashScreen is really just another Window with no border, and it is not resizable (nor can you interact with it in any way). You'd probably want to hide it from the task bar, center it on the screen, etc. Play around with various settings until you get the effect that you want.

Here's a quick one I whipped up in about 5 minutes to prove the theory:

<Window x:Class="MyWhateverApp.MySplashScreen"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ShowInTaskbar="False" 
        ResizeMode="NoResize" 
        WindowStartupLocation="CenterScreen"
        WindowStyle="None" 
        Background="Transparent" 
        AllowsTransparency="True"
        Title="Sandbox Splash Screen" 
        SizeToContent="Width" 
        Topmost="True" 
        Height="{Binding RelativeSource={RelativeSource Self}, 
                         Path=ActualWidth}">

    <Border CornerRadius="8" Margin="15">
        <Border.Background>
            <ImageBrush ImageSource="Resources\sandtexture.jpeg" 
                        Stretch="Fill" />
        </Border.Background>
        <Border.Effect>
            <DropShadowEffect Color="#894F3B" 
                              BlurRadius="10" 
                              Opacity="0.75" 
                              ShadowDepth="15" />
        </Border.Effect>

        <TextBlock FontSize="40"
                   FontFamily="Bauhaus 93"
                   Foreground="White"
                   Margin="10"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Text="WPF 3.5 Sandbox">
            <TextBlock.Effect>
                <DropShadowEffect Color="Black" />
            </TextBlock.Effect>
        </TextBlock>        
    </Border>
</Window>

Next, modify your App.xaml file to remove the startup window, and instead raise the Startup event:

<Application x:Class="MyWhateverApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
    <Application.Resources>

    </Application.Resources>
</Application>

And in the code-behind, handle the Application_Startup event in whatever way you think is best. For example:

Window1 mainWindow = null;

private void Application_Startup(object sender, StartupEventArgs e)
{
    MySplashScreen splash = new MySplashScreen();
    splash.Show();
    mainWindow = new Window1();
    mainWindow.Show();
    splash.Close();
}
Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92
14

See WPF 3.5 SP1: Splash Screen

Or within VS2010 click on the Solution Explorer do Add -> New Item, select WPF from the list of installed templates and Splash Screen should be on the bottom of the list in the middle.

Note: The splash screen is removed after the constructor and before/when the main window Window_Loaded callback. I moved all of my initialisation into the main window constructor and it works a treat, and is very easy.

Richard Harrison
  • 19,247
  • 4
  • 40
  • 67
  • I think that I cannot control for how long splash will be shown (possible default time is 0.5 seconds, or something like that). What I would like to achieve is to show splash, WHILE loading some data (5-6 seconds). – sokolovic Oct 12 '10 at 11:59
  • the splash screen is removed after the constructor and when the main window Window_Loaded callback. I moved all of my initialisation into the main window constructor and it works a treat, and is very easy. – Richard Harrison Oct 12 '10 at 17:10
9

Short answer: Add -> New Item -> Splash Screen. It dumps a PNG in the project - just modify this. note that it supports full alpha transparencey so can contain drop-shadows, etc...

Basic
  • 26,321
  • 24
  • 115
  • 201
  • 3
    While this is useful for some scenarios, the major problem with SplashScreen is that it doesn't support dynamic content (since the image is bundled at compile time). If you want to show progress bar on your splash screen, or even application version, you must create a standard WPF window. On the positive side, splash screens created with this method load faster because it doesn't require framework to load. – dotNET Jun 11 '17 at 14:05
  • @dotNET Yeah, it's always a trade-off. In the past, I've created a new window handle and performed the painting directly, which meant I could implement a progress bar without needing winforms/wpf to finish loading. Was very quick and powerful (could alpha, click-through, etc) but quite inflexible as you were re-implementing UI functionality for each control you wanted to render, and had to handle invalidation/overdraw efficiently. – Basic Jun 11 '17 at 18:53