Created
February 14, 2025 11:05
-
-
Save journey-ad/4116d4638dde0b2ab3ce9db841381db1 to your computer and use it in GitHub Desktop.
查找标题并设置窗口尺寸
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$WindowTitle, | |
[Parameter(Mandatory=$true)] | |
[int]$Width, | |
[Parameter(Mandatory=$true)] | |
[int]$Height | |
) | |
Add-Type @" | |
using System; | |
using System.Runtime.InteropServices; | |
public class Win32 { | |
[DllImport("user32.dll")] | |
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); | |
[DllImport("user32.dll")] | |
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); | |
public struct RECT { | |
public int Left; | |
public int Top; | |
public int Right; | |
public int Bottom; | |
} | |
} | |
"@ | |
# 获取包含指定标题的窗口 | |
$windows = Get-Process | Where-Object {$_.MainWindowTitle -like "*$WindowTitle*" -and $_.MainWindowHandle -ne 0} | |
if ($windows.Count -eq 0) { | |
Write-Host "未找到包含标题 '$WindowTitle' 的窗口" | |
exit | |
} | |
foreach ($window in $windows) { | |
$handle = $window.MainWindowHandle | |
$rect = New-Object Win32+RECT | |
# 获取当前窗口位置 | |
[Win32]::GetWindowRect($handle, [ref]$rect) | |
$currentX = $rect.Left | |
$currentY = $rect.Top | |
# 保持原来的位置,只改变宽度和高度 | |
[Win32]::MoveWindow($handle, $currentX, $currentY, $Width, $Height, $true) | |
Write-Host "已调整窗口大小:'$($window.MainWindowTitle)'" | |
} |
Author
journey-ad
commented
Feb 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment