How to echo with different colors in the Windows command line

前端 未结 23 1249
野性不改
野性不改 2020-11-22 08:55

I know that the color bf command sets the colors of the whole command line window but I wanted to to print one single line in a different color.

相关标签:
23条回答
  • 2020-11-22 09:34

    To get this working on Windows 10, you can enable this flag: ENABLE_VIRTUAL_TERMINAL_PROCESSING.

    With this registry key you can set this by default

    [HKCU\Console] VirtualTerminalLevel dword 0x1

    0 讨论(0)
  • 2020-11-22 09:35

    I wanted to to print one single line in a different color.

    Use ANSI Escape Sequences.

    Windows before 10 - no native support for ANSI colors on the console

    For Windows version below 10, the Windows command console doesn't support output coloring by default. You could install either Cmder, ConEmu, ANSICON or Mintty (used by default in GitBash and Cygwin) to add coloring support to your Windows command console.

    Windows 10 - Command Line Colors

    Starting from Windows 10 the Windows console support ANSI Escape Sequences and some colors by default. The feature shipped with the Threshold 2 Update in Nov 2015.

    MSDN Documentation

    Update (05-2019): The ColorTool enables you to change the color scheme of the console. It's part of the Microsoft Terminal project.

    Demo

    Batch Command

    The win10colors.cmd was written by Michele Locati:

    The text below is stripped of special characters and will not work. You must copy it from here.

    @echo off
    cls
    echo [101;93m STYLES [0m
    echo ^<ESC^>[0m [0mReset[0m
    echo ^<ESC^>[1m [1mBold[0m
    echo ^<ESC^>[4m [4mUnderline[0m
    echo ^<ESC^>[7m [7mInverse[0m
    echo.
    echo [101;93m NORMAL FOREGROUND COLORS [0m
    echo ^<ESC^>[30m [30mBlack[0m (black)
    echo ^<ESC^>[31m [31mRed[0m
    echo ^<ESC^>[32m [32mGreen[0m
    echo ^<ESC^>[33m [33mYellow[0m
    echo ^<ESC^>[34m [34mBlue[0m
    echo ^<ESC^>[35m [35mMagenta[0m
    echo ^<ESC^>[36m [36mCyan[0m
    echo ^<ESC^>[37m [37mWhite[0m
    echo.
    echo [101;93m NORMAL BACKGROUND COLORS [0m
    echo ^<ESC^>[40m [40mBlack[0m
    echo ^<ESC^>[41m [41mRed[0m
    echo ^<ESC^>[42m [42mGreen[0m
    echo ^<ESC^>[43m [43mYellow[0m
    echo ^<ESC^>[44m [44mBlue[0m
    echo ^<ESC^>[45m [45mMagenta[0m
    echo ^<ESC^>[46m [46mCyan[0m
    echo ^<ESC^>[47m [47mWhite[0m (white)
    echo.
    echo [101;93m STRONG FOREGROUND COLORS [0m
    echo ^<ESC^>[90m [90mWhite[0m
    echo ^<ESC^>[91m [91mRed[0m
    echo ^<ESC^>[92m [92mGreen[0m
    echo ^<ESC^>[93m [93mYellow[0m
    echo ^<ESC^>[94m [94mBlue[0m
    echo ^<ESC^>[95m [95mMagenta[0m
    echo ^<ESC^>[96m [96mCyan[0m
    echo ^<ESC^>[97m [97mWhite[0m
    echo.
    echo [101;93m STRONG BACKGROUND COLORS [0m
    echo ^<ESC^>[100m [100mBlack[0m
    echo ^<ESC^>[101m [101mRed[0m
    echo ^<ESC^>[102m [102mGreen[0m
    echo ^<ESC^>[103m [103mYellow[0m
    echo ^<ESC^>[104m [104mBlue[0m
    echo ^<ESC^>[105m [105mMagenta[0m
    echo ^<ESC^>[106m [106mCyan[0m
    echo ^<ESC^>[107m [107mWhite[0m
    echo.
    echo [101;93m COMBINATIONS [0m
    echo ^<ESC^>[31m                     [31mred foreground color[0m
    echo ^<ESC^>[7m                      [7minverse foreground ^<-^> background[0m
    echo ^<ESC^>[7;31m                   [7;31minverse red foreground color[0m
    echo ^<ESC^>[7m and nested ^<ESC^>[31m [7mbefore [31mnested[0m
    echo ^<ESC^>[31m and nested ^<ESC^>[7m [31mbefore [7mnested[0m
    
    0 讨论(0)
  • 2020-11-22 09:37

    Setting color to the log statements in powershell is not a big deal friend. you can use -ForegroundColor parameter.

    To write a confirmation message.

    Write-Host "Process executed Successfully...." -ForegroundColor Magenta
    

    To write an error message.

    Write-Host "Sorry an unexpected error occurred.." -ForegroundColor Red
    

    To write a progress message.

    Write-Host "Working under pocess..." -ForegroundColor Green
    
    0 讨论(0)
  • 2020-11-22 09:39

    Put the following lines into a file called ColourText.bas on your desktop.

    Imports System
    Imports System.IO
    Imports System.Runtime.InteropServices
    Imports Microsoft.Win32
    
    Public Module MyApplication  
    Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
    Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
    Public Const STD_ERROR_HANDLE = -12&
    Public Const STD_INPUT_HANDLE = -10&
    Public Const STD_OUTPUT_HANDLE = -11&
    
    Sub Main()
        Dim hOut as Long
        Dim Ret as Long
        Dim Colour As Long
        Dim Colour1 As Long
        Dim Text As String
        hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
        Colour = CLng("&h" & Split(Command(), " ")(0))
        Colour1 = Clng("&h" & Split(Command(), " ")(1))
        Text = Mid(Command(), 7)
        Ret = SetConsoleTextAttribute(hOut,  Colour)
        Console.Out.WriteLine(text)
        Ret = SetConsoleTextAttribute(hOut, Colour1)
    End Sub
    End Module
    

    Save it and type the following in a command prompt.

    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%userprofile%\desktop\ColourText.exe" "%userprofile%\desktop\ColourText.bas" /verbose
    

    A file called ColourText.exe will appear on your desktop. Move it to the Windows folder.

    To use you must use two character codes to set colour eg 01 not 1.

    ColourText ColourOfText ColourOfTextWhenFinished Text
    

    EG To set blue on white by not passing any text, then red on white text, finishing with blue on grey.

    ColourText F1 F1
    ColourText F2 71 This is green on white
    

    or

    ColourText F1 F1
    cls
    ColourText F4 F4
    Echo Hello
    Echo Hello today
    ColourText F1 F1
    

    Also the CLS command becomes interesting. Color command without parameters resets all colours to startup colours.

    To get the colour code add the following numbers together. Use Calculator in programmers mode. These are hex numbers. They can be added together eg Red + Blue + FG Intensity = 13 = D. As 10+ wasn't used the background will be black. Colour codes MUST be two characters, eg 08 not 8.

    FOREGROUND_RED = &H4     '  text color contains red.
    FOREGROUND_INTENSITY = &H8     '  text color is intensified.
    FOREGROUND_GREEN = &H2     '  text color contains green.
    FOREGROUND_BLUE = &H1     '  text color contains blue.
    BACKGROUND_BLUE = &H10    '  background color contains blue.
    BACKGROUND_GREEN = &H20    '  background color contains green.
    BACKGROUND_INTENSITY = &H80    '  background color is intensified.
    BACKGROUND_RED = &H40    '  background color contains red.
    
    0 讨论(0)
  • 2020-11-22 09:42

    Windows 10 - TH2 and above:

    (a.k.a. Version 1511, build 10586, release 2015-11-10)

    At Command Prompt:

    echo ^[[32m HI ^[[0m
    

    Using the actual keys:   echo Ctrl+[[32m HI Ctrl+[[0mEnter

    You should see a green "HI" below it.

    Code numbers can be found here:

    • https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

    Notepad:

    To save this into notepad, you can type ESC into it using: Alt+027 with the numpad, then the [32m part. Another trick when I was on a laptop, redirect the line above into a file to get started, then cut and paste:

    echo echo ^[[32m HI ^[[0m >> batch_file.cmd
    
    0 讨论(0)
  • 2020-11-22 09:43

    You could use ANSICON to enable ANSI terminal codes in older versions of Windows. There are 32 and 64 bit versions that I have used in Windows XP and Windows 7.

    0 讨论(0)
提交回复
热议问题