【问题标题】:How to set Powershell background color programmatically to RGB Value如何以编程方式将 Powershell 背景颜色设置为 RGB 值
【发布时间】:2013-09-08 16:06:27
【问题描述】:

目前从 Console Colors 中选择的 16 种颜色对我来说不是正确的选择。我想使用这些颜色更深的变体作为背景。

我绝对可以使用 UI 设置这些并在那里更改 RGB 值。

例如,我可以选择深蓝色,然后在 RGB 部分为蓝色选择 65(默认为 128)。有人可以告诉我如何以编程方式执行此操作。

类似:

(Get-Host).UI.RawUI.BackgroundColor=DarkBlue

但有额外的选择。

【问题讨论】:

标签: powershell console


【解决方案1】:

Lee Holmes 的这篇旧文章解释了如何将颜色更改为所需的任何值。您必须更改注册表 - http://www.leeholmes.com/blog/2008/06/01/powershells-noble-blue/

Push-Location 
Set-Location HKCU:\Console 
New-Item ".\%SystemRoot%_system32_WindowsPowerShell_v1.0_powershell.exe" 
Set-Location ".\%SystemRoot%_system32_WindowsPowerShell_v1.0_powershell.exe"

New-ItemProperty . ColorTable00 -type DWORD -value 0×00562401 
New-ItemProperty . ColorTable07 -type DWORD -value 0x00f0edee 
New-ItemProperty . FaceName -type STRING -value "Lucida Console" 
New-ItemProperty . FontFamily -type DWORD -value 0×00000036 
New-ItemProperty . FontSize -type DWORD -value 0x000c0000 
New-ItemProperty . FontWeight -type DWORD -value 0×00000190 
New-ItemProperty . HistoryNoDup -type DWORD -value 0×00000000 
New-ItemProperty . QuickEdit -type DWORD -value 0×00000001 
New-ItemProperty . ScreenBufferSize -type DWORD -value 0x0bb80078 
New-ItemProperty . WindowSize -type DWORD -value 0×00320078 
Pop-Location

【讨论】:

    【解决方案2】:

    此 powershell 函数模仿 cmd 行调用:color b0

    function Set-ConsoleColor ($bc, $fc) {
        $Host.UI.RawUI.BackgroundColor = $bc
        $Host.UI.RawUI.ForegroundColor = $fc
        Clear-Host
    }
    Set-ConsoleColor 'cyan' 'black'
    

    控制台颜色名​​称可以通过以下代码获取:

    [Enum]::GetValues([ConsoleColor])
    

    【讨论】:

    • 在输入命令之前一直有效。然后它会退回到旧的配色方案。
    【解决方案3】:

    我已将此功能添加到我的 powershell 配置文件中,因为有一个程序经常弄乱我的 shell 的颜色。

    $DefaultForeground = (Get-Host).UI.RawUI.ForegroundColor
    $DefaultBackground = (Get-Host).UI.RawUI.BackgroundColor
    function SetColors
    {
        Param
        (
            [string]$Foreground = "",
            [string]$Background = ""
        )
    
        $ValidColors = "black","blue","cyan","darkblue" ,"darkcyan","darkgray",
            "darkgreen","darkmagenta","darkred","darkyellow","gray","green",
            "magenta","red","white","yellow";
    
        $Foreground = $Foreground.ToLower()
        $Background = $Background.ToLower()
    
        if ( $Foreground -eq "" )
        {
            $Foreground = $DefaultForeground
        }
        if ( $Background -eq "" )
        {
            $Background = $DefaultBackground
        }
    
        if ( $ValidColors -contains $Foreground -and
             $ValidColors -contains $Background )
        {
            $a = (Get-Host).UI.RawUI
            $a.ForegroundColor = $Foreground
            $a.BackgroundColor = $Background
        }
        else 
        {
            write-host "Foreground/Background Colors must be one of the following:"
            $ValidColors 
        }
    }
    set-alias set-colors SetColors
    

    一些注意事项:

    "$DefaultCololrs = (Get-Host).UI.RawUI" 创建的指针类型对象多于对象的实际副本。这意味着,如果您稍后设置一个不同的变量等于 "(Get-Host).UI.RawUI",并进行更改,$DefaultColors 也会更改(这就是为什么我确保将它们作为字符串复制到此处)。

    我尝试设置其他颜色(使用十六进制代码),但运气不佳,虽然我确实找到了Setting Powershell colors with hex values in profile script(我还没有尝试过,因为我不是特别喜欢在注册表中捣乱,并且默认的颜色列表似乎已经足够了)。

    我还找到了这个文档:https://technet.microsoft.com/en-us/library/ff406264.aspx,我可能需要稍后使用它来弄清楚如何修改我的“grep”命令(目前我将它别名为 select-string)

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 2021-10-18
      • 2023-03-26
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 2021-05-20
      • 2015-11-20
      • 1970-01-01
      相关资源
      最近更新 更多