Windows CMD更改一个字符的颜色



有没有办法在Windows命令提示符中只更改一个字符的颜色?我找了这个,但只找到了如何改变整个背景和前景。有没有办法只为一个字符实现Color命令?

感谢

这不是一个简单的命令,但Carlos创建了这个函数来帮助更改字符颜色。运行批处理文件,它会显示一些颜色变化。

@Echo Off
Call :Color A "######" n E "" C " 21 " E "!" n B "######" n
Pause >Nul
Exit /B
:Color
:: v21
:: Arguments: hexColor text [n] ...
:: n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd 
SetLocal DisableDelayedExpansion
If Not Exist `.7 (
Echo(|(Pause >Nul &Findstr "^" >`)
Set /P "=." >>` <Nul
For /F "delims=;" %%# In (
'"Prompt $H;&For %%_ In (_) Do Rem"') Do (
Set /P "=%%#%%#%%#" <Nul >`.3
Set /P "=%%#%%#%%#%%#%%#" <Nul >`.5
Set /P "=%%#%%#%%#%%#%%#%%#%%#" <Nul >`.7))
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
 / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If ==%%# (Findstr /A:%~1 . ` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.` Nul
Type `.5) Else (Echo %%#..`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Goto :Eof
Goto :__Color

如果您使用Windows 10,您也可以使用VT100序列来实现这一点:通过设置ESC字符开始您的文件

for /f "delims=" %%E in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x1B"') do (
    set "ESC=%%E"
)

现在,您可以使用它来相应地内联更改颜色:

echo This is %ESC%[36ma%ESC%[0m single character

给出

This is a single character

而a是蓝色的。你可以用它制作方便的宏,比如:

set "BLUE=%ESC%[36m"
set "ENDC=%ESC%[0m"

然后像这样使用它们:

echo This is also %BLUE%a%ENDC% single character

点击此处查看VT100 的更多信息

最新更新