views:

147

answers:

1

I have an application that has a splash screen shown before I create my main window. I want to avoid the main window stealing the focus when it gets created if the user has focused another application while the splash screen was shown.

My application's startup looks like this:

private void Application_Startup(object sender, StartupEventArgs e)
{
    SplashScreenService.Start();

    // Initializing stuff...
    ...

    // Ready to open the main window.
    MainWindow mainWindow = MainWindow();
    mainWindow.ShowActivated = SplashScreenService.HasFocus();
    mainWindow.ShowDialog();
}

To prevent the main window from getting activated, I'm setting Window.ShowActivated to false, only if the splash screen has lost the focus. But this causes an exception:

InvalidOperationException: Cannot show Window when ShowActivated is false and WindowState is set to Maximized.

Indeed, my MainWindow sets the WindowState to Maximized (restored from previous session settings). I don't understand why there is such a limitation with the ShowActivated feature. I couldn't find any documentation about this issue.

I tried forcing WindowState to Normal when ShowActivated is false, and it prevents the exception, but it also activates the window! Maybe changing the window state actually causes an activation?

Is there a trick I can use to work around this? I want to restore my main window in a maximized state, and not have the main window steal focus away from other applications if the user has focused away from my app during the splash screen.

A: 

I had this problem as well.

The first thing I tried was to re-activate the main window after creating and showing the splash screen. This returned the activation to the main window and worked but it didn't seem very elegant.

A better solution is to not set WindowState on the splash screen to maximize, instead manually maximize the splash screen to cover the entire screen using interop and Windows Forms as described here:

http://www.inveigledsoftware.com/2009/07/maximizing-a-wpf-window-with-windowstatenone/

Ashley Davis
It's the Main Window that's maximised, not the splash screen.
ChrisF
Sorry, my misunderstanding ;) This technique could still work for you though, just don't set the Window state of the main window and manually maximize it.
Ashley Davis