将2个代码组合为颜色和中心文本。可能吗?

时间:2019-07-23 09:29:07

标签: batch-file cmd

我正在尝试编写简单的代码,但使其看起来很漂亮:D 我想将这些合并为代码,但我不知道如何

call :ColorText 0a "Green"
set "Center=Code Options" & call :CenterText Center strLen

每个工作分别很好,但我想结合两者使文本居中和着色

编辑

我要使文本“ Code By ZeE”为绿色 这是代码

@echo off
setlocal enabledelayedexpansion
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
title New Title
:: color 2
mode 120,30
set "cmdwidth=120"  
cls             
echo.                               
:Display
echo.
echo.
echo.
echo.
echo.
echo.                               
echo.               
set "Center=-----------------------------" & call :CenterText Center strLen
set "Center=Code By ZeE" & call :CenterText Center strLen
set "Center=-----------------------------" & call :CenterText Center strLen
Call :ColorText 0a "lol"
echo.
pause               


:CenterText
  if not "!%1:~%len%!"=="" set /A len+=1 & goto :CenterText
(endlocal & set %2=%len%)
goto CenterTextDisplay
:AddSpace
set "spaces=%spaces% "
goto :eof
:CenterTextDisplay
set /a "indent=(cmdwidth - strLen)/2" 
set "spaces= "
for /l %%a in (1,1,%indent%) do call :AddSpace
echo %spaces%%Center%
set "len=0"
goto :eof
:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

1 个答案:

答案 0 :(得分:0)

:CenterText过程中,更改

  echo %spaces%%Center%

if "%~3" == "" (
  echo %spaces%%Center%
) else (
  Call :ColorText %~3 "%spaces%%Center%"&echo(
)

然后,在:CenterText过程调用中添加第三个参数(颜色代码)可能会有所帮助:

set "Center=Code By ZeE" & call :CenterText Center strLen 0a
rem                                                       ^^