本帖最后由 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-亮白
- :: 16 至 231 :多种颜色
- :: 232 至 255:灰度,从较黑到较白(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-37:3/4位色前景色; 38;5:设置前景色(8位);38;2:设置前景色(24位)
- :: 40-47:3/4位色背景色; 48;5:设置背景色(8位);48;2:设置背景色(24位)
复制代码
|