I do not allow my wpf window to get focus by doing below, and I can still activate it using ALT-TAB or by clicking on it with a taskbar item.
Here you change the window styles in your window so that it does not activate.
var yourWindow = new YourWindowType();
This is what I added as an extra precaution, plus it allows you to drag your window if it is borderless:
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) {
They add a hook so that WndProc is actually called in WPF:
private void Window_Loaded(object sender, RoutedEventArgs e) { HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); if (source == null) return; source.AddHook(WndProc); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); if (source == null) return; source.RemoveHook(WndProc); }
Just FYI .. this still works, even if you don't focus:
private void WpfPillForm_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.DragMove(); }
Here's the Win32 API declarations, so you don't need to look for them:
[StructLayout(LayoutKind.Sequential)] public struct WINDOWPOS { public IntPtr hwnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public int flags; } [StructLayout(LayoutKind.Sequential)] struct RECT { public int left, top, right, bottom; } public static class MouseActivate { public const int MA_ACTIVATE = 1; public const int MA_ACTIVATEANDEAT = 2; public const int MA_NOACTIVATE = 3; public const int MA_NOACTIVATEANDEAT = 4; } public enum WindowLongFlags : int { GWL_EXSTYLE = -20, GWLP_HINSTANCE = -6, GWLP_HWNDPARENT = -8, GWL_ID = -12, GWL_STYLE = -16, GWL_USERDATA = -21, GWL_WNDPROC = -4, DWLP_USER = 0x8, DWLP_MSGRESULT = 0x0, DWLP_DLGPROC = 0x4 } public const int WM_MOVING = 0x0216; public const uint WS_EX_NOACTIVATE = 0x08000000, [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int GetWindowLong(IntPtr hwnd, int index);