SplashScreen.Close () Steals MainWindow Focus

When SplashScreen closes (manually or using AutoClose), it steals the focus of MainWindow during the fade out animation. As a result, the title bar of the main window switches from active to inactive (gray) to active. Is there a trick to getting SplashScreen to steal focus?

+4
source share
2 answers

Tell SplashScreen that MainWindow is its parent window. When a child window loses focus, its parent gains focus. If there is no parent, the window manager decides.

splashScreen.Show (MainWindow); Strike>

EDIT

I just found out that there is a SplashScreen class. It looks like you are using this class, not just the regular form, as I assumed.

So, I just made a simple WPF application with SplashScreen, and for me the mentioned effect did not happen. The main window has not lost focus.

I would suggest you comment on the potions of your application initialization code until the blinking stops. Then you have a starting point for further research on why the focus is lost.

EDIT2

Not knowing my code, I tried to reproduce this phenomenon, and it was not too difficult. No matter what I tried, a change in focus always occurred when the main window was already shown and had focus.

So, the best solution that I see is to manually show the main window after , calling the Spl () splash screen method:

  • Remove StartupUri from App.xaml

  • Show SplashScreen after starting the application and initializing resources. After a (fixed) delay, close SplashScreen and show the main window:


public partial class App : Application { const int FADEOUT_DELAY = 2000; SplashScreen splash = new SplashScreen("splash.jpg"); protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); splash.Show(false, true); var worker = new BackgroundWorker(); worker.DoWork += (sender, ea) => { Thread.Sleep(1000); splash.Close(new TimeSpan(0, 0, 0, 0, FADEOUT_DELAY)); // you could reduce the delay and show the main window with a nice transition Thread.Sleep(FADEOUT_DELAY); Dispatcher.BeginInvoke(new Action(() => MainWindow.Show())); }; worker.RunWorkerAsync(); MainWindow = new MainWindow(); // do more initialization } } 
+5
source

Using ILSpy, I found that the SplashScreen.Close method calls SetActiveWindow at some point, causing the slpash screen to activate when it closes. (At least in .NET 4.0.) I added a call to my MainWindow.Focus right after calling SplashScreen.Close , and this fixes the problem.

Otherwise, I could see how the main window was inactive only while the fadeout animation was busy. In addition, I show a modal dialog box when the main window is loaded and it remains active. But when this dialog box closes, the main window will no longer focus, and instead I will get into the Visual Studio window or something that launched my application. Inserting a Focus call from above also helps in this situation.

By the way, here is a good article that explained to me how to use the SplashScreen class manually instead of the project file option: http://tech.pro/tutorial/822/wpf-tutorial-using-splash-screens-in-sp1

This is the code from my application:

In App.xaml.cs:

 /// <summary> /// Application Entry Point. /// </summary> [STAThread] public static void Main() { splashScreen = new SplashScreen("Splashscreen.png"); splashScreen.Show(false, true); Thread.Sleep(2000); // For demonstration purposes only! App app = new App(); app.InitializeComponent(); app.Run(); } 

In the main window of the ViewModel Init command (the main window is already fully visible):

 private void OnInitCommand() { ConnectDatabase(); App.SplashScreen.Close(TimeSpan.FromMilliseconds(500)); MainWindow.Instance.Focus(); // This corrects the window focus SomeDialog dlg = new SomeDialog(); dlg.Owner = MainWindow.Instance; if (dlg.ShowDialog() == true) { // Do something with it } // Now the main window is focused again } 
+2
source

All Articles