diff --git a/src/ManagedShell.AppBar/FullScreenHelper.cs b/src/ManagedShell.AppBar/FullScreenHelper.cs index fb67b6ab..0a8eb87f 100644 --- a/src/ManagedShell.AppBar/FullScreenHelper.cs +++ b/src/ManagedShell.AppBar/FullScreenHelper.cs @@ -141,7 +141,7 @@ private void updateFullScreenWindows() else { // Still full screen but no longer active - if ((GetWindowLong(hWnd, GWL_EXSTYLE) & (int)ExtendedWindowStyles.WS_EX_TOPMOST) == (int)ExtendedWindowStyles.WS_EX_TOPMOST) + if (((int)GetWindowLongPtr(hWnd, WindowLongFlags.GWL_EXSTYLE) & (int)ExtendedWindowStyles.WS_EX_TOPMOST) == (int)ExtendedWindowStyles.WS_EX_TOPMOST) { // If the new foreground window is a topmost window, don't consider this full-screen app inactive continue; @@ -265,7 +265,7 @@ private FullScreenApp getFullScreenApp(IntPtr hWnd, bool fromTasksService = fals private Rect GetEffectiveWindowRect(IntPtr hWnd) { - int style = GetWindowLong(hWnd, GWL_STYLE); + int style = (int)GetWindowLongPtr(hWnd, WindowLongFlags.GWL_STYLE); Rect rect; if ((((int)WindowStyles.WS_CAPTION | (int)WindowStyles.WS_THICKFRAME) & style) == ((int)WindowStyles.WS_CAPTION | (int)WindowStyles.WS_THICKFRAME) || diff --git a/src/ManagedShell.Common/Helpers/WindowHelper.cs b/src/ManagedShell.Common/Helpers/WindowHelper.cs index df8835ed..7d60306a 100644 --- a/src/ManagedShell.Common/Helpers/WindowHelper.cs +++ b/src/ManagedShell.Common/Helpers/WindowHelper.cs @@ -103,7 +103,7 @@ public static IntPtr GetLowestDesktopChildHwnd() public static void HideWindowFromTasks(IntPtr hWnd) { - int style = GetWindowLong(hWnd, GWL_EXSTYLE) & ~(int)ExtendedWindowStyles.WS_EX_APPWINDOW; + int style = (int)GetWindowLongPtr(hWnd, WindowLongFlags.GWL_EXSTYLE) & ~(int)ExtendedWindowStyles.WS_EX_APPWINDOW; if (EnvironmentHelper.IsWindows11OrBetter) { // If the window has a Hidden Window owner, set the owner as a tool window instead so that we still receive WM_DPICHANGED on Windows 11. @@ -121,7 +121,7 @@ public static void HideWindowFromTasks(IntPtr hWnd) GetWindowText(hwndOwner, titleBuilder, TITLE_LENGTH + 1); if (titleBuilder.ToString() == "Hidden Window") { - SetWindowLong(hwndOwner, GWL_EXSTYLE, GetWindowLong(hwndOwner, GWL_EXSTYLE) | (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW); + SetWindowLongPtr(hwndOwner, WindowLongFlags.GWL_EXSTYLE, (IntPtr)((int)GetWindowLongPtr(hwndOwner, WindowLongFlags.GWL_EXSTYLE) | (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW)); } } } @@ -129,7 +129,7 @@ public static void HideWindowFromTasks(IntPtr hWnd) { style |= (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW; } - SetWindowLong(hWnd, GWL_EXSTYLE, style); + SetWindowLongPtr(hWnd, WindowLongFlags.GWL_EXSTYLE, (IntPtr)style); ExcludeWindowFromPeek(hWnd); } diff --git a/src/ManagedShell.Common/SupportingClasses/ShellWindow.cs b/src/ManagedShell.Common/SupportingClasses/ShellWindow.cs index 673cf624..7d90dea3 100644 --- a/src/ManagedShell.Common/SupportingClasses/ShellWindow.cs +++ b/src/ManagedShell.Common/SupportingClasses/ShellWindow.cs @@ -24,9 +24,9 @@ public ShellWindow() CreateHandle(cp); MessageReceived += ShellWndProc; - NativeMethods.SetWindowLong(Handle, NativeMethods.GWL_EXSTYLE, - NativeMethods.GetWindowLong(Handle, NativeMethods.GWL_EXSTYLE) & - ~(int)NativeMethods.ExtendedWindowStyles.WS_EX_NOACTIVATE); + NativeMethods.SetWindowLongPtr(Handle, NativeMethods.WindowLongFlags.GWL_EXSTYLE, + (IntPtr)((int)NativeMethods.GetWindowLongPtr(Handle, NativeMethods.WindowLongFlags.GWL_EXSTYLE) & + ~(int)NativeMethods.ExtendedWindowStyles.WS_EX_NOACTIVATE)); if (NativeMethods.SetShellWindow(Handle) == 1) { diff --git a/src/ManagedShell.Interop/NativeMethods.User32.cs b/src/ManagedShell.Interop/NativeMethods.User32.cs index 97078052..85197685 100644 --- a/src/ManagedShell.Interop/NativeMethods.User32.cs +++ b/src/ManagedShell.Interop/NativeMethods.User32.cs @@ -214,8 +214,20 @@ public enum WindowStyles : uint } public const int MA_NOACTIVATE = 0x0003; - public const int GWL_STYLE = -16; - public const int GWL_EXSTYLE = -20; + + 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 HSHELL_HIGHBIT = 0x8000; @@ -267,7 +279,24 @@ public enum MinimizedMetricsArrangement public delegate IntPtr WndProcDelegate(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); [DllImport(User32_DllName, SetLastError = true)] - public static extern int GetWindowLong(IntPtr hWnd, int nIndex); + private static extern int GetWindowLong(IntPtr hWnd, int nIndex); + + [DllImport(User32_DllName, EntryPoint = "GetWindowLongPtr", SetLastError = true)] + private static extern IntPtr GetWindowLong64(IntPtr hWnd, int nIndex); + + public static IntPtr GetWindowLongPtr(IntPtr hWnd, WindowLongFlags nIndex) + { + return GetWindowLongPtr(hWnd, (int)nIndex); + } + + // This static method is required because Win32 does not support + // GetWindowLongPtr directly + public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex) + { + if (IntPtr.Size == 8) + return GetWindowLong64(hWnd, nIndex); + return (IntPtr)GetWindowLong(hWnd, nIndex); + } [return: MarshalAs(UnmanagedType.Bool)] [DllImport(User32_DllName, SetLastError = true)] @@ -1704,7 +1733,24 @@ public enum WindowShowStyle : uint public static extern bool IsWindowEnabled(IntPtr hWnd); [DllImport(User32_DllName)] - public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); + private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); + + [DllImport(User32_DllName, EntryPoint = "SetWindowLongPtr")] + private static extern IntPtr SetWindowLong64(IntPtr hWnd, int nIndex, IntPtr dwNewLong); + + public static IntPtr SetWindowLongPtr(IntPtr hWnd, WindowLongFlags nIndex, IntPtr dwNewLong) + { + return SetWindowLongPtr(hWnd, (int)nIndex, dwNewLong); + } + + // This static method is required because Win32 does not support + // SetWindowLongPtr directly + public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong) + { + if (IntPtr.Size == 8) + return SetWindowLong64(hWnd, nIndex, dwNewLong); + return (IntPtr)SetWindowLong(hWnd, nIndex, dwNewLong.ToInt32()); + } [DllImport(User32_DllName, SetLastError = true)] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); diff --git a/src/ManagedShell.WindowsTasks/ApplicationWindow.cs b/src/ManagedShell.WindowsTasks/ApplicationWindow.cs index 9baade44..8a8c74a7 100644 --- a/src/ManagedShell.WindowsTasks/ApplicationWindow.cs +++ b/src/ManagedShell.WindowsTasks/ApplicationWindow.cs @@ -338,7 +338,7 @@ public int WindowStyles { get { - return NativeMethods.GetWindowLong(Handle, NativeMethods.GWL_STYLE); + return (int)NativeMethods.GetWindowLongPtr(Handle, NativeMethods.WindowLongFlags.GWL_STYLE); } } @@ -346,7 +346,7 @@ public int ExtendedWindowStyles { get { - return NativeMethods.GetWindowLong(Handle, NativeMethods.GWL_EXSTYLE); + return (int)NativeMethods.GetWindowLongPtr(Handle, NativeMethods.WindowLongFlags.GWL_EXSTYLE); } } diff --git a/src/ManagedShell.WindowsTray/TrayService.cs b/src/ManagedShell.WindowsTray/TrayService.cs index de1e0e35..d5a95c7d 100644 --- a/src/ManagedShell.WindowsTray/TrayService.cs +++ b/src/ManagedShell.WindowsTray/TrayService.cs @@ -239,9 +239,9 @@ private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam) if ((wndPos.flags & SetWindowPosFlags.SWP_SHOWWINDOW) != 0) { - SetWindowLong(HwndTray, GWL_STYLE, - GetWindowLong(HwndTray, GWL_STYLE) & - ~(int)WindowStyles.WS_VISIBLE); + SetWindowLongPtr(HwndTray, WindowLongFlags.GWL_STYLE, + (IntPtr)((int)GetWindowLongPtr(HwndTray, WindowLongFlags.GWL_STYLE) & + ~(int)WindowStyles.WS_VISIBLE)); ShellLogger.Debug($"TrayService: {TrayWndClass} became visible; hiding"); }