From bd9b66afa4cd851c73e16c0e3426a74ceeb9f5a9 Mon Sep 17 00:00:00 2001 From: Yu Leng Date: Tue, 9 Dec 2025 10:52:53 +0800 Subject: [PATCH] Refactor to use nint for window handles and P/Invoke Replaces IntPtr with nint for window handles and native API calls in WindowHelper, removing 32/64-bit conditional logic. Cleans up unused minimize/restore methods. Updates TrayIconService constructor usage in App.xaml.cs. --- .../PowerDisplay/Helpers/TrayIconService.cs | 1 - .../PowerDisplay/Helpers/WindowHelper.cs | 90 ++++--------------- .../PowerDisplay/PowerDisplayXAML/App.xaml.cs | 1 - 3 files changed, 19 insertions(+), 73 deletions(-) diff --git a/src/modules/powerdisplay/PowerDisplay/Helpers/TrayIconService.cs b/src/modules/powerdisplay/PowerDisplay/Helpers/TrayIconService.cs index 1ea5a15fff..e33a3cbafb 100644 --- a/src/modules/powerdisplay/PowerDisplay/Helpers/TrayIconService.cs +++ b/src/modules/powerdisplay/PowerDisplay/Helpers/TrayIconService.cs @@ -51,7 +51,6 @@ namespace PowerDisplay.Helpers public TrayIconService( ISettingsUtils settingsUtils, - Action showWindowAction, Action toggleWindowAction, Action exitAction, Action openSettingsAction) diff --git a/src/modules/powerdisplay/PowerDisplay/Helpers/WindowHelper.cs b/src/modules/powerdisplay/PowerDisplay/Helpers/WindowHelper.cs index 9f9f905089..ce3ce1548b 100644 --- a/src/modules/powerdisplay/PowerDisplay/Helpers/WindowHelper.cs +++ b/src/modules/powerdisplay/PowerDisplay/Helpers/WindowHelper.cs @@ -31,37 +31,25 @@ namespace PowerDisplay.Helpers private const uint SwpNosize = 0x0001; private const uint SwpNomove = 0x0002; private const uint SwpFramechanged = 0x0020; - private static readonly IntPtr HwndTopmost = new IntPtr(-1); - private static readonly IntPtr HwndNotopmost = new IntPtr(-2); + private const nint HwndTopmost = -1; + private const nint HwndNotopmost = -2; // ShowWindow commands private const int SwHide = 0; private const int SwShow = 5; - private const int SwMinimize = 6; - private const int SwRestore = 9; - // P/Invoke declarations -#if WIN64 + // P/Invoke declarations (64-bit only - PowerToys only builds for x64/ARM64) [LibraryImport("user32.dll", EntryPoint = "GetWindowLongPtrW")] - private static partial IntPtr GetWindowLong(IntPtr hWnd, int nIndex); -#else - [LibraryImport("user32.dll", EntryPoint = "GetWindowLongW")] - private static partial int GetWindowLong(IntPtr hWnd, int nIndex); -#endif + private static partial nint GetWindowLong(nint hWnd, int nIndex); -#if WIN64 [LibraryImport("user32.dll", EntryPoint = "SetWindowLongPtrW")] - private static partial IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); -#else - [LibraryImport("user32.dll", EntryPoint = "SetWindowLongW")] - private static partial int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); -#endif + private static partial nint SetWindowLong(nint hWnd, int nIndex, nint dwNewLong); [LibraryImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static partial bool SetWindowPos( - IntPtr hWnd, - IntPtr hWndInsertAfter, + nint hWnd, + nint hWndInsertAfter, int x, int y, int cx, @@ -70,16 +58,16 @@ namespace PowerDisplay.Helpers [LibraryImport("user32.dll", EntryPoint = "ShowWindow")] [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool ShowWindowNative(IntPtr hWnd, int nCmdShow); + private static partial bool ShowWindowNative(nint hWnd, int nCmdShow); [LibraryImport("user32.dll", EntryPoint = "IsWindowVisible")] [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool IsWindowVisibleNative(IntPtr hWnd); + private static partial bool IsWindowVisibleNative(nint hWnd); /// /// Check if window is visible /// - public static bool IsWindowVisible(IntPtr hWnd) + public static bool IsWindowVisible(nint hWnd) { return IsWindowVisibleNative(hWnd); } @@ -87,14 +75,10 @@ namespace PowerDisplay.Helpers /// /// Disable window moving and resizing functionality /// - public static void DisableWindowMovingAndResizing(IntPtr hWnd) + public static void DisableWindowMovingAndResizing(nint hWnd) { // Get current window style -#if WIN64 - int style = (int)GetWindowLong(hWnd, GwlStyle); -#else - int style = GetWindowLong(hWnd, GwlStyle); -#endif + nint style = GetWindowLong(hWnd, GwlStyle); // Remove resizable borders, title bar, and system menu style &= ~WsThickframe; @@ -104,32 +88,20 @@ namespace PowerDisplay.Helpers style &= ~WsSysmenu; // Remove system menu // Set new window style -#if WIN64 - _ = SetWindowLong(hWnd, GwlStyle, new IntPtr(style)); -#else _ = SetWindowLong(hWnd, GwlStyle, style); -#endif // Get extended style and remove related borders -#if WIN64 - int exStyle = (int)GetWindowLong(hWnd, GwlExstyle); -#else - int exStyle = GetWindowLong(hWnd, GwlExstyle); -#endif + nint exStyle = GetWindowLong(hWnd, GwlExstyle); exStyle &= ~WsExDlgmodalframe; exStyle &= ~WsExWindowedge; exStyle &= ~WsExClientedge; exStyle &= ~WsExStaticedge; -#if WIN64 - _ = SetWindowLong(hWnd, GwlExstyle, new IntPtr(exStyle)); -#else _ = SetWindowLong(hWnd, GwlExstyle, exStyle); -#endif // Refresh window frame SetWindowPos( hWnd, - IntPtr.Zero, + 0, 0, 0, 0, @@ -140,7 +112,7 @@ namespace PowerDisplay.Helpers /// /// Set whether window is topmost /// - public static void SetWindowTopmost(IntPtr hWnd, bool topmost) + public static void SetWindowTopmost(nint hWnd, bool topmost) { SetWindowPos( hWnd, @@ -155,53 +127,29 @@ namespace PowerDisplay.Helpers /// /// Show or hide window /// - public static void ShowWindow(IntPtr hWnd, bool show) + public static void ShowWindow(nint hWnd, bool show) { ShowWindowNative(hWnd, show ? SwShow : SwHide); } - /// - /// Minimize window - /// - public static void MinimizeWindow(IntPtr hWnd) - { - ShowWindowNative(hWnd, SwMinimize); - } - - /// - /// Restore window - /// - public static void RestoreWindow(IntPtr hWnd) - { - ShowWindowNative(hWnd, SwRestore); - } - /// /// Hide window from taskbar /// - public static void HideFromTaskbar(IntPtr hWnd) + public static void HideFromTaskbar(nint hWnd) { // Get current extended style -#if WIN64 - int exStyle = (int)GetWindowLong(hWnd, GwlExstyle); -#else - int exStyle = GetWindowLong(hWnd, GwlExstyle); -#endif + nint exStyle = GetWindowLong(hWnd, GwlExstyle); // Add WS_EX_TOOLWINDOW style to hide window from taskbar exStyle |= WsExToolwindow; // Set new extended style -#if WIN64 - _ = SetWindowLong(hWnd, GwlExstyle, new IntPtr(exStyle)); -#else _ = SetWindowLong(hWnd, GwlExstyle, exStyle); -#endif // Refresh window frame SetWindowPos( hWnd, - IntPtr.Zero, + 0, 0, 0, 0, diff --git a/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/App.xaml.cs b/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/App.xaml.cs index 0e45f40269..ad7bc1f5ee 100644 --- a/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/App.xaml.cs +++ b/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/App.xaml.cs @@ -134,7 +134,6 @@ namespace PowerDisplay // Initialize tray icon service _trayIconService = new TrayIconService( _settingsUtils, - ShowMainWindow, ToggleMainWindow, () => Environment.Exit(0), OpenSettings);