假设我在PowerShell窗口中打印了100行带有随机背景色的行。我如何确定背景颜色,是适用于特定的线条和字符之后的事实?
我尝试更改为一个位置并打印控制台背景色,但正如我所怀疑的那样,它显示已经设置为输出默认值的背景色:
$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发布于 2018-08-15 05:58:44
正如PetSerAl在注释中指出的那样,$Host.UI.RawUI.GetBufferContents能够从缓冲区中获取任意范围的内容,包括颜色。我将它封装在一个函数中,我可能会使用这个函数:
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;
}
}用法:
$c = Get-ColorsAtPosition -x 3 -y 5
$c.ForegroundColor # White
$c.BackgroundColor # Blue请记住,-x和-y是零索引的.
https://stackoverflow.com/questions/51853041
复制相似问题