From 829dbaafe7fae537d487f4b67cfe593c01ac5081 Mon Sep 17 00:00:00 2001
From: Jack251970 <1160210343@qq.com>
Date: Fri, 14 Feb 2025 16:12:15 +0800
Subject: [PATCH] Use CSWin32 for code quality
---
Flow.Launcher/Helper/WindowsInteropHelper.cs | 44 ++++++--------------
Flow.Launcher/NativeMethods.txt | 5 ++-
2 files changed, 17 insertions(+), 32 deletions(-)
diff --git a/Flow.Launcher/Helper/WindowsInteropHelper.cs b/Flow.Launcher/Helper/WindowsInteropHelper.cs
index eeb24af2e..4891bf41a 100644
--- a/Flow.Launcher/Helper/WindowsInteropHelper.cs
+++ b/Flow.Launcher/Helper/WindowsInteropHelper.cs
@@ -151,29 +151,11 @@ public class WindowsInteropHelper
#region Alt Tab
- private const int GWL_EXSTYLE = -20;
- private const int WS_EX_TOOLWINDOW = 0x00000080;
- private const int WS_EX_APPWINDOW = 0x00040000;
-
- [DllImport("user32.dll")]
- private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
-
- [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
- private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
-
- [DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
- private static extern int IntSetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
-
- [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
- private static extern void SetLastError(int dwErrorCode);
-
- private static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
+ private static IntPtr SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
{
- SetLastError(0); // Clear any existing error
+ PInvoke.SetLastError(WIN32_ERROR.NO_ERROR); // Clear any existing error
- if (IntPtr.Size == 4) return new IntPtr(IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong)));
-
- return IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
+ return PInvoke.SetWindowLong(hWnd, nIndex, dwNewLong);
}
private static int IntPtrToInt32(IntPtr intPtr)
@@ -182,44 +164,44 @@ public class WindowsInteropHelper
}
///
- /// Hide windows in the Alt+Tab window list
+ /// Hide windows in the Alt+Tab window list
///
/// To hide a window
public static void HideFromAltTab(Window window)
{
var helper = new WindowInteropHelper(window);
- var exStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
+ var exStyle = PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
// Add TOOLWINDOW style, remove APPWINDOW style
- exStyle = (exStyle | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW;
+ var newExStyle = ((uint)exStyle | (uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) & ~(uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
- SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
+ SetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
}
///
- /// Restore window display in the Alt+Tab window list.
+ /// Restore window display in the Alt+Tab window list.
///
/// To restore the displayed window
public static void ShowInAltTab(Window window)
{
var helper = new WindowInteropHelper(window);
- var exStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
+ var exStyle = PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
// Remove the TOOLWINDOW style and add the APPWINDOW style.
- exStyle = (exStyle & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW;
+ var newExStyle = ((uint)exStyle & ~(uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) | (uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
- SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
+ SetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
}
///
- /// To obtain the current overridden style of a window.
+ /// To obtain the current overridden style of a window.
///
/// To obtain the style dialog window
/// current extension style value
public static int GetCurrentWindowStyle(Window window)
{
var helper = new WindowInteropHelper(window);
- return GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
+ return PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
}
#endregion
diff --git a/Flow.Launcher/NativeMethods.txt b/Flow.Launcher/NativeMethods.txt
index 2b147c05f..88eeeca6e 100644
--- a/Flow.Launcher/NativeMethods.txt
+++ b/Flow.Launcher/NativeMethods.txt
@@ -14,4 +14,7 @@ FindWindowEx
WINDOW_STYLE
WM_ENTERSIZEMOVE
-WM_EXITSIZEMOVE
\ No newline at end of file
+WM_EXITSIZEMOVE
+
+SetLastError
+WINDOW_EX_STYLE
\ No newline at end of file