标题: [特效代码] 批处理指定某行颜色的原理是什么? [打印本页]
作者: 857185187 时间: 2019-12-12 10:03 标题: 批处理指定某行颜色的原理是什么?
- @echo off
- call :x c 第一行输出红色
- call :x a 第二行输出绿色
- call :x f 第三行输出白色
- pause
- goto :eof
- :x
- echo. >%2&findstr /a:%1 . %2*&del %2
复制代码
网上看到一个例子,每行可以有不同的颜色
看来是通过Findstr实现的,但是echo. 是什么意思?具体又是怎么实现的呢?
作者: a20150604 时间: 2019-12-12 13:33
回复 1# 857185187
你运行
findstr /?
看其帮助信息就知道了, 这是老的不借用非原生 exe 实现 cmd 窗口彩色输出的方案, 效率是很低的
如果 你用的 Win 10, 只要版本不是太老, 可以用 VT100 指令实现更好的彩色输出, 另外还能轻松实现窗口任意坐标的字符操作
下面是一个彩色指针时钟示例, 请在 WIN10 上运行, 包括 WIN8 在内, 老的 WINDOWS 控制台 都不支持 VT100- @echo off & setlocal enabledelayedexpansion
- >nul chcp 437
- (for /f "delims==" %%a in ('set') do set "%%a=") & set "Path=%SystemRoot%\system32"
- for /F %%a in ('echo prompt $E^| cmd') do set "_ESC=%%a"
- set "_PEN=#"
- set "_PEN_SCALE=*"
- set /a "_SIZE=51" & REM Set the size of the clock, recommended from 37 to 100
- set /a "_LOW_LIMIT_OF_WIDTH_OF_FIFTEEN_SEGMENT_DISPLAY=37"
- set /a "_s=(_SIZE-15)>>31, _SIZE=(_LOW_LIMIT_OF_WIDTH_OF_FIFTEEN_SEGMENT_DISPLAY&_s)+(_SIZE&~_s)" & REM size lower limit: 37
- set /a "_WID=_HEI=_SIZE|1,_R_FACE=_WID/2-1, _R_FACE_SQ=_R_FACE*_R_FACE, _R_FACE_1=_R_FACE-1,_R_FACE_2=_R_FACE-2"
- set /a "_LEFT_FIFTEEN_SEGMENT_DISPLAY=(_WID-_LOW_LIMIT_OF_WIDTH_OF_FIFTEEN_SEGMENT_DISPLAY)/2+1, _TOP_FIFTEEN_SEGMENT_DISPLAY=_WID/2+_R_FACE/4"
- color 0F & mode %_WID%,%_HEI%
- REM The work that needs "Path" is done, now you can clean it up.
- set "Path="
- set "_SIN=(#-#*#/1875*#/320000+#*#/1875*#/15625*#/16000*#/2560000-#*#/1875*#/15360*#/15625*#/15625*#/16000*#/44800000)"
- set "_COS=(10000-#*#/20000+#*#/1875*#/15625*#/819200-#*#/1875*#/15360*#/15625*#/15625*#/10240000+#*#/1875*#/15360*#/15625*#/15625*#/16000*#/15625*#/229376000)"
- REM 角度常量, 不直接用字面数值, 让 宏代码 定义更易读
- set /a "_PI=31416, _2PI=2*_PI, _PI#2=_PI/2, _3PI#2=3*_PI/2, _3PI#2_1=_3PI#2-1"
- set "_SIN(t)=(#=(t) %% %_2PI%, #+=#>>31&%_2PI%, #1=(#-%_PI#2%^#-%_3PI#2%)>>31, #3=%_3PI#2_1%-#>>31, #=(-#)+(#&~#1)+(%_PI%)+(-%_2PI%), %_SIN%)"
- set "_COS(t)=(#=(t) %% %_2PI%, #+=#>>31&%_2PI%, #1=(#-%_PI#2%^#-%_3PI#2%)>>31, #3=%_3PI#2_1%-#>>31, #=(-#)+(#&~#1)+(%_PI%)+(-%_2PI%), #=%_COS%, (-#)+(#&~#1))"
- set "_SIN(t),_COS(t)=(#=(t) %% %_2PI%, #+=#>>31&%_2PI%, #1=(#-%_PI#2%^#-%_3PI#2%)>>31, #3=%_3PI#2_1%-#>>31, #=(-#)+(#&~#1)+(%_PI%)+(-%_2PI%), #S=%_SIN%, #=%_COS%, #C=(-#)+(#&~#1))"
- set /a "_DEG=_PI/180, _6DEG=6*_PI/180, _30DEG=30*_PI/180, _3.6DEG=36*_PI/(180*10)"
- set /a "_XCZOOM = 10000 * _WID/2, _XC=_WID/2+1, _YCZOOM = 10000 * _HEI/2, _YC=_HEI/2+1, _TH0=-_PI#2"
- set /a "_2XC=2*_XC, _2YC=2*_YC"
- <nul set /p "=%_ESC%[?25l" & REM ESC [ ? 25 l DECTCEM Text Cursor Enable Mode Hide Hide the cursor
- set "_RGB_SCALE=0;0;255"
- set "_RGB_FACE=255;255;255"
- set "$erase_last_pin="
- set /a "_DENSITY=150, _SPEED=3*%_DEG%, th=_TH0"
- set /a "_CENTISECONDS_OF_A_DAY=24*60*60*100"
- set "_LEFT37DOWN1=%_ESC%[37D%_ESC%[1B"
- REM 每 _GAP 帧计算一次 FPS, ! _GAP 必须是 2 的幂, 并且不小于 2
- set /a "_GAP=2<<5"
- (
- for /f "delims==" %%a in ('set _') do set "%%a="
- set /a "_PIN_LEN_S=%_R_FACE%-3,_PIN_LEN_M=_PIN_LEN_S-1,_PIN_LEN_H=_PIN_LEN_S/2+%_SIZE%/15,_PIN_LEN_C=_PIN_LEN_S/4-0"
- set "_RGB_C=0;255;0"
- set "_RGB_S=255;0;0"
- set "_RGB_M=100;100;100"
- set "_RGB_H=0;0;0"
- <nul set /p "=%_ESC%[48;2;%_RGB_FACE%m"
- REM gen clock dial: Distance method, quick but not meticulous
- for /L %%x in (%_XC% -1 1) do (
- for /L %%y in (%_YC% -1 1) do (
- set /a "_dx=%%x-%_XC%, _dy=%%y-%_YC%, t=_dx*_dx+_dy*_dy-%_R_FACE_SQ%-1"
- if !t! lss 0 (
- set /a "#x_=%_2XC%-%%x, #y_=%_2YC%-%%y"
- set "$pin=%_ESC%[%%y;!#x_!H%_PEN%%_ESC%[!#y_!;%%xH%_PEN%%_ESC%[!#y_!;!#x_!H%_PEN%%_ESC%[%%y;%%xH%_PEN%!$pin!"
- )
- )
- set "$pin=%_ESC%[38;2;%_RGB_FACE%m!$pin!"
- <nul set /p "=!$pin!"
- set "$pin="
- )
- REM gen clock dial: rotary scanning polishing edge
- for /L %%i in (0 1 %_DENSITY%) do (
- set /a "th+=%_SPEED%, %_SIN(t),_COS(t):t=th%, #x=(%_XCZOOM%+%_R_FACE%*#C)/10000+1, #y=(%_YCZOOM%+%_R_FACE%*#S)/10000+1, #x_=%_2XC%-#x, #y_=%_2YC%-#y"
- set "$pin=%_ESC%[!#y!;!#x_!H%_PEN%%_ESC%[!#y_!;!#x!H%_PEN%%_ESC%[!#y_!;!#x_!H%_PEN%%_ESC%[!#y!;!#x!H%_PEN%!$pin!"
- set "$pin=%_ESC%[38;2;%_RGB_FACE%m!$pin!"
- <nul set /p "=!$pin!"
- set "$pin="
- )
- REM nail up scale
- <nul set /p "=%_ESC%[48;2;%_RGB_FACE%m"
- for /L %%i in (0 1 3) do (
- set /a "r3=%%i %% 3"
- set /a "th=-%_PI#2% + %%i*%_2PI%/12, %_SIN(t),_COS(t):t=th%, $x=%_XCZOOM%-#C, $y=%_YCZOOM%-#S"
- for /l %%a in (0 1 %_R_FACE%) do (
- set /a "#x=($x+=#C)/10000+1, #y=($y+=#S)/10000+1, #x_=%_2XC%-#x, #y_=%_2YC%-#y"
- if !r3!==0 (
- if %%a geq %_R_FACE_2% if %%a lss %_R_FACE% (
- set "$pin=%_ESC%[!#y!;!#x!H%_PEN_SCALE%%_ESC%[!#y_!;!#x_!H%_PEN_SCALE%%_ESC%[!#y!;!#x_!H%_PEN_SCALE%%_ESC%[!#y_!;!#x!H%_PEN_SCALE%!$pin!"
- )
- ) else (
- if %%a equ %_R_FACE_1% (
- set "$pin=%_ESC%[!#y!;!#x!H%_PEN_SCALE%%_ESC%[!#y_!;!#x_!H%_PEN_SCALE%%_ESC%[!#y!;!#x_!H%_PEN_SCALE%%_ESC%[!#y_!;!#x!H%_PEN_SCALE%!$pin!"
- )
- )
- )
- set "$pin=%_ESC%[38;2;%_RGB_SCALE%m!$pin!"
- <nul set /p "=!$erase_last_pin!!$pin!"
- set "$pin="
- )
- <nul set /p "=%_ESC%[48;2;%_RGB_FACE%m"
- set /a "_cnt=0, $v=0"
- for /L %%i in () do (
- set "tm=!time: =0!" & set /a "SS=1!tm:~6,2!-100, MM=1!tm:~3,2!-100, HH=1!tm:~0,2!-100, CC=1!tm:~-2!-100"
- set /a "th_S=-%_PI#2% + (SS * 100 + CC) * %_6DEG% / 100, th_M=-%_PI#2% + (MM * 60 + SS) * %_DEG% / 10, th_H=-%_PI#2% + ((HH * 60 + MM) * 60 + SS) * %_DEG% / 120, th_C=-%_PI#2% + CC*%_3.6DEG%"
- REM Draw 4 pointers
- for %%K in (C S M H) do (
- set /a "th=th_%%K, %_SIN(t),_COS(t):t=th%, $x=%_XCZOOM%-#C, $y=%_YCZOOM%-#S"
- for /l %%a in (0 1 !_PIN_LEN_%%K!) do (
- set /a "#x=($x+=#C)/10000+1, #y=($y+=#S)/10000+1"
- set "$pin=%_ESC%[!#y!;!#x!H%_PEN%!$pin!"
- )
- set "$pin=%_ESC%[38;2;!_RGB_%%K!m!$pin!"
- )
- <nul set /p "=!$erase_last_pin!!$pin!"
- set "$erase_last_pin=!$pin:%_PEN%= !"
- set "$pin="
- title !tm! pointer_clock_WIN10
- set /a "t=-((_cnt+=1)&(%_GAP%-1))>>31, $$=($u=((HH*60+MM)*60+SS)*100+CC)-$v, $$+=$$>>31&%_CENTISECONDS_OF_A_DAY%, $$=(~t&$$)+(t&1), FPS=(~t&(100*%_GAP%/$$))+(t&FPS), $v=(~t&$u)+(t&$v)"
- if !t!==0 (
- <nul set /p "=%_ESC%[48;2;0;0;0m%_ESC%[1;1HFPS:!FPS! %_ESC%[48;2;%_RGB_FACE%m"
- )
- )
- )
- >nul pause
- exit
复制代码
作者: RandolphNiu 时间: 2019-12-12 16:43
回复 2# a20150604
这个批处理太66
作者: 857185187 时间: 2019-12-13 09:36
回复 2# a20150604
WIN7和XP上用的
作者: RandolphNiu 时间: 2019-12-14 10:23
本帖最后由 RandolphNiu 于 2019-12-14 10:25 编辑
示例:
set/p= <nul>示例文字
findstr /a:f1 . 示例文字?
del 示例文字?
效果:显示白色背景黑色前景的“示例文字”
注释:可以多次利用此方法在一行内显示多种不同前景色和背景色的文字
在没有发现这个用途之前,findstr的开关/a可以说是“蛇足”,它在文档中虽有说明,
但语焉不详,更别说如此奇诡的用途;/a: 后的字符可以根据color /?的说明进行调整
原文链接http://bbs.bathome.net/thread-7629-1-1.html
作者: cmd1152 时间: 2021-10-23 22:44
回复 2# a20150604
不行.......我的电脑不支持ANSI重构(我眼睛要瞎了)
27H#←[26;26H#←[38;2;0;255;0m←[28;31H#←[27;30H#←[27;
29H#←[27;28H#←[26;27H#←[26;26H#←[38;2;0;0;0m←[16;18
H ←[17;18H ←[18;19H ←[18;20H ←[19;20H ←[20;21H ←[21
;21H ←[21;22H ←[22;23H ←[23;23H ←[24;24H ←[24;25H ←
[25;25H ←[26;26H ←[38;2;100;100;100m←[28;6H ←[28;7H
←[28;8H ←[28;9H ←[28;10H ←[28;11H ←[27;12H ←[27;13
H ←[27;14H ←[27;15H ←[27;16H ←[27;17H ←[27;18H ←[27
;19H ←[27;20H ←[27;21H ←[26;22H ←[26;23H ←[26;24H ←
[26;25H ←[26;26H ←[38;2;255;0;0m←[14;43H ←[15;43H ←
[15;42H ←[16;41H ←[16;40H ←[17;39H ←[18;38H ←[18;38
H ←[19;37H ←[19;36H ←[20;35H ←[20;34H ←[21;33H ←[22
;33H ←[22;32H ←[23;31H ←[23;30H ←[24;29H ←[24;28H ←
[25;28H ←[25;27H ←[26;26H ←[38;2;0;255;0m←[28;31H ←
[27;30H ←[27;29H ←[27;28H ←[26;27H ←[26;26H ←[38;2;
0;0;0m←[16;18H#←[17;18H#←[18;19H#←[18;20H#←[19;20H#
←[20;21H#←[21;21H#←[21;22H#←[22;23H#←[23;23H#←[24;2
4H#←[24;25H#←[25;25H#←[26;26H#←[38;2;100;100;100m←[
28;6H#←[28;7H#←[28;8H#←[28;9H#←[28;10H#←[28;11H#←[2
7;12H#←[27;13H#←[27;14H#←[27;15H#←[27;16H#←[27;17H#
←[27;18H#←[27;19H#←[27;20H#←[27;21H#←[26;22H#←[26;2
3H#←[26;24H#←[26;25H#←[26;26H#←[38;2;255;0;0m←[14;4
3H#←[15;43H#←[15;42H#←[16;41H#←[16;40H#←[17;39H#←[1
8;38H#←[18;38H#←[19;37H#←[19;36H#←[20;35H#←[20;34H#
←[21;33H#←[22;33H#←[22;32H#←[23;31H#←[23;30H#←[24;2
9H#←[24;28H#←[25;28H#←[25;27H#←[26;26H#←[38;2;0;255
;0m←[29;30H#←[28;29H#←[28;29H#←[27;28H#←[27;27H#←[2
6;26H#←[38;2;0;0;0m←[16;18H ←[17;18H ←[18;19H ←[18;
20H ←[19;20H ←[20;21H ←[21;21H ←[21;22H ←[22;23H ←[
23;23H ←[24;24H ←[24;25H ←[25;25H ←[26;26H ←[38;2;1
00;100;100m←[28;6H ←[28;7H ←[28;8H ←[28;9H ←[28;10H
←[28;11H ←[27;12H ←[27;13H ←[27;14H ←[27;15H ←[27;
16H ←[27;17H ←[27;18H ←[27;19H ←[27;20H ←[27;21H ←[
26;22H ←[26;23H ←[26;24H ←[26;25H ←[26;26H ←[38;2;2
55;0;0m←[14;43H ←[15;43H ←[15;42H ←[16;41H ←[16;40H
←[17;39H ←[18;38H ←[18;38H ←[19;37H ←[19;36H ←[20;
35H ←[20;34H ←[21;33H ←[22;33H ←[22;32H ←[23;31H ←[
23;30H ←[24;29H ←[24;28H ←[25;28H ←[25;27H ←[26;26H
←[38;2;0;255;0m←[29;30H ←[28;29H ←[28;29H ←[27;28H
←[27;27H ←[26;26H ←[38;2;0;0;0m←[16;18H#←[17;18H#←
[18;19H#←[18;20H#←[19;20H#←[20;21H#←[21;21H#←[21;22
H#←[22;23H#←[23;23H#←[24;24H#←[24;25H#←[25;25H#←[26
;26H#←[38;2;100;100;100m←[28;6H#←[28;7H#←[28;8H#←[2
8;9H#←[28;10H#←[28;11H#←[27;12H#←[27;13H#←[27;14H#←
[27;15H#←[27;16H#←[27;17H#←[27;18H#←[27;19H#←[27;20
H#←[27;21H#←[26;22H#←[26;23H#←[26;24H#←[26;25H#←[26
;26H#←[38;2;255;0;0m←[14;43H#←[15;43H#←[15;42H#←[16
;41H#←[17;40H#←[17;39H#←[18;38H#←[18;38H#←[19;37H#←
[19;36H#←[20;35H#←[20;34H#←[21;33H#←[22;33H#←[22;32
H#←[23;31H#←[23;30H#←[24;29H#←[24;28H#←[25;28H#←[25
;27H#←[26;26H#←[38;2;0;255;0m←[30;29H#←[29;29H#←[28
;28H#←[27;27H#←[27;27H#←[26;26H#
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |