返回列表 发帖

CMD/bat里使用彩色(示例+函数)

本帖最后由 hnfeng 于 2024-6-21 21:45 编辑

批处理使用彩色字符和背景,我知道的有三种:
1、使用第三方工具,例如 syba.exe,  CoColor.exe 等等,目前知道的只支持16色
2、使用 findstr,优点是可以在win10(不含)之前的系统中使用,缺点是可能有点字符会有问题。也是只支持16色
3、使用转义,优点是支持16色、256色、真彩色,缺点是只支持win10(含)以后的系统

这几天研究了下转义方式,在cmd/bat里面使用彩色,写了个函数方便自己使用,分享出来
批处理里面的演示部分,有的并没有使用函数,而是直接输出,你比较一下就知道怎么用,很简单的
@echo off
for /f "delims=#" %%i in ('prompt #$E#^&echo on^&for %%a in ^(1^) do rem') do set EscK=%%i
setlocal EnableDelayedExpansion&cd /d "%~dp0"&title %~nx0  [hnfeng]&color 07
mode con lines=40 cols=182
for /l %%i in (1,1,40) do set bar=!bar!=
:: 3/4位(8色/16色):
echo 3/4位(8色/16色):
echo %bar%
for /l %%i in (30,1,37) do set /p "str=%EscK%[%%i;40m %%i %EscK%[m"<nul
for /l %%i in (30,1,37) do set /p "str=%EscK%[1;%%i;40m %%i %EscK%[m"<nul
for /l %%i in (90,1,97) do set /p "str=%EscK%[%%i;40m %%i %EscK%[m"<nul
echo;
for /l %%i in (30,1,37) do set /p "str=%EscK%[7;%%i;40m %%i %EscK%[m"<nul
for /l %%i in (30,1,37) do set /p "str=%EscK%[7;1;%%i;40m %%i %EscK%[m"<nul
for /l %%i in (90,1,97) do set /p "str=%EscK%[7;%%i;40m %%i %EscK%[m"<nul
echo;&echo;
:: 8位(256色):
echo 8位(256色):
echo %bar%
set Invert=
call :DoDisp %Invert%
echo;&echo;
::(8位)256色背景(使用反显的方法)
set Invert=i
call :DoDisp %Invert%
echo;&echo;&echo;
:: 24位(RGB真彩色):
echo 24位(RGB真彩色):
echo %bar%
for /L %%r in (64,2,255) do (call :EchoC " " "38;2;%%r;0;0" x 40 i
  ping -n 1 127.1>nul)
echo;
for /L %%g in (64,2,255) do (call :EchoC " " "38;2;0;%%g;0" x 40 i
  ping -n 1 127.1>nul)
echo;
for /L %%b in (64,2,255) do (call :EchoC " " "38;2;0;0;%%b" x 40 i
  ping -n 1 127.1>nul)
echo;&echo;
for /L %%g in (0,8,255) do (
  set /p "str=%EscK%[30;48;2;255;%%g;0m %EscK%[m"<nul
  ping -n 1 127.1>nul)
for /L %%r in (255,-8,0) do (
  set /p "str=%EscK%[30;48;2;%%r;255;0m %EscK%[m"<nul
  ping -n 1 127.1>nul)
for /L %%b in (0,8,255) do (
  set /p "str=%EscK%[30;48;2;0;255;%%bm %EscK%[m"<nul
  ping -n 1 127.1>nul)
for /L %%g in (255,-8,0) do (
  set /p "str=%EscK%[30;48;2;0;%%g;255m %EscK%[m"<nul
  ping -n 1 127.1>nul)
echo;&echo;&echo;
endlocal
pause
exit /b
:DoDisp %Invert%
for /l %%i in (0,1,15) do (
    set /a text=1000+%%i
    rem call :EchoC " !text:~-3! " "38;5;%%i" x 40 %1
    call :EchoC " !text:~-3! " "38;5;%%i" x "48;5;0" %1
)
echo;
set /a N=36
set /a L=0
for /l %%i in (16,1,231) do (
    set /a text=1000+%%i
    call :EchoC " !text:~-3! " "38;5;%%i" x 40 %1
    rem call :EchoC " !text:~-3! " "38;5;%%i" x "48;5;0" %1
    set /a L+=1
    if !L! GEQ %N% (
      set /a L=0
      echo;
    )
)
for /l %%i in (232,1,255) do (
    set /a text=1000+%%i
    call :EchoC " !text:~-3! " "38;5;%%i" x 40 %1
    rem call :EchoC " !text:~-3! " "38;5;%%i" x "48;5;0" %1
)
goto :EOF
::===============================================================
:EchoC
::EchoC  <text> <ForegroundColor> [Return] [BackgroundColor] [Invert]
:: <text> ---- 字符
::  <ForegroundColor> 前景色
::    3/4位色-前景色      31                  范围 30-37
::    3/4位色-高亮前景色  "1;36"              范围 30-37
::                    或  91                  范围 90-97
::    8位(256色)          "38;5;100"          范围 0-255
::    24位(RGB色)         "38;2;255;128;128"  RGB 范围 0-255
::  <ForegroundColor> 背景色
::    3/4位色-背景色      41                  范围 40-47
::    3/4位色-高亮背景色  103                 范围 100-107
::    8位(256色)          "48;5;100"          范围 0-255
::    24位(RGB色)         "48;2;255;128;128"  RGB 范围 0-255
:: [Invert] 是否反显:I-反显,其他-不反显
:: [Return] 显示字符后是否换行:R-换行,其他-不换行
::
:: 例子:call :EchoC "Text 文字" 31
::       call :EchoC "Text 文字" "1;36" x 40 i
::       call :EchoC "Text 文字" "38;5;99" r
::       call :EchoC "Text 文字" "38;2;255;255;16" R "48;2;0;94;94"
::       call :EchoC "Text 文字" "38;2;255;128;64" R 46
::CMD中可使用下列命令显示全部256色("^["是"Ctrl+["):
::for /l %i in (0,1,255) do @echo ^[[38;5;%im_%i_%i_%i_%i_%i_%i_%i_%i_%i_%i^[[m
::for /l %i in (0,1,255) do @echo ^[[7;38;5;%im_%i_%i_%i_%i_%i_%i_%i_%i_%i_%i^[[m
setlocal EnableDelayedExpansion
if /i %5_ EQU I_ (
  set /p "str=%EscK%[7;%~2;%~4m%~1%EscK%[m"<nul
) else (
  if %4_ EQU _ (set /p "str=%EscK%[%~2;40m%~1%EscK%[m"<nul
  ) else (set /p "str=%EscK%[%~2;%~4m%~1%EscK%[m"<nul)
)
if /i %3_ EQU R_ echo;
endlocal&goto :EOF
::<ForegroundColor> 前景色
::[BackgroundColor] 背景色
::  1. 3/4位色(8/16色):
::    前景色 30-黑,31-红,32-绿,33-黄,34-蓝,35-品红,36-青,37-白
::      16色就是加上高亮的8
::          开始高亮前景  [1;
::          关闭高亮前景  [22;
::       90-灰,91-亮红,92-亮绿,93-亮黄,94-亮蓝,95-亮品红,96-亮青,97-亮白
::    背景色     40-黑,41-红,42-绿,43-黄,44-蓝,45-品红,46-青,47-白
::    高亮背景色 100-灰,101-亮红,102-亮绿,103-亮黄,104-亮蓝,105-亮品红,106-亮青,107-亮白
::  2. 8位色(256色):
::    256色前景色及引导 [38;5;n
::    256色背景色及引导 [48;5;n
::      n为颜色值:
::        0-黑,1-红,  2-绿,  3-黄,  4-蓝,  5-品红,  6-青,  7-白
::        8-灰, 9-亮红,10-亮绿,11-亮黄,12-亮蓝,13-亮品红,14-亮青,15-亮白
::        16231 :多种颜色
::        232255:灰度,从较黑到较白(0最黑,15最白)
::  3. 24位色(RGB):
::    24位色前景色及引导 [38;2;R;G;B
::    24位色背景色及引导 [48;2;R;G;B
::      0 <= R,G,B <=255
::  4. 反显:
::    开启  [7;
::    关闭  [27;
::  5. 下划线:
::    开启  [4;
::    关闭  [24;
::
:: 下面内容有部分试验不出来
::  恢复默认:[0m/[m ;粗体(高亮?):[1m ;亮度减半开关:[2m/[22m ;下划线开关:[4m/[24m ;闪烁开关:[5m/[25m
::  反显开关:[7m/[27m ;隐藏(前景背景色一样)开关:[8m/[28m ;划掉开关:[9m/[29m;上划线开关:[53m/[55m
::  30-373/4位色前景色; 38;5:设置前景色(8位);38;2:设置前景色(24位)
::  40-473/4位色背景色; 48;5:设置背景色(8位);48;2:设置背景色(24位)COPY

本帖最后由 hnfeng 于 2024-6-22 13:23 编辑

再给个例子
@echo off
for /f "delims=#" %%i in ('prompt #$E#^&echo on^&for %%a in ^(1^) do rem') do set EscK=%%i
setlocal EnableDelayedExpansion&cd /d "%~dp0"&title %~nx0&color 07
for /l %%v in (1,1,50) do (set bar=!bar!=)
echo;&echo;&call :EchoC "%Bar%" "38;5;33" r&echo;
For /f "tokens=2* delims==" %%s in ('wmic OS get Caption /value ^| findstr /i "Caption"') do set OS=%%s
call :EchoC "当前系统是:" "38;5;37"
call :EchoC "%OS%" "38;5;120" r
echo;&call :EchoC "%Bar%" "38;5;33" r&echo;&echo;
for /f "tokens=2 delims==" %%i in ('wmic path win32_operatingsystem get LocalDateTime /value ^| findstr "="') do (set "strDate=%%i")
echo;&echo 使用ECHO的方法
echo %EscK%[38;5;208m现在时间是:%EscK%[38;5;196m%strDate:~0,4%%EscK%[38;5;135m年%EscK%[38;5;202m%strDate:~4,2%%EscK%[38;5;135m月%EscK%[38;5;208m%strDate:~6,2%%EscK%[38;5;135m日%EscK%[38;5;214m%strDate:~8,2%%EscK%[38;5;135m点%EscK%[38;5;220m%strDate:~10,2%%EscK%[38;5;135m分%EscK%[38;5;226m%strDate:~12,2%%EscK%[38;5;135m秒%EscK%[m
echo;&echo 使用函数的方法
call :EchoC "现在时间是:" "38;5;208"
call :EchoC "%strDate:~0,4%" "38;5;196"
call :EchoC "年" "38;5;135"
call :EchoC "%strDate:~4,2%"  "38;5;202"
call :EchoC "月" "38;5;135"
call :EchoC "%strDate:~6,2%"  "38;5;208"
call :EchoC "日" "38;5;135"
call :EchoC "%strDate:~8,2%" "38;5;214"
call :EchoC "点" "38;5;135"
call :EchoC "%strDate:~10,2%" "38;5;220"
call :EchoC "分" "38;5;135"
call :EchoC "%strDate:~12,2%" "38;5;226"
call :EchoC "秒" "38;5;135" R
echo;&echo;
pause
exit /b
::===============================================================
:EchoC
setlocal EnableDelayedExpansion
if /i %5_ EQU I_ (set /p "str=%EscK%[7;%~2;%~4m%~1%EscK%[m"<nul
) else (if %4_ EQU _ (set /p "str=%EscK%[%~2;40m%~1%EscK%[m"<nul
  ) else (set /p "str=%EscK%[%~2;%~4m%~1%EscK%[m"<nul))
if /i %3_ EQU R_ echo;
endlocal&goto :EOFCOPY

TOP

这是逼着我用WIN10啊!
WIN7系统不支持转义符,但我有第三方工具echox,能实现简单的彩色字符。
semiuel 发表于 2024-6-23 19:51



    echox 确实也不错。
syba.exe 是最好的一类,可以指定坐标
echox.exe 是第二类,可以保留光标不换行
Cocoloer.exe 是第三类,总是换行光标下移

TOP

返回列表