How to echo with different colors in the Windows command line

前端 未结 23 1643
野性不改
野性不改 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: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.
    

提交回复
热议问题