如何在Windows命令行中使用不同的颜色进行回显

我知道color bf命令设置了整个命令行窗口的颜色,但是我想用不同的颜色打印一行。

我想用不同的颜色打印一行。

使用ANSI转义序列。

Windows 10以前的版本 – 在控制台上不支持ANSI颜色

对于低于10的Windows版本,Windows命令控制台默认不支持输出着色。 您可以安装Cmder , ConEmu , ANSICON或Mintty (在GitBash和Cygwin中默认使用)为您的Windows命令控制台添加着色支持。

Windows 10 – 命令行颜色

从Windows 10开始,Windows控制台默认支持ANSI Escape Sequences和一些颜色。 2015年11月的“阈值2更新”附带的功能。

MSDN文档

演示

在这里输入图像描述

批量命令

win10colors.cmd是由Michele Locati编写的:

 @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 

这是一个自编的bat / .net混合体 (应该保存为.BAT ),可以在任何已安装.net框架的系统上使用(即使是最老的XP也可以看到没有.NET框架的窗口/ 2003安装)。 它使用jscript.net编译器来创建一个exe文件,只能为当前行打印不同背景/前景色的字符串。

 @if (@X)==(@Y) @end /* JScript comment @echo off setlocal for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do ( set "jsc=%%v" ) if not exist "%~n0.exe" ( "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0" ) %~n0.exe %* endlocal & exit /b %errorlevel% */ import System; var arguments:String[] = Environment.GetCommandLineArgs(); var newLine = false; var output = ""; var foregroundColor = Console.ForegroundColor; var backgroundColor = Console.BackgroundColor; var evaluate = false; var currentBackground=Console.BackgroundColor; var currentForeground=Console.ForegroundColor; //http://stackoverflow.com/a/24294348/388389 var jsEscapes = { 'n': '\n', 'r': '\r', 't': '\t', 'f': '\f', 'v': '\v', 'b': '\b' }; function decodeJsEscape(_, hex0, hex1, octal, other) { var hex = hex0 || hex1; if (hex) { return String.fromCharCode(parseInt(hex, 16)); } if (octal) { return String.fromCharCode(parseInt(octal, 8)); } return jsEscapes[other] || other; } function decodeJsString(s) { return s.replace( // Matches an escape sequence with UTF-16 in group 1, single byte hex in group 2, // octal in group 3, and arbitrary other single-character escapes in group 4. /\\(?:u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([0-3][0-7]{0,2}|[4-7][0-7]?)|(.))/g, decodeJsEscape); } function printHelp( ) { print( arguments[0] + " -s string [-f foreground] [-b background] [-n] [-e]" ); print( " " ); print( " string String to be printed" ); print( " foreground Foreground color - a " ); print( " number between 0 and 15." ); print( " background Background color - a " ); print( " number between 0 and 15." ); print( " -n Indicates if a new line should" ); print( " be written at the end of the "); print( " string(by default - no)." ); print( " -e Evaluates special character " ); print( " sequences like \\n\\b\\r and etc "); print( "" ); print( "Colors :" ); for ( var c = 0 ; c < 16 ; c++ ) { Console.BackgroundColor = c; Console.Write( " " ); Console.BackgroundColor=currentBackground; Console.Write( "-"+c ); Console.WriteLine( "" ); } Console.BackgroundColor=currentBackground; } function errorChecker( e:Error ) { if ( e.message == "Input string was not in a correct format." ) { print( "the color parameters should be numbers between 0 and 15" ); Environment.Exit( 1 ); } else if (e.message == "Index was outside the bounds of the array.") { print( "invalid arguments" ); Environment.Exit( 2 ); } else { print ( "Error Message: " + e.message ); print ( "Error Code: " + ( e.number & 0xFFFF ) ); print ( "Error Name: " + e.name ); Environment.Exit( 666 ); } } function numberChecker( i:Int32 ){ if( i > 15 || i < 0 ) { print("the color parameters should be numbers between 0 and 15"); Environment.Exit(1); } } if ( arguments.length == 1 || arguments[1].toLowerCase() == "-help" || arguments[1].toLowerCase() == "-help" ) { printHelp(); Environment.Exit(0); } for (var arg = 1; arg <= arguments.length-1; arg++ ) { if ( arguments[arg].toLowerCase() == "-n" ) { newLine=true; } if ( arguments[arg].toLowerCase() == "-e" ) { evaluate=true; } if ( arguments[arg].toLowerCase() == "-s" ) { output=arguments[arg+1]; } if ( arguments[arg].toLowerCase() == "-b" ) { try { backgroundColor=Int32.Parse( arguments[arg+1] ); } catch(e) { errorChecker(e); } } if ( arguments[arg].toLowerCase() == "-f" ) { try { foregroundColor=Int32.Parse(arguments[arg+1]); } catch(e) { errorChecker(e); } } } Console.BackgroundColor = backgroundColor ; Console.ForegroundColor = foregroundColor ; if ( evaluate ) { output=decodeJsString(output); } if ( newLine ) { Console.WriteLine(output); } else { Console.Write(output); } Console.BackgroundColor = currentBackground; Console.ForegroundColor = currentForeground; 

示例coloroutput.bat -s "aa\nbb\n\u0025cc" -b 10 -f 3 -n -e

您也可以检查carlos的颜色功能 – > http://www.dostips.com/forum/viewtopic.php?f=3&t=4453

这不是一个好的答案,但是如果你知道目标工作站有Powershell,你可以这样做(假设BAT / CMD脚本):

 CALL:ECHORED "Print me in red!" :ECHORED %Windir%\System32\WindowsPowerShell\v1.0\Powershell.exe write-host -foregroundcolor Red %1 goto:eof 

试试这个例子:

 @echo off SETLOCAL EnableDelayedExpansion for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do ( set "DEL=%%a" ) echo say the name of the colors, don't read call :ColorText 0a "blue" call :ColorText 0C "green" call :ColorText 0b "red" echo( call :ColorText 19 "yellow" call :ColorText 2F "black" call :ColorText 4e "white" goto :eof :ColorText echo off <nul set /p ".=%DEL%" > "%~2" findstr /v /a:%1 /R "^$" "%~2" nul del "%~2" > nul 2>&1 goto :eof 

您可以使用ANSICON在现代Windows中启用ANSI终端代码。 我在Windows XP和Windows 7中使用了32位和64位版本。

我对cmd中缺乏正确的着色感到恼火,于是我继续创建了cmdcolor 。 它只是一个stdout代理,它寻找一组有限的ANSI / VT100控制序列(换句话说就像在bash中),即echo \033[31m RED \033[0m DEFAULT | cmdcolor.exe echo \033[31m RED \033[0m DEFAULT | cmdcolor.exe

用法和下载 。

我看着这个,因为我想介绍一些简单的文本颜色到Win7批处理文件。 这是我想出来的。 谢谢你的帮助。

 @echo off cls && color 08 rem .... the following line creates a [DEL] [ASCII 8] [Backspace] character to use later rem .... All this to remove [:] for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a") echo. <nul set /p="(" call :PainText 09 "BLUE is cold" && <nul set /p=") (" call :PainText 02 "GREEN is earth" && <nul set /p=") (" call :PainText F0 "BLACK is night" && <nul set /p=")" echo. <nul set /p="(" call :PainText 04 "RED is blood" && <nul set /p=") (" call :PainText 0e "YELLOW is pee" && <nul set /p=") (" call :PainText 0F "WHITE all colors"&& <nul set /p=")" goto :end :PainText <nul set /p "=%DEL%" > "%~2" findstr /v /a:%1 /R "+" "%~2" nul del "%~2" > nul goto :eof :end echo. pause 

你可以使用cecho ..你也可以使用它嵌入你的脚本,所以你不必携带一个.com或.exe

http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files

您将需要回显ANSI转义代码序列以更改文本颜色: http : //en.wikipedia.org/wiki/ANSI_escape_code

这些转义代码的另一个很好的来源是http://ascii-table.com/ansi-escape-sequences.php

我们曾经用ANSI终端代码做这个。 不知道他们是否还在工作,但你可以尝试他们。

将下面的行放到桌面上名为ColourText.bas的文件中。

 Imports System Imports System.IO Imports System.Runtime.InteropServices Imports Microsoft.Win32 Public modulee 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 modulee 

保存它并在命令提示符下键入以下内容。

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

一个名为ColourText.exe的文件将出现在您的桌​​面上。 将其移动到Windows文件夹

要使用你必须使用两个字符代码来设置颜色,例如01不是1

 ColourText ColourOfText ColourOfTextWhenFinished Text 

EG通过不传递任何文本来设置白色的蓝色,然后在白色文本上显示红色,在灰色上显示蓝色。

 ColourText F1 F1 ColourText F2 71 This is green on white 

要么

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

CLS命令也变得有趣。 不带参数的Color命令将所有颜色都重置为启动颜色。

要获得颜色代码,请将以下数字加在一起。 在程序员模式下使用计算器。 这些是十六进制数字。 可以将它们加在一起,例如红+蓝+ FG强度= 13 = D。由于不使用10+,所以背景是黑色的。 颜色代码必须是两个字符,例如08不是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. 

要在Windows 10上正常工作,可以启用此标志: ENABLE_VIRTUAL_TERMINAL_PROCESSING

有了这个注册表项,你可以在默认情况下设置

[HKCU\Console] VirtualTerminalLevel dword 0x1

使用颜色命令。 容易和内置。

 Color 0F 

是黑色和白色

 Color 0A 

是黑色和绿色