Fredson
Fredson

Reputation: 85

How to wait for click then close the Splashscreen in WPF application?

I have a WPF Application and there's a button which shows an image (Splash Screen) containing the company logo and the name of the developers of the application. I want this image be shown until the user interacts with whatever. When user clicks or enters a keyboard key, the image must close. Refer to the comment line in my code.

private void Info_BeforeCommandExecute(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
    SplashScreen SS = new SplashScreen("Images/InfotecSplashScreenInfo.png");
    SS.Show(false);

    // Need to do something here to wait user to click or press any key

    SS.Close(TimeSpan.FromSeconds(1));
}

Upvotes: 2

Views: 1873

Answers (2)

Brock Hensley
Brock Hensley

Reputation: 3655

Reed's answer is no doubt easiest, but you could also avoid using SplashScreen altogether and just use a custom Window to give you full control.

YourCode.cs

private void Info_BeforeCommandExecute(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
    SplashWindow SS = new SplashWindow();
    SS.ShowDialog(); // ShowDialog will wait for window to close before continuing
}

SplashWindow.xaml

<Window x:Class="WpfApplication14.SplashWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SplashWindow" Height="350" Width="525">
    <Grid>
        <!-- Your Image here, I would make a Custom Window Style with no border and transparency etc. -->
        <Button Content="Click Me" Click="Button_Click"/>
    </Grid>
</Window>

SplashWindow.xaml.cs

public partial class SplashWindow : Window
{
    public SplashWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Once user clicks now we can close the window 
        // and/or add keyboard handler
        this.Close();
    }
}

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564691

You could add a handler for the Keyboard class using AddKeyDownHandler:

private void Info_BeforeCommandExecute(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
    SplashScreen SS = new SplashScreen("Images/InfotecSplashScreenInfo.png");
    SS.Show(false);

    KeyEventHandler handler;
    handler = (o,e) =>
    {
          Keyboard.RemoveKeyDownHandler(this, handler);
          SS.Close(TimeSpan.FromSeconds(1));
    };

    Keyboard.AddKeyDownHandler(this, handler);        
}

This would allow you to close the splash screen after the user presses a key.

Upvotes: 4

Related Questions