17
votes

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.

3
possible duplicate of WPF animated splash screenPreet Sangha

3 Answers

13
votes

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.

27
votes

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();
}
8
votes

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