我知道该color bf
命令设置整个命令行窗口的颜色,但是我想用另一种颜色打印一行。
我知道该color bf
命令设置整个命令行窗口的颜色,但是我想用另一种颜色打印一行。
Answers:
我想用另一种颜色打印一行。
使用ANSI转义序列。
Windows 10之前的版本-控制台上不支持ANSI颜色
对于低于10的Windows版本,默认情况下Windows命令控制台不支持输出颜色。您可以安装Cmder,ConEmu,ANSICON或Mintty(默认情况下在GitBash和Cygwin中使用)以向Windows命令控制台添加颜色支持。
Windows 10-命令行颜色
从Windows 10开始,Windows控制台默认支持ANSI转义序列和某些颜色。2015年11月,Threshold 2 Update随附了此功能。
更新(05-2019):使用ColorTool可以更改控制台的配色方案。它是Microsoft Terminal项目的一部分。
演示版
批处理命令
该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
.cmd
或.bat
-两者都应该起作用。但是,我想我知道为什么它不起作用:似乎在发布到StackOverflow时删除了转义符号。:请不要将内容从我的答案复制,使用内容从这里开始,而不是gist.githubusercontent.com/mlocati/...
ESC
在记事本+ +可以使用数字键盘和左ALT键做到这一点与ALT-码之前...:L-ALT
+ 0
+ 2
+7
ESC
以下方式在批处理文件中生成字符:for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
这是一个自编译的bat / .net混合文件(应另存为.BAT
),可以在已安装.net框架的任何系统上使用(即使对于最旧的XP / 2003,看到没有.NET框架的窗口也很罕见)装置)。它使用jscript.net编译器创建一个exe,该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
这是个旧答案,但我想我要澄清一下并简化一下
自7以来,PowerShell现在已包含在 Windows的所有版本中。因此,可以将此答案的语法简化为更简单的形式:
-fore
代替-foregroundcolor
-back
代替-backgroundcolor
echo
powershell write-host -fore Cyan This is Cyan text
powershell write-host -back Red This is Red background
颜色和更多信息的完整列表可在
- PowerShell中的文档Write-Host
Windows 10-TH2及以上:
(aka版本1511,内部版本10586,版本2015-11-10)
在命令提示符下:
echo ^[[32m HI ^[[0m
使用实际按键:echo Ctrl+ [[32m HI
Ctrl+[[0m
Enter
您应该在其下方看到绿色的“ HI”。
代码编号可以在这里找到:
记事本:
要将其保存到记事本中,可以使用以下命令在其中键入ESC:Alt+ 027
数字键盘,然后输入[32m
零件。当我在笔记本电脑上时,另一个技巧是将上面的行重定向到一个文件中以开始使用,然后剪切并粘贴:
echo echo ^[[32m HI ^[[0m >> batch_file.cmd
\033
但是不行...我如何在Java中做到这一点?
您可以仅使用要打印的单词名称创建文件,使用可以彩色打印的findstr,然后删除文件。试试这个例子:
@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"
)
call :ColorText 0a "green"
call :ColorText 0C "red"
call :ColorText 0b "cyan"
echo(
call :ColorText 19 "blue"
call :ColorText 2F "white"
call :ColorText 4e "yellow"
goto :eof
:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
运行color /?
以获取颜色列表。
您可以使用ANSICON在较早版本的Windows中启用ANSI终端代码。我在Windows XP和Windows 7中使用了32位和64位版本。
cmd也缺少适当的着色,这让我很烦恼,所以我继续创建cmdcolor。它只是一个stdout代理,它寻找一组有限的ANSI / VT100控制序列(换句话说,就像在bash中一样),即echo \033[31m RED \033[0m DEFAULT | cmdcolor.exe
。
cmdcolor.exe; PE32 executable for MS Windows (console) Intel 80386 32-bit
upx
。您能通过电子邮件与我联系吗?
我之所以这样看,是因为我想向Win7 Batch文件中引入一些简单的文本颜色。这就是我想出的。谢谢你的帮助。
@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
有一个已经接受的答案,已经有250多个投票。我仍然做出贡献的原因是,escape
许多编辑器不接受回显所需的字符(例如,我正在使用MS Code),所有其他解决方案都需要某些第三方(非Windows默认)软件。
仅使用普通批处理命令的解决方法是使用PROMPT
而不是ECHO
。该PROMPT
命令escape
以对任何编辑者友好的方式接受字符作为$E
字符序列。(只需将ASCII转义码Esc
中的替换为)。$E
这是一个演示代码:
@ECHO OFF
:: Do not pollute environment with the %prompt.bak% variable
:: ! forgetting ENDLOCAL at the end of the batch leads to prompt corruption
SETLOCAL
:: Old prompt settings backup
SET prompt.bak=%PROMPT%
:: Entering the "ECHO"-like section
:: Forcing prompt to display after every command (see below)
ECHO ON
:: Setting the prompt using the ANSI Escape sequence(s)
:: - Always start with $E[1A, otherwise the text would appear on a next line
:: - Then the decorated text follows
:: - And it all ends with $E30;40m, which makes the following command invisible
:: - assuming default background color of the screen
@ PROMPT $E[1A$E[30;42mHELLO$E[30;40m
:: An "empty" command that forces the prompt to display.
:: The word "rem" is displayed along with the prompt text but is made invisible
rem
:: Just another text to display
@ PROMPT $E[1A$E[33;41mWORLD$E[30;40m
rem
:: Leaving the "ECHO"-like section
@ECHO OFF
:: Or a more readable version utilizing the cursor manipulation ASCII ESC sequences
:: the initial sequence
PROMPT $E[1A
:: formating commands
PROMPT %PROMPT%$E[32;44m
:: the text
PROMPT %PROMPT%This is an "ECHO"ed text...
:: new line; 2000 is to move to the left "a lot"
PROMPT %PROMPT%$E[1B$E[2000D
:: formating commands fro the next line
PROMPT %PROMPT%$E[33;47m
:: the text (new line)
PROMPT %PROMPT%...spreading over two lines
:: the closing sequence
PROMPT %PROMPT%$E[30;40m
:: Looks like this without the intermediate comments:
:: PROMPT $E[1A
:: PROMPT %PROMPT%$E[32;44m
:: PROMPT %PROMPT%This is an "ECHO"ed text...
:: PROMPT %PROMPT%$E[1B$E[2000D
:: PROMPT %PROMPT%$E[33;47m
:: PROMPT %PROMPT%...spreading over two lines
:: PROMPT %PROMPT%$E[30;40m
:: show it all at once!
ECHO ON
rem
@ECHO OFF
:: End of "ECHO"-ing
:: Setting prompt back to its original value
:: - We prepend the settings with $E[37;40m in case
:: the original prompt settings do not specify color
:: (as they don't by default).
:: - If they do, the $E[37;40m will become overridden, anyway.
:: ! It is important to write this command
:: as it is with `ENDLOCAL` and in the `&` form.
ENDLOCAL & PROMPT $E[37;40m%prompt.bak%
EXIT /B 0
注意:唯一的缺点是,color
如果未明确知道,此技术会与用户cmd颜色设置(命令或设置)相冲突。
-希望这会有所帮助,因为这是开头提到的原因,这是我唯一可以接受的解决方案。-
编辑:
根据评论,我附上另一个受@Jeb启发的代码段。它:
ECHO
命令PROMPT
价值ECHO
输出着色不可避免地会影响PROMPT
颜色,因此无论如何必须重置颜色@ECHO OFF
:: ! To observe color effects on prompt below in this script
:: run the script from a fresh cmd window with no custom
:: prompt settings
:: Only not to pollute the environment with the %\e% variable (see below)
:: Not needed because of the `PROMPT` variable
SETLOCAL
:: Parsing the `escape` character (ASCII 27) to a %\e% variable
:: Use %\e% in place of `Esc` in the [http://ascii-table.com/ansi-escape-sequences.php]
FOR /F "delims=#" %%E IN ('"prompt #$E# & FOR %%E IN (1) DO rem"') DO SET "\e=%%E"
:: Demonstrate that prompt did not get corrupted by the previous FOR
ECHO ON
rem : After for
@ECHO OFF
:: Some fancy ASCII ESC staff
ECHO [ ]
FOR /L %%G IN (1,1,10) DO (
TIMEOUT /T 1 > NUL
ECHO %\e%[1A%\e%[%%GC%\e%[31;43m.
ECHO %\e%[1A%\e%[11C%\e%[37;40m]
)
:: ECHO another decorated text
:: - notice the `%\e%[30C` cursor positioning sequence
:: for the sake of the "After ECHO" test below
ECHO %\e%[1A%\e%[13C%\e%[32;47mHELLO WORLD%\e%[30C
:: Demonstrate that prompt did not get corrupted by ECHOing
:: neither does the cursor positioning take effect.
:: ! But the color settings do.
ECHO ON
rem : After ECHO
@ECHO OFF
ENDLOCAL
:: Demonstrate that color settings do not reset
:: even when out of the SETLOCAL scope
ECHO ON
rem : After ENDLOCAL
@ECHO OFF
:: Reset the `PROMPT` color
:: - `PROMPT` itself is untouched so we did not need to backup it.
:: - Still ECHOING in color apparently collide with user color cmd settings (if any).
:: ! Resetting `PROMPT` color this way extends the `PROMPT`
:: by the initial `$E[37;40m` sequence every time the script runs.
:: - Better solution then would be to end every (or last) `ECHO` command
:: with the `%\e%[37;40m` sequence and avoid setting `PROMPT` altogether.
:: which makes this technique preferable to the previous one (before EDIT)
:: - I am keeping it this way only to be able to
:: demonstrate the `ECHO` color effects on the `PROMPT` above.
PROMPT $E[37;40m%PROMPT%
ECHO ON
rem : After PROMPT color reset
@ECHO OFF
EXIT /B 0
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "\e=%%E"
set ASCII27=←
的for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ASCII27=%%E"
。这将使用提示命令创建的转义符,而无需永久更改提示(因为该prompt
命令在子进程中执行)。顺便说一句。您echos.bat
有一些优化的潜力……
我要添加一个答案来解决上面的一些评论中提到的问题:内联ansi颜色代码在FOR循环内(实际上,在任何带括号的代码块内)可能会出现异常。 下面的.bat代码演示(1)内联颜色代码的使用;(2)在FOR循环中或括号内的代码块中使用内联颜色代码时可能发生的颜色故障;以及(3)解决方案问题。 当.bat代码执行时,测试2和3证明颜色代码失败,而测试4没有失败,因为它实现了解决方案。
[编辑2020-04-07:我发现了另一个解决方案,它可能比调用子例程更有效。将FINDSTR短语括在括号中,如以下行所示:
echo success | (findstr /R success)
ENDEDIT]
注意:根据我的经验(有限的经验),只有将输入通过管道传递到代码块内的FINDSTR后,颜色代码问题才会出现。这就是下面的.bat重现此问题的方式。 颜色代码问题可能比传递给FINDSTR之后更普遍。如果有人可以解释问题的性质,并且有解决此问题的更好方法,我将不胜感激。
@goto :main
:resetANSI
EXIT /B
rem The resetANSI subroutine is used to fix the colorcode
rem bug, even though it appears to do nothing.
:main
@echo off
setlocal EnableDelayedExpansion
rem Define some useful colorcode vars:
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESCchar=%%E"
set "green=%ESCchar%[92m"
set "yellow=%ESCchar%[93m"
set "magenta=%ESCchar%[95m"
set "cyan=%ESCchar%[96m"
set "white=%ESCchar%[97m"
set "black=%ESCchar%[30m"
echo %white%Test 1 is NOT in a FOR loop nor within parentheses, and color works right.
echo %yellow%[Test 1] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.%yellow%
echo %green%This is green.
echo %cyan%Test 1 completed.
echo %white%Test 2 is within parentheses, and color stops working after the pipe to FINDSTR.
( echo %yellow%[Test 2] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 2 completed.
echo %white%Test 3 is within a FOR loop, and color stops working after the pipe to FINDSTR.
for /L %%G in (3,1,3) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 3 completed.
echo %white%Test 4 is in a FOR loop but color works right because subroutine :resetANSI is
echo called after the pipe to FINDSTR, before the next color code is used.
for /L %%G in (4,1,4) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
call :resetANSI
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.
echo %green%This is green.
)
echo %cyan%Test 4 completed.%white%
EXIT /B
您可以使用cecho ..您也可以使用它直接嵌入到脚本中,因此您不必携带.com或.exe
http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files
我刚刚从Win 7 Home转换为Win 10 Pro,并想替换我从其他批次调用的批次以回显彩色信息。回顾上面讨论的内容,我将使用以下内容将直接替换之前的内容。注意在消息中添加“〜”,以便可以使用带空格的消息。我不需要记住代码,而是使用字母作为所需的颜色。
如果%2包含空格,则需要“ ...”%1黑色强烈颜色:R =红色G =绿色Y =黄色W =白色
ECHO OFF
IF "%1"=="R" ECHO ^[91m%~2[0m
IF "%1"=="G" ECHO ^[92m%~2[0m
IF "%1"=="Y" ECHO ^[93m%~2[0m
IF "%1"=="W" ECHO ^[97m%~2[0m
您需要回显ANSI转义码序列以更改文本颜色:http : //en.wikipedia.org/wiki/ANSI_escape_code
这些转义代码的另一个很好的来源是http://ascii-table.com/ansi-escape-sequences.php
将以下行放入ColourText.bas
桌面上的文件中。
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
保存它,然后在命令提示符下键入以下内容。
"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
not 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.
就像Glenn Slayden在此答案中所说的,您可以将适当的值添加到注册表中,以使cmd“更加丰富多彩”。
幸运的是,可以将全局默认值从“选择加入”更改为“选择退出”。HKEY_CURRENT_USER \ Console \ VirtualTerminalLevel处的注册表项设置用于处理ANSI转义序列的全局默认行为。默认情况下,创建一个DWORD键(如有必要)并将其值设置为1以全局启用(或0以禁用`)ANSI处理。
call :color_echo "blue" "blue txt"
call :color_echo "red" "red txt"
echo "white txt"
REM : https://www.robvanderwoude.com/ansi.php
:color_echo
@echo off
set "color=%~1"
set "txt=%~2"
set ESC=
set black=%ESC%[30m
set red=%ESC%[31m
set green=%ESC%[32m
set yellow=%ESC%[33m
set blue=%ESC%[34m
set magenta=%ESC%[35m
set cyan=%ESC%[36m
set white=%ESC%[37m
if "%~1" == "black" set "color=!black!"
if "%~1" == "red" set "color=!red!"
if "%~1" == "green" set "color=!green!"
if "%~1" == "yellow" set "color=!yellow!"
if "%~1" == "blue" set "color=!blue!"
if "%~1" == "magenta" set "color=!magenta!"
if "%~1" == "cyan" set "color=!cyan!"
if "%~1" == "white" set "color=!white!"
echo | set /p="!color!!txt!"
echo.
REM : return to standard white color
echo | set /p="!white!"
REM : exiting the function only
EXIT /B 0
%ESC%
为空,因此将无法正常工作。echo !white!
将颜色设置为白色。要返回默认颜色(无论用户设置是什么):color
不带参数的命令将执行该操作。
我们曾经使用ANSI终端代码来执行此操作。不知道它们是否仍然有效,但是您可以尝试使用它们。
您可以使用color命令更改整个控制台的颜色
Color 0F
是黑白的
Color 0A
是黑色和绿色