Win7 WPF Alt+Tab Focus Oddity

1.9k views Asked by At

After a couple hours of Googling I haven't been able to find any comments on this issue. We have a WindowStyle=None window with transparent background and allows transparency and does not show in taskbar, all pretty normal. Here's the XAML so you can test for yourself:

<Window x:Class="AltTabTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="TestWindow" Title="TestWindow"
WindowStyle="None" AllowsTransparency="True" ResizeMode="NoResize" 
Background="Transparent" ShowInTaskbar="False"
Width="816" Height="820">

<Grid>
    <Border BorderThickness="0" Background="LightBlue" CornerRadius="15" />
</Grid>

Now what's bizarre is once you compile and run this window, follow these steps:

  1. Click on Show Desktop to hide all apps
  2. Alt+Tab back to the WPF Test App
  3. Click outside of the Light Blue border area (to the desktop workspace)
  4. Observe WPF Test App magically disappear
  5. Optionally Alt+Tab to any other running app and observe WPF Test App magically re-appear

So my question is: what the heck is going on here?! Is this expected behavior? If so, is there any way around it?

It feels like the WPF app is not truly getting focus after the Alt+Tab is resolved. Please note this is being tested in Windows 7 and I have not had the capability to test this in Vista or XP. I'd like a way to force the app to truly get focus, but if that's not possible then I'm wondering if there's a way to trap and ignore Alt+Tab actions. The app this is intended to fix is a lockdown mode app so users should never be able to truly get away from it running on the desktop. Any advice, examples, insight or point in the right direction would be appreciated, thanks! =)

1

There are 1 answers

1
Jim Wallace On

According to Spy++ the problem is that the window when set with WindowStyle="None" is not receiving a WM_ACTIVATE message anymore when you alt-tab to it. Having AllowsTransparency="True" is disabling hit testing for the window, so when you click beyond the blue rectangle the only HitTest returning true is that of the desktop.

Because WM_ACTIVATE was never fired, the MinimizeAllWindowsToDesktop thingey doesn't recognize any active windows and so when it receives the notification that you clicked on the desktop the desktop is rendering just as it would had no windows activated and you were doing some work on the desktop (the expected behavior for this function).

I don't know why in Win7 the window is not receiving the WM_ACTIVATE message though.


EDIT: Nevermind, this looks like it's just a bug in WPF and Windows 7. This behavior persists no matter what the settings of the window.