Use CSWin32 for code quality

This commit is contained in:
Jack251970 2025-02-14 16:12:15 +08:00
parent 5313229fb9
commit 829dbaafe7
2 changed files with 17 additions and 32 deletions

View file

@ -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
}
/// <summary>
/// Hide windows in the Alt+Tab window list
/// Hide windows in the Alt+Tab window list
/// </summary>
/// <param name="window">To hide a window</param>
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);
}
/// <summary>
/// Restore window display in the Alt+Tab window list.
/// Restore window display in the Alt+Tab window list.
/// </summary>
/// <param name="window">To restore the displayed window</param>
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);
}
/// <summary>
/// To obtain the current overridden style of a window.
/// To obtain the current overridden style of a window.
/// </summary>
/// <param name="window">To obtain the style dialog window</param>
/// <returns>current extension style value</returns>
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

View file

@ -14,4 +14,7 @@ FindWindowEx
WINDOW_STYLE
WM_ENTERSIZEMOVE
WM_EXITSIZEMOVE
WM_EXITSIZEMOVE
SetLastError
WINDOW_EX_STYLE