Repository navigation
Expand file tree
/
Copy pathSet-WindowRect.ps1
More file actions
187 lines (165 loc) · 7.87 KB
/
Copy pathSet-WindowRect.ps1
File metadata and controls
187 lines (165 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<#
.SYNOPSIS
精确设置任意窗口的大小和位置(默认 1280×720,在所在显示器上居中)。
.DESCRIPTION
- 默认让“看得见的窗口外框”正好是 Width × Height(已扣除 Win10/11 的隐形缩放边框)。
- 加 -ClientArea 则让“内容区/客户区”(不含标题栏和边框)正好是 Width × Height,
适合游戏、模拟器、录屏等需要精确画面分辨率的场景。
- 不指定 -X/-Y 时居中(默认相对工作区,即不含任务栏;加 -FullMonitor 相对整个屏幕)。
- 以物理像素为单位,不受系统缩放(125%/150% 等)干扰。
.EXAMPLE
.\Set-WindowRect.ps1 -ProcessName notepad
# 记事本外框 1280×720 并居中
.EXAMPLE
.\Set-WindowRect.ps1 -ProcessName pcsx2-qt -Width 1280 -Height 720 -ClientArea
# 画面区域正好 1280×720 并居中
.EXAMPLE
.\Set-WindowRect.ps1 -Title "*Chrome*" -Width 1920 -Height 1080 -X 0 -Y 0
# 按标题匹配,放到左上角
.EXAMPLE
.\Set-WindowRect.ps1
# 不指定目标:3 秒内点一下要调整的窗口,脚本作用于当前前台窗口
.EXAMPLE
.\Set-WindowRect.ps1 -List
# 列出所有可调整的窗口(句柄、进程名、标题、当前尺寸),可配合 -ProcessName/-Title 过滤
#>
[CmdletBinding()]
param(
[string]$ProcessName,
[string]$Title,
[long]$Handle,
[ValidateRange(1, 32767)][int]$Width = 1280,
[ValidateRange(1, 32767)][int]$Height = 720,
[switch]$ClientArea,
[int]$X,
[int]$Y,
[switch]$FullMonitor,
[ValidateRange(0, 60)][int]$Delay = 3,
[switch]$List
)
if (-not ('WinRect.Native' -as [type])) {
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
namespace WinRect {
public static class Native {
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int L, T, R, B; }
[StructLayout(LayoutKind.Sequential)] struct MONITORINFO { public int cbSize; public RECT rcMonitor; public RECT rcWork; public uint dwFlags; }
[DllImport("user32.dll")] public static extern IntPtr SetThreadDpiAwarenessContext(IntPtr ctx);
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] public static extern bool IsWindow(IntPtr h);
[DllImport("user32.dll")] public static extern bool IsZoomed(IntPtr h);
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr h);
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int cmd);
[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr h, IntPtr after, int x, int y, int cx, int cy, uint flags);
[DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] static extern bool GetClientRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] static extern IntPtr MonitorFromWindow(IntPtr h, uint flags);
[DllImport("user32.dll")] static extern bool GetMonitorInfo(IntPtr m, ref MONITORINFO mi);
[DllImport("dwmapi.dll")] static extern int DwmGetWindowAttribute(IntPtr h, int attr, out RECT r, int size);
// { 窗口矩形 L,T,R,B, 客户区 W,H, 可见外框 L,T,R,B }
public static int[] Rects(IntPtr h) {
RECT w, c, f;
GetWindowRect(h, out w);
GetClientRect(h, out c);
if (DwmGetWindowAttribute(h, 9 /* DWMWA_EXTENDED_FRAME_BOUNDS */, out f, 16) != 0) f = w;
return new int[] { w.L, w.T, w.R, w.B, c.R - c.L, c.B - c.T, f.L, f.T, f.R, f.B };
}
// 窗口所在显示器的 { L,T,R,B },work=true 时为工作区(不含任务栏)
public static int[] MonitorArea(IntPtr h, bool work) {
MONITORINFO mi = new MONITORINFO();
mi.cbSize = Marshal.SizeOf(typeof(MONITORINFO));
GetMonitorInfo(MonitorFromWindow(h, 2 /* NEAREST */), ref mi);
RECT r = work ? mi.rcWork : mi.rcMonitor;
return new int[] { r.L, r.T, r.R, r.B };
}
}
}
'@
}
# 让坐标统一为物理像素(Per-Monitor DPI Aware V2)
[void][WinRect.Native]::SetThreadDpiAwarenessContext([IntPtr](-4))
# 按进程名/标题筛选有主窗口的进程(标题不含通配符时按“包含”匹配)
function Find-WindowProcess {
$procs = Get-Process | Where-Object { $_.MainWindowHandle -ne [IntPtr]::Zero }
if ($ProcessName) {
$pn = $ProcessName -replace '\.exe$', ''
$procs = $procs | Where-Object { $_.ProcessName -like $pn }
}
if ($Title) {
$pat = if ($Title -match '[\*\?\[]') { $Title } else { "*$Title*" }
$procs = $procs | Where-Object { $_.MainWindowTitle -like $pat }
}
@($procs)
}
# ---------- 列出窗口 ----------
if ($List) {
Find-WindowProcess | ForEach-Object {
$r = [WinRect.Native]::Rects($_.MainWindowHandle)
[pscustomobject][ordered]@{
'Handle' = [long]$_.MainWindowHandle
'Process' = $_.ProcessName
'外框' = '{0}×{1}' -f ($r[8] - $r[6]), ($r[9] - $r[7])
'客户区' = '{0}×{1}' -f $r[4], $r[5]
'位置' = '({0}, {1})' -f $r[6], $r[7]
'Title' = $_.MainWindowTitle
}
} | Sort-Object Process | Format-Table -AutoSize
return
}
# ---------- 找到目标窗口 ----------
if ($Handle) {
$h = [IntPtr]$Handle
}
elseif ($ProcessName -or $Title) {
$procs = Find-WindowProcess
if ($procs.Count -eq 0) { throw "没有找到匹配的窗口(进程名: '$ProcessName' 标题: '$Title')" }
if ($procs.Count -gt 1) {
Write-Warning "匹配到 $($procs.Count) 个窗口,使用第一个。可用 -Handle 精确指定:"
$procs | ForEach-Object { Write-Host (" -Handle {0} [{1}] {2}" -f $_.MainWindowHandle, $_.ProcessName, $_.MainWindowTitle) }
}
$h = $procs[0].MainWindowHandle
}
else {
Write-Host "请在 $Delay 秒内点击要调整的窗口..."
Start-Sleep -Seconds $Delay
$h = [WinRect.Native]::GetForegroundWindow()
}
if (-not [WinRect.Native]::IsWindow($h)) { throw "无效的窗口句柄: $h" }
# 最大化/最小化状态下无法设置尺寸,先还原
if ([WinRect.Native]::IsZoomed($h) -or [WinRect.Native]::IsIconic($h)) {
[void][WinRect.Native]::ShowWindow($h, 9) # SW_RESTORE
Start-Sleep -Milliseconds 300
}
# ---------- 计算并设置(最多 3 轮校正,应对菜单栏换行等情况)----------
$area = [WinRect.Native]::MonitorArea($h, -not $FullMonitor)
for ($i = 0; $i -lt 3; $i++) {
$r = [WinRect.Native]::Rects($h)
$winW = $r[2] - $r[0]; $winH = $r[3] - $r[1]
$cliW = $r[4]; $cliH = $r[5]
# 隐形边框(GetWindowRect 比肉眼看到的外框大出的部分)
$invL = $r[6] - $r[0]; $invT = $r[7] - $r[1]
$invR = $r[2] - $r[8]; $invB = $r[3] - $r[9]
if ($ClientArea) {
$outW = $Width + ($winW - $cliW); $outH = $Height + ($winH - $cliH)
} else {
$outW = $Width + $invL + $invR; $outH = $Height + $invT + $invB
}
$visW = $outW - $invL - $invR; $visH = $outH - $invT - $invB
$visX = if ($PSBoundParameters.ContainsKey('X')) { $X } else { $area[0] + [math]::Floor(($area[2] - $area[0] - $visW) / 2) }
$visY = if ($PSBoundParameters.ContainsKey('Y')) { $Y } else { $area[1] + [math]::Floor(($area[3] - $area[1] - $visH) / 2) }
# SWP_NOZORDER | SWP_NOACTIVATE
[void][WinRect.Native]::SetWindowPos($h, [IntPtr]::Zero, $visX - $invL, $visY - $invT, $outW, $outH, 0x14)
Start-Sleep -Milliseconds 100
$r = [WinRect.Native]::Rects($h)
$ok = if ($ClientArea) { $r[4] -eq $Width -and $r[5] -eq $Height }
else { ($r[8] - $r[6]) -eq $Width -and ($r[9] - $r[7]) -eq $Height }
if ($ok) { break }
}
# ---------- 报告结果 ----------
$r = [WinRect.Native]::Rects($h)
Write-Host ("可见外框: {0}×{1} 位置 ({2}, {3})" -f ($r[8] - $r[6]), ($r[9] - $r[7]), $r[6], $r[7])
Write-Host ("客户区 : {0}×{1}" -f $r[4], $r[5])
if (-not $ok) {
Write-Warning "窗口没有完全接受目标尺寸(可能有最小/最大尺寸限制,或是全屏独占/固定大小的程序)。"
}