using System;
using System.Runtime.InteropServices;
using Quicker.Public;
public class WindowStyler
{
// 使用 uint 明确处理大数值
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
// 十六进制显式声明 + uint 处理
private const int GWL_STYLE = -16;
private const uint WS_POPUP = 0x80000000u; // 原值 2147483648
private const uint WS_CAPTION = 0x00C00000u;
private const uint WS_THICKFRAME = 0x00040000u;
private const uint WS_SYSMENU = 0x00080000u;
private const uint SWP_FRAMECHANGED = 0x0020;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_NOOWNERZORDER = 0x0200;
public static void Exec(IStepContext context)
{
try
{
IntPtr hWnd = GetForegroundWindow();
if (hWnd == IntPtr.Zero)
throw new InvalidOperationException("无法获取前台窗口句柄");
// 获取当前样式(转换为 uint 处理位运算)
uint currentStyle = (uint)GetWindowLong(hWnd, GWL_STYLE);
// 移除边框和标题栏
uint newStyle = currentStyle & ~(WS_CAPTION | WS_THICKFRAME | WS_SYSMENU);
newStyle |= WS_POPUP; // 添加无边框样式
// 使用 unchecked 处理类型转换
int result = SetWindowLong(hWnd, GWL_STYLE, unchecked((int)newStyle));
if (result == 0)
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
// 强制窗口重绘
bool success = SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER);
if (!success)
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}
catch (Exception ex)
{
// 增强错误信息输出
throw new ApplicationException($"窗口样式修改失败: {ex.Message} [类型:{ex.GetType().Name}]");
}
}
}
修订版本 | 更新时间 | 更新说明 |
---|---|---|
0 | 8天10小时前 |