设置Powershell窗口的位置
大家好,我正在尝试创建一个具有特定大小和位置的新powershell窗口当前我有:设置Powershell窗口的位置,powershell,Powershell,大家好,我正在尝试创建一个具有特定大小和位置的新powershell窗口当前我有: Start-Process powershell -ArgumentList "-noexit", "-command [console]::windowwidth=50; [console]::windowheight=10;","python myscript.py"; 但是我找不到使用参数或命令设置窗口位置的方法。如果您将此函数复制并粘贴到文件中,然后使用导入模块-Path MYPATH导入它,您将能够调
Start-Process powershell -ArgumentList "-noexit", "-command [console]::windowwidth=50; [console]::windowheight=10;","python myscript.py";
但是我找不到使用参数或命令设置窗口位置的方法。如果您将此函数复制并粘贴到文件中,然后使用导入模块-Path MYPATH导入它,您将能够调用它来调整指定进程的窗口大小
Function Set-Window {
<#
.SYNOPSIS
Sets the window size (height,width) and coordinates (x,y) of
a process window.
.DESCRIPTION
Sets the window size (height,width) and coordinates (x,y) of
a process window.
.PARAMETER ProcessName
Name of the process to determine the window characteristics
.PARAMETER X
Set the position of the window in pixels from the top.
.PARAMETER Y
Set the position of the window in pixels from the left.
.PARAMETER Width
Set the width of the window.
.PARAMETER Height
Set the height of the window.
.PARAMETER Passthru
Display the output object of the window.
.NOTES
Name: Set-Window
Author: Boe Prox
Version History
1.0//Boe Prox - 11/24/2015
- Initial build
.OUTPUT
System.Automation.WindowInfo
.EXAMPLE
Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru
ProcessName Size TopLeft BottomRight
----------- ---- ------- -----------
powershell 1262,642 2040,142 3302,784
Description
-----------
Set the coordinates on the window for the process PowerShell.exe
#>
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param (
[parameter(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[switch]$Passthru
)
Begin {
Try{
[void][Window]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
}
Process {
$Rectangle = New-Object RECT
$Handle = (Get-Process -id $ProcessName).MainWindowHandle
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If (-NOT $PSBoundParameters.ContainsKey('Width')) {
$Width = $Rectangle.Right - $Rectangle.Left
}
If (-NOT $PSBoundParameters.ContainsKey('Height')) {
$Height = $Rectangle.Bottom - $Rectangle.Top
}
If ($Return) {
$Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
}
If ($PSBoundParameters.ContainsKey('Passthru')) {
$Rectangle = New-Object RECT
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If ($Return) {
$Height = $Rectangle.Bottom - $Rectangle.Top
$Width = $Rectangle.Right - $Rectangle.Left
$Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
$TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
$BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
Write-Warning "Window is minimized! Coordinates will not be accurate."
}
$Object = [pscustomobject]@{
ProcessName = $ProcessName
Size = $Size
TopLeft = $TopLeft
BottomRight = $BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
}
}
我希望这有帮助 控制台内的缓冲区窗口可以很容易地用
[console]::SetWindowPosition()
重新定位,但对于桌面窗口,您需要调用
Start-Process powershell -ArgumentList "-noexit","-command (Set-Window -ProcessName $pid -X 1911 -Y '-369' -Width 266 -Height 113)"