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.
This commit is contained in:
Yu Leng
2025-12-09 10:52:53 +08:00
parent dd02ed54e0
commit bd9b66afa4
3 changed files with 19 additions and 73 deletions

View File

@@ -51,7 +51,6 @@ namespace PowerDisplay.Helpers
public TrayIconService( public TrayIconService(
ISettingsUtils settingsUtils, ISettingsUtils settingsUtils,
Action showWindowAction,
Action toggleWindowAction, Action toggleWindowAction,
Action exitAction, Action exitAction,
Action openSettingsAction) Action openSettingsAction)

View File

@@ -31,37 +31,25 @@ namespace PowerDisplay.Helpers
private const uint SwpNosize = 0x0001; private const uint SwpNosize = 0x0001;
private const uint SwpNomove = 0x0002; private const uint SwpNomove = 0x0002;
private const uint SwpFramechanged = 0x0020; private const uint SwpFramechanged = 0x0020;
private static readonly IntPtr HwndTopmost = new IntPtr(-1); private const nint HwndTopmost = -1;
private static readonly IntPtr HwndNotopmost = new IntPtr(-2); private const nint HwndNotopmost = -2;
// ShowWindow commands // ShowWindow commands
private const int SwHide = 0; private const int SwHide = 0;
private const int SwShow = 5; private const int SwShow = 5;
private const int SwMinimize = 6;
private const int SwRestore = 9;
// P/Invoke declarations // P/Invoke declarations (64-bit only - PowerToys only builds for x64/ARM64)
#if WIN64
[LibraryImport("user32.dll", EntryPoint = "GetWindowLongPtrW")] [LibraryImport("user32.dll", EntryPoint = "GetWindowLongPtrW")]
private static partial IntPtr GetWindowLong(IntPtr hWnd, int nIndex); private static partial nint GetWindowLong(nint hWnd, int nIndex);
#else
[LibraryImport("user32.dll", EntryPoint = "GetWindowLongW")]
private static partial int GetWindowLong(IntPtr hWnd, int nIndex);
#endif
#if WIN64
[LibraryImport("user32.dll", EntryPoint = "SetWindowLongPtrW")] [LibraryImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
private static partial IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); private static partial nint SetWindowLong(nint hWnd, int nIndex, nint dwNewLong);
#else
[LibraryImport("user32.dll", EntryPoint = "SetWindowLongW")]
private static partial int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
#endif
[LibraryImport("user32.dll")] [LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetWindowPos( private static partial bool SetWindowPos(
IntPtr hWnd, nint hWnd,
IntPtr hWndInsertAfter, nint hWndInsertAfter,
int x, int x,
int y, int y,
int cx, int cx,
@@ -70,16 +58,16 @@ namespace PowerDisplay.Helpers
[LibraryImport("user32.dll", EntryPoint = "ShowWindow")] [LibraryImport("user32.dll", EntryPoint = "ShowWindow")]
[return: MarshalAs(UnmanagedType.Bool)] [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")] [LibraryImport("user32.dll", EntryPoint = "IsWindowVisible")]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
private static partial bool IsWindowVisibleNative(IntPtr hWnd); private static partial bool IsWindowVisibleNative(nint hWnd);
/// <summary> /// <summary>
/// Check if window is visible /// Check if window is visible
/// </summary> /// </summary>
public static bool IsWindowVisible(IntPtr hWnd) public static bool IsWindowVisible(nint hWnd)
{ {
return IsWindowVisibleNative(hWnd); return IsWindowVisibleNative(hWnd);
} }
@@ -87,14 +75,10 @@ namespace PowerDisplay.Helpers
/// <summary> /// <summary>
/// Disable window moving and resizing functionality /// Disable window moving and resizing functionality
/// </summary> /// </summary>
public static void DisableWindowMovingAndResizing(IntPtr hWnd) public static void DisableWindowMovingAndResizing(nint hWnd)
{ {
// Get current window style // Get current window style
#if WIN64 nint style = GetWindowLong(hWnd, GwlStyle);
int style = (int)GetWindowLong(hWnd, GwlStyle);
#else
int style = GetWindowLong(hWnd, GwlStyle);
#endif
// Remove resizable borders, title bar, and system menu // Remove resizable borders, title bar, and system menu
style &= ~WsThickframe; style &= ~WsThickframe;
@@ -104,32 +88,20 @@ namespace PowerDisplay.Helpers
style &= ~WsSysmenu; // Remove system menu style &= ~WsSysmenu; // Remove system menu
// Set new window style // Set new window style
#if WIN64
_ = SetWindowLong(hWnd, GwlStyle, new IntPtr(style));
#else
_ = SetWindowLong(hWnd, GwlStyle, style); _ = SetWindowLong(hWnd, GwlStyle, style);
#endif
// Get extended style and remove related borders // Get extended style and remove related borders
#if WIN64 nint exStyle = GetWindowLong(hWnd, GwlExstyle);
int exStyle = (int)GetWindowLong(hWnd, GwlExstyle);
#else
int exStyle = GetWindowLong(hWnd, GwlExstyle);
#endif
exStyle &= ~WsExDlgmodalframe; exStyle &= ~WsExDlgmodalframe;
exStyle &= ~WsExWindowedge; exStyle &= ~WsExWindowedge;
exStyle &= ~WsExClientedge; exStyle &= ~WsExClientedge;
exStyle &= ~WsExStaticedge; exStyle &= ~WsExStaticedge;
#if WIN64
_ = SetWindowLong(hWnd, GwlExstyle, new IntPtr(exStyle));
#else
_ = SetWindowLong(hWnd, GwlExstyle, exStyle); _ = SetWindowLong(hWnd, GwlExstyle, exStyle);
#endif
// Refresh window frame // Refresh window frame
SetWindowPos( SetWindowPos(
hWnd, hWnd,
IntPtr.Zero, 0,
0, 0,
0, 0,
0, 0,
@@ -140,7 +112,7 @@ namespace PowerDisplay.Helpers
/// <summary> /// <summary>
/// Set whether window is topmost /// Set whether window is topmost
/// </summary> /// </summary>
public static void SetWindowTopmost(IntPtr hWnd, bool topmost) public static void SetWindowTopmost(nint hWnd, bool topmost)
{ {
SetWindowPos( SetWindowPos(
hWnd, hWnd,
@@ -155,53 +127,29 @@ namespace PowerDisplay.Helpers
/// <summary> /// <summary>
/// Show or hide window /// Show or hide window
/// </summary> /// </summary>
public static void ShowWindow(IntPtr hWnd, bool show) public static void ShowWindow(nint hWnd, bool show)
{ {
ShowWindowNative(hWnd, show ? SwShow : SwHide); ShowWindowNative(hWnd, show ? SwShow : SwHide);
} }
/// <summary>
/// Minimize window
/// </summary>
public static void MinimizeWindow(IntPtr hWnd)
{
ShowWindowNative(hWnd, SwMinimize);
}
/// <summary>
/// Restore window
/// </summary>
public static void RestoreWindow(IntPtr hWnd)
{
ShowWindowNative(hWnd, SwRestore);
}
/// <summary> /// <summary>
/// Hide window from taskbar /// Hide window from taskbar
/// </summary> /// </summary>
public static void HideFromTaskbar(IntPtr hWnd) public static void HideFromTaskbar(nint hWnd)
{ {
// Get current extended style // Get current extended style
#if WIN64 nint exStyle = GetWindowLong(hWnd, GwlExstyle);
int exStyle = (int)GetWindowLong(hWnd, GwlExstyle);
#else
int exStyle = GetWindowLong(hWnd, GwlExstyle);
#endif
// Add WS_EX_TOOLWINDOW style to hide window from taskbar // Add WS_EX_TOOLWINDOW style to hide window from taskbar
exStyle |= WsExToolwindow; exStyle |= WsExToolwindow;
// Set new extended style // Set new extended style
#if WIN64
_ = SetWindowLong(hWnd, GwlExstyle, new IntPtr(exStyle));
#else
_ = SetWindowLong(hWnd, GwlExstyle, exStyle); _ = SetWindowLong(hWnd, GwlExstyle, exStyle);
#endif
// Refresh window frame // Refresh window frame
SetWindowPos( SetWindowPos(
hWnd, hWnd,
IntPtr.Zero, 0,
0, 0,
0, 0,
0, 0,

View File

@@ -134,7 +134,6 @@ namespace PowerDisplay
// Initialize tray icon service // Initialize tray icon service
_trayIconService = new TrayIconService( _trayIconService = new TrayIconService(
_settingsUtils, _settingsUtils,
ShowMainWindow,
ToggleMainWindow, ToggleMainWindow,
() => Environment.Exit(0), () => Environment.Exit(0),
OpenSettings); OpenSettings);