Skip to content

About

Resize and center any Windows window to an exact pixel size (e.g. 1280×720). A single PowerShell script.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

WindowResize

把任意 Windows 窗口精确调整到指定像素尺寸并居中(或放到指定坐标)。单个 PowerShell 脚本,无需安装任何东西。

.\Set-WindowRect.ps1 -ProcessName notepad                 # 记事本 → 1280×720,居中
.\Set-WindowRect.ps1 -ProcessName pcsx2-qt -ClientArea    # 画面区域正好 1280×720,居中

为什么不直接拖?

手动拖拽很难精确到像素,而且即便用代码调整,也有几个容易踩的坑,本脚本都处理了:

问题 说明 本脚本的处理
外框还是画面? “1280×720” 可能指整个窗口,也可能指去掉标题栏和边框后的内容区。外框 1280×720 时,内容区通常只有约 1277×680 默认按外框;加 -ClientArea 按内容区
隐形边框 Win10/11 窗口四周有约 7 像素看不见的缩放边框,系统报告的窗口尺寸会比肉眼看到的大,按它居中会偏 读取 DWM 的真实可见外框来计算
系统缩放 125%/150% 缩放下,不同程序看到的坐标单位不同 统一使用物理像素(Per-Monitor DPI Aware V2)
任务栏 按整块屏幕居中会被任务栏遮住一部分 默认在工作区(不含任务栏)内居中,-FullMonitor 可改为整屏
最大化窗口 最大化状态下无法设置尺寸 自动先还原
菜单栏换行等 改变宽度可能让菜单栏折行,内容区高度随之变化 自动复测并校正,最多 3 轮

要求

  • Windows 10 1703 及以上 / Windows 11
  • Windows PowerShell 5.1(系统自带)或 PowerShell 7+

用法

下载 Set-WindowRect.ps1,在 PowerShell 里运行。

选择目标窗口

# 按进程名(支持通配符,可带或不带 .exe)
.\Set-WindowRect.ps1 -ProcessName notepad

# 按窗口标题(不含通配符时按“包含”匹配)
.\Set-WindowRect.ps1 -Title "Chrome"

# 按窗口句柄(匹配到多个窗口时,脚本会列出各自的句柄)
.\Set-WindowRect.ps1 -Handle 132456

# 什么都不指定:3 秒内点一下目标窗口,作用于当前前台窗口
.\Set-WindowRect.ps1

不知道进程名或标题?先列出来看看:

.\Set-WindowRect.ps1 -List
.\Set-WindowRect.ps1 -List -ProcessName chrome
  Handle Process  外框      客户区    位置       Title
  ------ -------  ----      ------    ----       -----
  132456 Notepad  1280×720  1277×680  (320, 150) 无标题 - 记事本

尺寸与位置

# 自定义尺寸,居中
.\Set-WindowRect.ps1 -ProcessName notepad -Width 1600 -Height 900

# 内容区正好 1920×1080(游戏、模拟器、录屏)
.\Set-WindowRect.ps1 -ProcessName obs64 -Width 1920 -Height 1080 -ClientArea

# 放到指定坐标(可见外框左上角),只给 -X 或 -Y 时另一个方向仍居中
.\Set-WindowRect.ps1 -ProcessName notepad -X 0 -Y 0

# 相对整块屏幕居中(包括任务栏区域)
.\Set-WindowRect.ps1 -ProcessName notepad -FullMonitor

窗口会在它当前所在的显示器上居中。

参数一览

参数 默认值 说明
-ProcessName 按进程名匹配,支持通配符
-Title 按窗口标题匹配
-Handle 按窗口句柄指定
-Width / -Height 1280 / 720 目标尺寸(像素)
-ClientArea 关 尺寸作用于内容区而不是外框
-X / -Y 居中 可见外框左上角坐标
-FullMonitor 关 相对整个屏幕而不是工作区居中
-Delay 3 未指定目标时,等待你切换窗口的秒数
-List 列出窗口而不调整

运行后会输出实际结果,例如:

可见外框: 1282×760  位置 (319, 130)
客户区  : 1280×720

提示“禁止运行脚本”?

Windows 默认不允许运行下载的脚本。任选一种:

# 只对这一次生效
powershell -ExecutionPolicy Bypass -File .\Set-WindowRect.ps1 -ProcessName notepad

# 或者解除这个文件的“来自网络”标记,并允许当前用户运行本地脚本
Unblock-File .\Set-WindowRect.ps1
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

做成快捷方式

新建桌面快捷方式,目标填:

powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "D:\路径\Set-WindowRect.ps1" -ProcessName notepad -ClientArea

不带 -ProcessName 时,双击快捷方式后 3 秒内点一下目标窗口即可。还可以在快捷方式属性里设置快捷键。

已知限制

  • 只能作用于每个进程的主窗口;同一进程有多个顶层窗口时,用 -List 查句柄后用 -Handle 指定。
  • 有最小/最大尺寸限制的窗口、固定大小的对话框、全屏独占模式的游戏可能不接受目标尺寸,脚本会给出警告。
  • 不支持高 DPI 的老程序会被系统放大显示,它自己认为的 1280×720 在屏幕上会更大;本脚本以屏幕上的物理像素为准。
  • 以管理员身份运行的窗口,需要在管理员 PowerShell 中运行脚本才能调整。

许可证

MIT

About

Resize and center any Windows window to an exact pixel size (e.g. 1280×720). A single PowerShell script.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages