首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PowerShell窗口中确定特定X,Y坐标的背景色

在PowerShell窗口中确定特定X,Y坐标的背景色
EN

Stack Overflow用户
提问于 2018-08-15 04:37:29
回答 1查看 243关注 0票数 1

假设我在PowerShell窗口中打印了100行带有随机背景色的行。我如何确定背景颜色,是适用于特定的线条和字符之后的事实?

我尝试更改为一个位置并打印控制台背景色,但正如我所怀疑的那样,它显示已经设置为输出默认值的背景色:

代码语言:javascript
复制
$pos = (Get-Host).UI.RawUI.CursorPosition
$pos.X = 10
$pos.Y = 10
(Get-Host).UI.RawUI.CursorPosition = $pos
Write-Host (Get-Host).UI.RawUI.BackgroundColor
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-15 05:58:44

正如PetSerAl在注释中指出的那样,$Host.UI.RawUI.GetBufferContents能够从缓冲区中获取任意范围的内容,包括颜色。我将它封装在一个函数中,我可能会使用这个函数:

代码语言:javascript
复制
function Get-ColorsAtPosition {
    Param(
        [Parameter(Mandatory=$true)]
        [int]$x,
        [Parameter(Mandatory=$true)]
        [int]$y
    )

    $colors = $Host.UI.RawUI.GetBufferContents(@{
        Left=$x; Right=$x; Top=$y; Bottom=$y;
    })
    return @{
        ForegroundColor=$colors.ForegroundColor;
        BackgroundColor=$colors.BackgroundColor;
    }
}

用法:

代码语言:javascript
复制
$c = Get-ColorsAtPosition -x 3 -y 5
$c.ForegroundColor # White
$c.BackgroundColor # Blue

请记住,-x-y是零索引的.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51853041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档