From 3fbebfc283bfe48ef56b2e03fe2b9b0057a8f5a1 Mon Sep 17 00:00:00 2001 From: Boris Makogonyuk Date: Thu, 8 Oct 2015 00:52:52 +0200 Subject: [PATCH] ~Changed how the fullscreen detection work. Checking by Topmost was a stupid idea. It will now check for the window to fill the whole screen area. --- Wox/Helper/WindowIntelopHelper.cs | 77 +++++++++++++++++++++++++------ Wox/MainWindow.xaml.cs | 10 ++-- 2 files changed, 67 insertions(+), 20 deletions(-) diff --git a/Wox/Helper/WindowIntelopHelper.cs b/Wox/Helper/WindowIntelopHelper.cs index caf7c2a89c..feccd8f6a4 100644 --- a/Wox/Helper/WindowIntelopHelper.cs +++ b/Wox/Helper/WindowIntelopHelper.cs @@ -1,19 +1,36 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Drawing; using System.Runtime.InteropServices; using System.Windows; +using System.Windows.Forms; +using System.Windows.Interop; namespace Wox.Helper { public class WindowIntelopHelper { private const int GWL_STYLE = -16; //WPF's Message code for Title Bar's Style - private const int GWL_EXSTYLE = -20; //Gets the exstyle of the window - private const int WS_EX_TOPMOST = 0x00000008; //Topmost flag private const int WS_SYSMENU = 0x80000; //WPF's Message code for System Menu + private static IntPtr _hwnd_shell; + private static IntPtr _hwnd_desktop; + //Accessors for shell and desktop handlers + //Will set the variables once and then will return them + private static IntPtr HWND_SHELL + { + get + { + return _hwnd_shell != null ? _hwnd_shell : _hwnd_shell = GetShellWindow(); + } + } + private static IntPtr HWND_DESKTOP + { + get + { + return _hwnd_desktop != null ? _hwnd_desktop : _hwnd_desktop = GetDesktopWindow(); + } + } + [DllImport("user32.dll", SetLastError = true)] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); @@ -23,23 +40,53 @@ namespace Wox.Helper [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); - /// - ///Checks if the foreground window is TopMost (even Wox) - /// - /// - public static bool IsForegroundWindowTopMost() + [DllImport("user32.dll")] + private static extern IntPtr GetDesktopWindow(); + + [DllImport("user32.dll")] + private static extern IntPtr GetShellWindow(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern int GetWindowRect(IntPtr hwnd, out RECT rc); + + public static bool IsWindowFullscreen() { - return (GetWindowLong(GetForegroundWindow(), GWL_EXSTYLE) & WS_EX_TOPMOST) == WS_EX_TOPMOST; + RECT foreWinBounds; + Rectangle screenBounds; + var hWnd = GetForegroundWindow(); + if (!hWnd.Equals(IntPtr.Zero)) + { + if (!(hWnd.Equals(HWND_DESKTOP) || hWnd.Equals(HWND_SHELL))) + { + GetWindowRect(hWnd, out foreWinBounds); + screenBounds = Screen.FromHandle(hWnd).Bounds; + if ((foreWinBounds.Bottom - foreWinBounds.Top) == screenBounds.Height && (foreWinBounds.Right - foreWinBounds.Left) == screenBounds.Width) + { + return true; + } + } + } + + return false; } /// - /// disable windows toolbar's control box - /// this will also disable system menu with Alt+Space hotkey + /// disable windows toolbar's control box + /// this will also disable system menu with Alt+Space hotkey /// public static void DisableControlBox(Window win) { - var hwnd = new System.Windows.Interop.WindowInteropHelper(win).Handle; + var hwnd = new WindowInteropHelper(win).Handle; SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU); } + + [StructLayout(LayoutKind.Sequential)] + public struct RECT + { + public int Left; + public int Top; + public int Right; + public int Bottom; + } } -} +} \ No newline at end of file diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 01d9f94f79..07a98f0872 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -362,11 +362,11 @@ namespace Wox /// private bool ShouldIgnoreHotkeys() { - - if (!IsVisible - && UserSettingStorage.Instance.IgnoreHotkeysOnTopMostFocus - && WindowIntelopHelper.IsForegroundWindowTopMost()) - return true; + //double if to omit calling win32 function + if (UserSettingStorage.Instance.IgnoreHotkeysOnTopMostFocus) + if(WindowIntelopHelper.IsWindowFullscreen()) + return true; + return false; }