3

On Windows 7, .NET 4.0 I have a problem that can be reproduced by copying the following code into a blank WPF application generated by Visual Studio:

    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        new Window() { Title = "Test", ShowInTaskbar = false, Owner = this }.Show();
    }
  1. Run app
  2. Activate secondary window
  3. Alt-Tab to other running application
  4. Use mouse to activate our WPF app in taskbar

Now the WPF app is active again, with the secondary window activated and the main window deactivated, which is the expected (and desired) behavior.

Now do this instead (only step 4 differs):

  1. Run app
  2. Activate secondary window
  3. Alt-Tab to other running application
  4. Alt-Tab back to our WPF app

The WPF app is active again, but now the main window is activated. Adding this code

private void Application_Activated(object sender, EventArgs e)
{
    Windows[1].Activate();
}

to App.xaml.cs does not solve the problem because now in the second case both windows are activated. Also, clicking the secondary window does not deactivate the main window. I have to click the (already activated) main window and the secondary window again to achieve this.

How can I avoid this (only the secondary window should be active in both cases)?

Golvellius
  • 1,928
  • 2
  • 13
  • 16
  • Do you need main window to be active (even if unfocused)? If not then `ShowDialog()` instead of `Show()` should work for you. – icebat May 21 '13 at 13:57
  • Yes, the main window must be independent. This is just a minimal example to illustrate the problem, it's going to be a larger application. It's simply weird behavior that I would like to eliminate. Imagine something like Visual Studio - if I float a document out of the main window and then hit Alt-Tab twice, only the floating window is still activated and not the main window. I get problems processing input correctly if the main windows is active even though it shouldn't be. – Golvellius May 21 '13 at 14:31

2 Answers2

1

CodeProject actually addresses this issue here, hope this is what you're looking for.

Combine with a post from Tamil Khason and in theory you can override the OnFocus event on a global level so that every time a window is on focus, that becomes the "main window" which will then be the target of ALT+TAB.

Rick Riensche
  • 1,050
  • 1
  • 12
  • 25
Halaster
  • 1,442
  • 1
  • 17
  • 27
  • Unfortunately I can't apply it to my case, as he is talking about ShowDialog(). However, I have found a workaround: If I activate the second window in reponse to MainWindow_Activated instead of Application_Activated, I get the desired behavior. I feel a bit stupid for having not tried this earlier -- of course I have to make sure that this activation only happens in response to a directly preceding activation of the application, so keeping track of the currently active window is important. Thanks for your reply. – Golvellius May 22 '13 at 13:00
0

based on THIS solution the following code can maybe (not tested jet) also do the trick

  protected override void OnStartup(StartupEventArgs e)
            {
                EventManager.RegisterClassHandler(typeof(Window), Window.LoadedEvent, new RoutedEventHandler(WindowLoaded));

                base.OnStartup(e);
            }

    void WindowLoaded(object sender, RoutedEventArgs e)
            {
                Window w = sender as Window;
                if (w != null)
                {
                   // this part works in my case very well
                    w.Owner =Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
                }
            }
Community
  • 1
  • 1
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89