如何使用PowerShell实现三屏环境下应用启动与窗口自动排布
实现方案
核心通过调用.NET内置类获取显示器信息,配合Win32 API调整窗口位置,无需额外安装工具。
完整可运行脚本
# 加载Windows Forms程序集用于读取显示器配置 Add-Type -AssemblyName System.Windows.Forms # 声明Win32窗口操作API Add-Type @" using System; using System.Runtime.InteropServices; public class Win32 { [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); public const uint SWP_NOZORDER = 0x0004; public const uint SWP_SHOWWINDOW = 0x0040; } "@ # 获取所有显示器配置,默认按从左到右排序,索引可根据你的实际布局调整 $leftScreen = [System.Windows.Forms.Screen]::AllScreens[0] $midScreen = [System.Windows.Forms.Screen]::AllScreens[1] $rightScreen = [System.Windows.Forms.Screen]::AllScreens[2] <# 通用启动程序+调整窗口函数 参数说明: FilePath: 程序本地路径 ArgumentList: 程序启动参数 X/Y: 窗口左上角坐标 Width/Height: 窗口宽高 #> function Start-AppWithPosition { param( [string]$FilePath, [string[]]$ArgumentList, [int]$X, [int]$Y, [int]$Width, [int]$Height ) # 启动程序并获取进程句柄 $proc = Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -PassThru # 等待程序窗口加载完成,最多等待3秒,可根据程序启动速度自行调整 do { Start-Sleep -Milliseconds 200 $proc.Refresh() } while (-not $proc.MainWindowHandle -and $proc.StartTime.AddSeconds(3) -gt (Get-Date)) # 调整窗口位置大小 if ($proc.MainWindowHandle) { [Win32]::SetWindowPos($proc.MainWindowHandle, [IntPtr]::Zero, $X, $Y, $Width, $Height, [Win32]::SWP_NOZORDER -bor [Win32]::SWP_SHOWWINDOW) | Out-Null } return $proc } # ---------------------- 启动对应程序 ---------------------- # 1. 启动VSCode并在中间屏最大化,替换为你本地VSCode实际路径 Start-AppWithPosition -FilePath 'C:\Users\你的用户名\AppData\Local\Programs\Microsoft VS Code\Code.exe' ` -ArgumentList 'C:\repos' ` -X $midScreen.Bounds.X ` -Y $midScreen.Bounds.Y ` -Width $midScreen.Bounds.Width ` -Height $midScreen.Bounds.Height # 2. 启动4个PowerShell,平分左屏四个角落 $leftHalfWidth = $leftScreen.Bounds.Width / 2 $leftHalfHeight = $leftScreen.Bounds.Height / 2 # 左上PowerShell Start-AppWithPosition -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' ` -ArgumentList '-NoExit', 'Set-Location C:\repos' ` -X $leftScreen.Bounds.X ` -Y $leftScreen.Bounds.Y ` -Width $leftHalfWidth ` -Height $leftHalfHeight # 右上PowerShell Start-AppWithPosition -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' ` -ArgumentList '-NoExit', 'Set-Location C:\repos' ` -X ($leftScreen.Bounds.X + $leftHalfWidth) ` -Y $leftScreen.Bounds.Y ` -Width $leftHalfWidth ` -Height $leftHalfHeight # 左下PowerShell Start-AppWithPosition -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' ` -ArgumentList '-NoExit', 'Set-Location C:\repos' ` -X $leftScreen.Bounds.X ` -Y ($leftScreen.Bounds.Y + $leftHalfHeight) ` -Width $leftHalfWidth ` -Height $leftHalfHeight # 右下PowerShell Start-AppWithPosition -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' ` -ArgumentList '-NoExit', 'Set-Location C:\repos' ` -X ($leftScreen.Bounds.X + $leftHalfWidth) ` -Y ($leftScreen.Bounds.Y + $leftHalfHeight) ` -Width $leftHalfWidth ` -Height $leftHalfHeight # 3. 启动Chrome并在右屏最大化,替换为你本地Chrome实际路径 Start-AppWithPosition -FilePath 'C:\Program Files\Google\Chrome\Application\chrome.exe' ` -ArgumentList 'https://你的常用首页' ` -X $rightScreen.Bounds.X ` -Y $rightScreen.Bounds.Y ` -Width $rightScreen.Bounds.Width ` -Height $rightScreen.Bounds.Height
注意事项
- 所有程序的
FilePath参数需替换为你本地实际的安装路径,避免启动失败 - 若显示器排序不符合左0、中1、右2的规则,可运行
[System.Windows.Forms.Screen]::AllScreens | Select-Object DeviceName, Bounds | Format-List查看各屏幕坐标,调整索引即可 - 若程序启动速度慢,可将函数中的3秒等待时间调整为更长数值
内容的提问来源于stack exchange,提问作者pax
相关产品推荐
相关产品推荐

