You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过标题查找窗口并使用PowerShell移除ControlBox

移除指定标题窗口的ControlBox按钮

PowerShell没有原生命令直接修改窗口的ControlBox设置,需要通过调用Windows API来实现,具体步骤如下:

1. 导入所需的Windows API函数

首先通过Add-Type把user32.dll里的关键函数导入PowerShell:

Add-Type @"
using System;
using System.Runtime.InteropServices;

public class WindowUtils {
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern int GetWindowLongPtr(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern int SetWindowLongPtr(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}
"@

2. 配置常量与目标窗口信息

定义窗口样式相关的常量,替换成你要操作的窗口标题:

# 窗口样式相关常量
$GWL_STYLE = -16          # 窗口样式的索引
$WS_SYSMENU = 0x00080000  # 包含ControlBox的样式标记
$SWP_NOMOVE = 0x0002      # 不改变窗口位置
$SWP_NOSIZE = 0x0001      # 不改变窗口大小
$SWP_FRAMECHANGED = 0x0020 # 强制刷新窗口框架

# 替换为你要修改的窗口标题
$targetWindowTitle = "你的目标窗口标题"

3. 查找窗口并修改样式

# 根据标题获取窗口句柄
$hWnd = [WindowUtils]::FindWindow($null, $targetWindowTitle)

if ($hWnd -eq [IntPtr]::Zero) {
    Write-Error "找不到标题为 '$targetWindowTitle' 的窗口"
    exit
}

# 获取当前窗口的样式
$currentStyle = [WindowUtils]::GetWindowLongPtr($hWnd, $GWL_STYLE)

# 移除WS_SYSMENU样式(该样式控制ControlBox和标题栏右键菜单)
$newStyle = $currentStyle -band (-bnot $WS_SYSMENU)

# 应用新的窗口样式
[WindowUtils]::SetWindowLongPtr($hWnd, $GWL_STYLE, $newStyle)

# 刷新窗口使修改生效
[WindowUtils]::SetWindowPos($hWnd, [IntPtr]::Zero, 0, 0, 0, 0, $SWP_NOMOVE -bor $SWP_NOSIZE -bor $SWP_FRAMECHANGED)

Write-Host "已成功移除窗口 '$targetWindowTitle' 的ControlBox按钮"

注意事项

  • 部分系统级窗口或受保护程序的窗口需要管理员权限运行PowerShell才能修改
  • 如果存在多个同名标题的窗口,FindWindow只会返回第一个匹配的窗口,若要处理多窗口,需要用EnumWindows函数扩展逻辑
  • 移除WS_SYSMENU后,不仅ControlBox会消失,窗口标题栏的右键菜单也会被移除

内容的提问来源于stack exchange,提问作者SixDegrees

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:26:03