2

我可以使用 $host.UI.RawUI.MaxPhysicalWindowSize.Width 来获取 PowerShell 命令窗口的最大宽度(即列数),并且设置 PowerShell 命令窗口的大小有据可查,但最小缓冲区宽度似乎机器之间有所不同。在一台机器上是 13,但在另一台机器上是 14。我可以将最小窗口高度和宽度设置为 1,最小缓冲区高度也可以是 1。

有谁知道我如何以编程方式获得这个最小缓冲区宽度值(而不仅仅是尝试值并捕获异常!)

4

3 回答 3

4

由于设置$host.UI.RawUI.BufferSize会影响其控制台屏幕的缓冲区,(命令提示符 -> 属性 -> 布局 -> 屏幕缓冲区大小在更改时会被修改$host.UI.RawUI.BufferSize),因此它对缓冲区大小的限制与控制台屏幕相同。

正如我们在这里所读到的,buffersize 的指定尺寸不能小于系统允许的最小尺寸。此最小值取决于控制台的当前字体大小(由用户选择)以及GetSystemMetrics函数返回的 SM_CXMIN 和 SM_CYMIN 值。

这意味着,您的控制台屏幕字体越大,您可以使缓冲区大小越小。

例如:这里是如何获得控制台屏幕的最小宽度。我正在使用这个高级函数(Joel Bennett 的 New-PInvoke)从 User32.dll P/Invoking GetSystemMetrics 函数。

$SM_CXMIN =28 # "The minimum width of a window, in pixels." enum value
New-PInvoke -Library User32.dll -Signature "int GetSystemMetrics(uint Metric)"
GetSystemMetrics $SM_CXMIN # returns 132 on my system

要检索控制台屏幕缓冲区使用的字体大小,请尝试从 kernel32.dll获取 GetConsoleFontSize 。

笔记:

GetSystemMetrics $SM_CXMIN 返回的值是控制台屏幕的总宽度(包括边框)。

于 2012-01-17T14:34:26.287 回答
1

我在谷歌搜索中看到了这篇文章。“jon Z”提供的“Joel Bennett 的 New-PInvoke”链接早已不复存在。使用互联网档案,我发现了 2015 年的一个捕获。为了保存,这里是函数。

function New-PInvoke
{
    <#
    .Synopsis
        Generate a powershell function alias to a Win32|C api function
    .Description
        Creates C# code to access a C function, and exposes it via a powershell function
    .Example
        New-PInvoke -Library User32.dll -Signature "int GetSystemMetrics(uint Metric)"
    .Parameter Library
        A C Library containing code to invoke
    .Parameter Signature
        The C# signature of the external method
    .Parameter OutputText
        If Set, retuns the source code for the pinvoke method.
        If not, compiles the type.
    #>
    param(
    [Parameter(Mandatory=$true,
        HelpMessage="The C Library Containing the Function, i.e. User32")]
    [String]
    $Library,
   
    [Parameter(Mandatory=$true,
        HelpMessage="The Signature of the Method, i.e.: int GetSystemMetrics(uint Metric)")]
    [String]
    $Signature,
   
    [Switch]
    $OutputText
    )
   
    process {
        if ($Library -notlike "*.dll*") {
            $Library+=".dll"
        }
        if ($signature -notlike "*;") {
            $Signature+=";"
        }
        if ($signature -notlike "public static extern*") {
            $signature = "public static extern $signature"
        }
       
        $name = $($signature -replace "^.*?\s(\w+)\(.*$",'$1')
       
        $MemberDefinition = "[DllImport(`"$Library`")]`n$Signature"
       
        if (-not $OutputText) {
            $type = Add-Type -PassThru -Name "PInvoke$(Get-Random)" -MemberDefinition $MemberDefinition
            iex "New-Item Function:Global:$name -Value { [$($type.FullName)]::$name.Invoke( `$args ) }"
        } else {
            $MemberDefinition
        }
    }
}
于 2020-12-10T18:21:39.383 回答
0

也许我错了,但是

[system.console]::BufferWidth 

你得到实际的缓冲区宽度大小。

此值不能小于当前[System.Console]::WindowWidth大小(将引发异常)。

于 2012-01-17T13:32:24.743 回答