From ebc72670c33d50484bbe4cba1ecc9a358c29fe5d Mon Sep 17 00:00:00 2001 From: AlexanderPro Date: Sat, 10 Feb 2024 11:08:59 +0300 Subject: [PATCH] Issues #5, Issues #8 Fixed detection "Normal" windows and Modal dialogs --- SmartContextMenu/Native/Constants.cs | 17 ++++++++++------- SmartContextMenu/Native/User32.cs | 4 ++-- SmartContextMenu/Utils/WindowUtils.cs | 13 +++++++++---- SmartContextMenu/Window.cs | 6 +++--- SmartContextMenu/WindowDetails.cs | 8 ++++---- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/SmartContextMenu/Native/Constants.cs b/SmartContextMenu/Native/Constants.cs index 73191f1..6c2674e 100644 --- a/SmartContextMenu/Native/Constants.cs +++ b/SmartContextMenu/Native/Constants.cs @@ -47,13 +47,16 @@ static class Constants public const int DWL_USER = 8; // WindowStyle - public const int WS_MAXIMIZEBOX = 0x00010000; - public const int WS_MINIMIZEBOX = 0x00020000; - public const int WS_EX_LAYERED = 0x00080000; - public const int WS_EX_TOPMOST = 0x00000008; - public const int WS_EX_TOOLWINDOW = 0x00000080; - public const int WS_EX_TRANSPARENT = 0x20; - public const int WS_EX_NOACTIVATE = 0x08000000; + public const long WS_CAPTION = 0x00C00000L; + public const long WS_SYSMENU = 0x00080000L; + public const long WS_POPUP = 0x80000000L; + public const long WS_MAXIMIZEBOX = 0x00010000L; + public const long WS_MINIMIZEBOX = 0x00020000L; + public const long WS_EX_LAYERED = 0x00080000L; + public const long WS_EX_TOPMOST = 0x00000008L; + public const long WS_EX_TOOLWINDOW = 0x00000080L; + public const long WS_EX_TRANSPARENT = 0x00000020L; + public const long WS_EX_NOACTIVATE = 0x08000000L; // Window Messages public const int WM_GETICON = 0x7F; diff --git a/SmartContextMenu/Native/User32.cs b/SmartContextMenu/Native/User32.cs index a3f4300..bd83111 100644 --- a/SmartContextMenu/Native/User32.cs +++ b/SmartContextMenu/Native/User32.cs @@ -65,10 +65,10 @@ static class User32 public static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl); [DllImport("user32.dll")] - public static extern int SetWindowLong(IntPtr handle, int nIndex, int dwNewLong); + public static extern int SetWindowLong(IntPtr handle, int nIndex, long dwNewLong); [DllImport("user32.dll")] - public static extern int GetWindowLong(IntPtr handle, int nIndex); + public static extern long GetWindowLong(IntPtr handle, int nIndex); [DllImport("user32.dll")] public static extern int GetClassLong(IntPtr handle, int nIndex); diff --git a/SmartContextMenu/Utils/WindowUtils.cs b/SmartContextMenu/Utils/WindowUtils.cs index e6d2f42..fbbc6de 100644 --- a/SmartContextMenu/Utils/WindowUtils.cs +++ b/SmartContextMenu/Utils/WindowUtils.cs @@ -30,12 +30,17 @@ public static Bitmap PrintWindow(IntPtr hWnd) public static IntPtr GetParentWindow(IntPtr hWnd) { - IntPtr parentHwnd; - var resultHwnd = hWnd; - while ((parentHwnd = GetParent(resultHwnd)) != IntPtr.Zero) + IntPtr resultHwnd; + var parentHwnd = hWnd; + do { resultHwnd = parentHwnd; - } + var windowStyle = GetWindowLong(resultHwnd, GWL_STYLE); + if ((windowStyle & WS_CAPTION) != 0 || (windowStyle & WS_SYSMENU) != 0 || (windowStyle & WS_POPUP) != 0) + { + return resultHwnd; + } + } while ((parentHwnd = GetParent(resultHwnd)) != IntPtr.Zero); return resultHwnd; } diff --git a/SmartContextMenu/Window.cs b/SmartContextMenu/Window.cs index 929a4f1..ec54c7b 100644 --- a/SmartContextMenu/Window.cs +++ b/SmartContextMenu/Window.cs @@ -93,11 +93,11 @@ public int Transparency { get { - int style = GetWindowLong(Handle, GWL_EXSTYLE); - bool isLayeredWindow = (style & WS_EX_LAYERED) == WS_EX_LAYERED; + var style = GetWindowLong(Handle, GWL_EXSTYLE); + var isLayeredWindow = (style & WS_EX_LAYERED) == WS_EX_LAYERED; if (!isLayeredWindow) return 0; GetLayeredWindowAttributes(Handle, out _, out var alpha, out _); - int transparency = 100 - (int)Math.Round(100 * alpha / 255f, MidpointRounding.AwayFromZero); + var transparency = 100 - (int)Math.Round(100 * alpha / 255f, MidpointRounding.AwayFromZero); return transparency; } } diff --git a/SmartContextMenu/WindowDetails.cs b/SmartContextMenu/WindowDetails.cs index 39609d7..616486d 100644 --- a/SmartContextMenu/WindowDetails.cs +++ b/SmartContextMenu/WindowDetails.cs @@ -36,11 +36,11 @@ class WindowDetails public int DWL_DLGPROC { get; set; } - public int GWL_STYLE { get; set; } + public long GWL_STYLE { get; set; } public int GCL_STYLE { get; set; } - public int GWL_EXSTYLE { get; set; } + public long GWL_EXSTYLE { get; set; } public uint WindowInfoExStyle { get; set; } @@ -48,9 +48,9 @@ class WindowDetails public bool LWA_COLORKEY { get; set; } - public int GWL_ID { get; set; } + public long GWL_ID { get; set; } - public int GWL_USERDATA { get; set; } + public long GWL_USERDATA { get; set; } public int DWL_USER { get; set; }