Disable WPF Window Focus

17,103

Solution 1

Found the answer elsewhere:

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);

    //Set the window style to noactivate.
    var helper = new WindowInteropHelper(this);
    SetWindowLong(helper.Handle, GWL_EXSTYLE,
        GetWindowLong(helper.Handle, GWL_EXSTYLE) | WS_EX_NOACTIVATE);
}   

private const int GWL_EXSTYLE = -20;
private const int WS_EX_NOACTIVATE = 0x08000000;

[DllImport("user32.dll")]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

Solution 2

Since .NET 3.5 SP1 WPF forms have a ShowActivated property. Set this to false and the form thus marked won't steal no focus no more.

Solution 3

You can prevent a WPF Window from activating on mouse click by adding a custom WndProc and handling WM_MOUSEACTIVATE:

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);
    var source = PresentationSource.FromVisual(this) as HwndSource;
    source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == WM_MOUSEACTIVATE)
    {
        handled = true;
        return new IntPtr(MA_NOACTIVATE);
    }
    else return IntPtr.Zero;
}
private const int WM_MOUSEACTIVATE = 0x0021;
private const int MA_NOACTIVATE = 0x0003;

References:

Solution 4

Prevent certain top-level windows from getting activated in WPF:

I tried the Win32 solution given here but it didn't work for me. While it does seem to prevent window "activation," the Focus is left in limbo afterwards, not restored to another eligible window in your application. Instead, the following worked for me:

First, make sure that all the non-primary windows have their Owner property set to the main Window. I do this in the constructor of the sub-window, in which case one must take some steps (not discussed here) to make sure that the main Window is loaded first.

public MySubWindow()
{
    if ((base.Owner = Application.Current.MainWindow) == null)
        throw new Exception();

    InitializeComponent();
}

Setting the Owner property should also ensure that the sub windows stay on top of the main window. For the sub window(s), set the following properties as indicated (XAML or code):

ShowActivated="False"
Focusable="False"
ShowInTaskbar="False"
IsEnabled="False"
FocusManager.IsFocusScope="False"

Finally, add a handler for OnActivated to the blocked windows. I don't call the base method since it fires the Activated event. (Note that you should not switch the activation away from the Visual Studio designer since it makes the window invisible).

protected override void OnActivated(EventArgs e)
{
    if (DesignerProperties.GetIsInDesignMode(this))
        return;

    base.Owner.Activate();
}
Share:
17,103

Related videos on Youtube

Lunyx
Author by

Lunyx

Updated on July 03, 2022

Comments

  • Lunyx
    Lunyx almost 2 years

    I have a WPF Window that shows up only when you hold down the tab key via Visibility.Hidden and Visibility.Visible. However, holding the key down shifts the focus from the active application to the WPF Window. Can I disable this behavior? Going even further: is it possible to completely prevent the window from getting focus even when a control is clicked, but still registering the click action of the control?

    • Lunyx
      Lunyx about 6 years
      @rugk This is different as the other question is asking how to show without taking focus, but is vague on whether or not user interaction is allowed. The answers also only solve the portion of no activation, but not it's not clear if user interaction is allowed (top selected answer does not).
  • Lunyx
    Lunyx over 11 years
    I have tried this already. It just causes my program to crash.
  • Lunyx
    Lunyx over 11 years
    You can see here: msdn.microsoft.com/en-us/library/system.windows.window.aspx that Window also has a Focusable property. I've tried setting everything to Focusable = false, but it still doesn't resolve my issue.
  • Nick
    Nick over 8 years
    This needs to happen in the Loaded event, which occurs before the Activated event. If you do it in OnActivated, the window is already activated and its too late... it only prevents focus from that point out.
  • Ievgen
    Ievgen about 8 years
    Better to use SourceInitialized
  • tofutim
    tofutim about 8 years
    I never get a message WM_MOUSEACTIVATE
  • mgarant
    mgarant over 7 years
    Wow! I've been looking for a solution to this problem for 2 days and it's the only one that did it for me! Thank you so much for this fantastic piece of code and for making me learn :)
  • Der_Meister
    Der_Meister almost 6 years
    It's better to use SetWindowLongPtr and GetWindowLongPtr. See definitions on the pinvoke.net web site.