英文:
How do I display the output of the color the user has selected while displaying the name of the color selected?
问题
最近我问过如何在命令提示符窗口中显示用户选择的颜色代码,称为"是否可以使用批处理编程在命令提示符中输出当前颜色代码?"。我想要花一点时间感谢那些回答了我的人。非常感谢!
现在我有一个关于同一主题的另一个问题。首先,我想说我找到了一个非常简单、直接的 .bat 文件,可以告诉你可以选择的颜色列表,并允许你将背景和文本更改为任何颜色。然后,一旦你做出选择并按下"Enter"键,代码将会告诉你选择了颜色 0a,这是因为我选择了黑色背景和绿色文本 (0a) 作为我的选择,代码如下:echo: You choose %color%
。
但是,我想更详细地解释一下。我想让文件分解用户所做的颜色选择,而不是输出实际的颜色代码,我希望它在输出颜色代码的顶部输出颜色名称。我想要像这样的东西:"你选择了颜色 0 = 黑色 [背景] 和 A = [文本]。我确实希望在echo
命令中使用方括号,以便用户在命令提示符窗口上看到方括号。
这能在批处理代码中完成吗?
英文:
Recently I have asked about displaying the color code the user has chosen on the command prompt window itself called "Is it possible to output the current color code on the command prompt using batch programming?
" I would like to take a moment and thank those who have responded to that. Thank you very much!
Now I have another question on the same topic. First I would like to say that I had found a really simple, straightforward .bat file that tell you the listing of colors you can choose from, and allows you to change the background and text to whatever color you want. Then once you have made your selection and hit "enter", the codes will say you choose color 0a, that's because I had picked the black background with the green text (0a) as my selection, and the code for that is echo: You choose %color%
.
However, I would like to break that down more. I would like the file to breakdown the color selection the user has made and instead of outputting the actual color code, I would like it to output the name on top of the color code being output. I would like to be able to have something like this: "You chose color 0 = Black [Background] and A = [Text]. I do would like to have the square brackets in the echo
command to have the user see the square brackets on the command prompt window.
Can this be done in batch code?
答案1
得分: 1
@ECHO OFF
MODE CON: cols=110 lines=40
REM - 必需
SETLOCAL EnableDelayedExpansion
REM - 用于颜色功能中删除提示符。
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
REM - 代码选择功能。 Choice 用于独立选择前景/背景的颜色代码
:ColorSelect
cls
CHOICE /N /C:abcdef0123456789 /M "选择背景颜色:A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! bgcode bgcolor
REM - 使用Call发送三个参数到:TESTCC。 <errorlevel> <codeposition> <positioncolor>
REM - 第一个参数具有需要测试的值,其他两个参数是要定义的变量,以在:TESTCC中使用。
CHOICE /N /C:abcdef0123456789 /M "选择前景颜色:A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! fgcode fgcolor
REM - 检查是否选择了匹配的颜色代码
IF "!bgcode!"=="!fgcode!" (
ECHO 颜色不能匹配。
Timeout 2 > nul
GOTO ColorSelect
)
cls
REM - 在标准颜色中显示,用于在难以区分颜色的情况下显示消息
REM - 调用函数以使用所选颜色打印消息。
ECHO 颜色 !bgcode!!fgcode! 背景代码 [!bgcode!] 颜色 [!bgcolor!] 前景代码 [!fgcode!] 颜色 [!fgcolor!]
CALL :colorize "!bgcode!!fgcode!" "颜色 !bgcode!!fgcode! 背景代码 [!bgcode!] 颜色 [!bgcolor!] 前景代码 [!fgcode!] 颜色 [!fgcolor!]"
ECHO.
pause
GOTO ColorSelect
REM - 使用Call发送的参数定义变量以供使用和显示
:TESTCC
IF %~1==1 Set "%~2=a" && Set "%~3=亮绿"
IF %~1==2 Set "%~2=b" && Set "%~3=亮青"
IF %~1==3 Set "%~2=c" && Set "%~3=亮红"
IF %~1==4 Set "%~2=d" && Set "%~3=亮紫"
IF %~1==5 Set "%~2=e" && Set "%~3=亮黄"
IF %~1==6 Set "%~2=f" && Set "%~3=亮白"
IF %~1==7 Set "%~2=0" && Set "%~3=黑色"
IF %~1==8 Set "%~2=1" && Set "%~3=蓝色"
IF %~1==9 Set "%~2=2" && Set "%~3=绿色"
IF %~1==10 Set "%~2=3" && Set "%~3=青色"
IF %~1==11 Set "%~2=4" && Set "%~3=红色"
IF %~1==12 Set "%~2=5" && Set "%~3=紫色"
IF %~1==13 Set "%~2=6" && Set "%~3=黄色"
IF %~1==14 Set "%~2=7" && Set "%~3=白色"
IF %~1==15 Set "%~2=8" && Set "%~3=灰色"
IF %~1==16 Set "%~2=9" && Set "%~3=亮蓝"
exit /b 0
REM - 颜色化功能。使用方法:
REM - CALL :Colorize colorcodes "带引号的字符串"
REM - 该函数通过创建一个以字符串命名的临时文件并以指定颜色打印它来工作。
REM - 注意:不能使用文件名中保留用于文件名的字符。删除尾随空格。
:Colorize
IF NOT EXIST "%TEMP%\colorize" md "%TEMP%\colorize"
PUSHD %TEMP%\colorize
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
POPD
goto :eof
存在许多不同的显示信息的方法,只是选择适当的方式。
英文:
@ECHO OFF
MODE CON: cols=110 lines=40
REM - Required
SETLOCAL EnableDelayedExpansion
REM - Used in Colorize Function to Remove the Prompt.
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
REM - Code selection Function. Choice is used to select Colorcodes Independantly for FG/BG
:ColorSelect
cls
CHOICE /N /C:abcdef0123456789 /M "Select Background Color: A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! bgcode bgcolor
REM - Three parameters get sent to :TESTCC with Call. <errorlevel> <codeposition> <positioncolor>
REM - The first paramater has a value that gets tested, the other 2 parameters are Variables to be Defined
REM c- in :TESTCC using the result of If checks on Parameter 1.
CHOICE /N /C:abcdef0123456789 /M "Select Foreground Color: A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! fgcode fgcolor
REM - Test that matching color codes have not been selected
IF "!bgcode!"=="!fgcode!" (
ECHO Colors Cannot Match.
Timeout 2 > nul
GOTO ColorSelect
)
cls
- When you echo something to the console standard output - stdout, you choose what to say. As long as any variables you expand are defined, you can display their information
- It's really up to you to imagine what information you wish to
display, and figuring out what input or variables you need to get or set to achieve the desired result.
REM - Display in standard Colors, for those times Colors are hard to distinguish
REM - Calls the Function to Print the message in the Selected colors.
ECHO Color !bgcode!!fgcode! Background Code [!bgcode!] Color [!bgcolor!] Foreground Code [!fgcode!] Color [!fgcolor!]
CALL :colorize "!bgcode!!fgcode!" "Color !bgcode!!fgcode! Background Code [!bgcode!] Color [!bgcolor!] Foreground Code [!fgcode!] Color [!fgcolor!]"
ECHO.
pause
GOTO ColorSelect
REM - Uses Parameters Sent by Call to Define Variables for Use and Display
:TESTCC
IF %~1==1 Set "%~2=a" && Set "%~3=Light Green"
IF %~1==2 Set "%~2=b" && Set "%~3=Light Aqua"
IF %~1==3 Set "%~2=c" && Set "%~3=Light Red"
IF %~1==4 Set "%~2=d" && Set "%~3=Light Purple"
IF %~1==5 Set "%~2=e" && Set "%~3=Light Yellow"
IF %~1==6 Set "%~2=f" && Set "%~3=Light White"
IF %~1==7 Set "%~2=0" && Set "%~3=Black"
IF %~1==8 Set "%~2=1" && Set "%~3=Blue"
IF %~1==9 Set "%~2=2" && Set "%~3=Green"
IF %~1==10 Set "%~2=3" && Set "%~3=Aqua"
IF %~1==11 Set "%~2=4" && Set "%~3=Red"
IF %~1==12 Set "%~2=5" && Set "%~3=Purple"
IF %~1==13 Set "%~2=6" && Set "%~3=Yellow"
IF %~1==14 Set "%~2=7" && Set "%~3=White"
IF %~1==15 Set "%~2=8" && Set "%~3=Grey"
IF %~1==16 Set "%~2=9" && Set "%~3=light Blue"
exit /b 0
REM - Colorize function. To use:
REM - CALL :Colorize colorcodes "String in quotes"
REM - Function works by creating a temporary file named using the string and Printing it Colored with FINDSTR
REM - Note: Cannot use strings with characters reserved against use in filenames. Strips trailing Spaces.
Color Function Used from Jebs answer here. Other, Improved methods can be found there.
:Colorize
IF NOT EXIST "%TEMP%\colorize" md "%TEMP%\colorize"
PUSHD %TEMP%\colorize
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
POPD
goto :eof
There's many, many different ways of displaying information, It's just a matter of choosing the means.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论