我在我的 PowerShell 控制台中使用了一些函数(如下)$Profile
来移动和调整我的 PowerShell 控制台的大小。这些工作非常好。我想更多地使用 Windows 终端,但我也想让这些类型的操作在那里工作,并且可能必须有一些技术来操作这些窗口(PowerShell 或访问 .NET 函数)。如何在 Windows 终端会话中获得与以下类似的结果?
请注意,这个问题专门针对新的“Windows 终端”而不是 PowerShell 控制台(以下工作正常)的操作。来自 Microsoft 的新 Windows 终端就在这里。https://www.microsoft.com/en-us/p/windows-terminal-preview/9n0dx20hk701?SilentAuth=1&wa=wsignin1.0
function Global:Set-ConsolePosition ($x, $y, $w, $h) {
# Note: the DLL code below should not be indented from the left-side
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int W, int H); '
# Do the Add-Type outside of the function as repeating it in a session can cause errors
$consoleHWND = [Console.Window]::GetConsoleWindow();
$consoleHWND = [Console.Window]::MoveWindow($consoleHWND, $x, $y, $w, $h);
# $consoleHWND = [Console.Window]::MoveWindow($consoleHWND,75,0,600,600);
# $consoleHWND = [Console.Window]::MoveWindow($consoleHWND,-6,0,600,600);
}
function lll {
Add-Type -AssemblyName System.Windows.Forms
$Screen = [System.Windows.Forms.Screen]::PrimaryScreen
$width = $Screen.WorkingArea.Width # .WorkingArea ignores the taskbar, .Bounds is whole screen
$height = $Screen.WorkingArea.Height
$w = $width/2 + 13
$h = $height + 8
$x = -7
$y = 0
Set-ConsolePosition $x $y $w $h
$MyBuffer = $Host.UI.RawUI.BufferSize
$MyWindow = $Host.UI.RawUI.WindowSize
$MyBuffer.Height = 9999
"`nWindowSize $($MyWindow.Width)x$($MyWindow.Height) (Buffer $($MyBuffer.Width)x$($MyBuffer.Height))"
"Position : Left:$x Top:$y Width:$w Height:$h`n"
}
function rrr {
Add-Type -AssemblyName System.Windows.Forms
$Screen = [System.Windows.Forms.Screen]::PrimaryScreen
$width = $Screen.WorkingArea.Width # .WorkingArea ignores the taskbar, .Bounds is whole screen
$height = $Screen.WorkingArea.Height
$w = $width/2 + 13
$h = $height + 8
$x = $w - 20
$y = 0
Set-ConsolePosition $x $y $w $h
$MyBuffer = $Host.UI.RawUI.BufferSize
$MyWindow = $Host.UI.RawUI.WindowSize
$MyBuffer.Height = 9999
"`nWindowSize $($MyWindow.Width)x$($MyWindow.Height) (Buffer $($MyBuffer.Width)x$($MyBuffer.Height))"
"Position : Left:$x Top:$y Width:$w Height:$h`n"
}
function fff {
Set-ConsolePosition -7 0 600 600
if ($Host.Name -match "console") {
$MaxHeight = $host.UI.RawUI.MaxPhysicalWindowSize.Height - 1
$MaxWidth = $host.UI.RawUI.MaxPhysicalWindowSize.Width
$MyBuffer = $Host.UI.RawUI.BufferSize
$MyWindow = $Host.UI.RawUI.WindowSize
$MyWindow.Height = $MaxHeight
$MyWindow.Width = $Maxwidth
$MyBuffer.Height = 9999
$MyBuffer.Width = $Maxwidth
# $host.UI.RawUI.set_bufferSize($MyBuffer)
# $host.UI.RawUI.set_windowSize($MyWindow)
$host.UI.RawUI.BufferSize = $MyBuffer
$host.UI.RawUI.WindowSize = $MyWindow
"`nWindowSize $($MyWindow.Width)x$($MyWindow.Height) (Buffer $($MyBuffer.Width)x$($MyBuffer.Height))`n"
}
}