Board logo

标题: [特效代码] 求助 批处理字符显示显示在CMD窗口中间 [打印本页]

作者: enpany    时间: 2020-8-14 10:51     标题: 求助 批处理字符显示显示在CMD窗口中间

如何在不改变CMD窗口的尺寸下,让输出字符显示在窗口中间?
作者: zaqmlp    时间: 2020-8-14 12:48

用制表符或空格控制缩进
作者: a20150604    时间: 2020-8-14 12:51

WIN 10 有效
  1. @echo off
  2. for /F %%a in ('echo prompt $E^| cmd') do set "_ESC=%%a"
  3. for /f "tokens=3,4 delims=, " %%A in (
  4.   'powershell -command "&{$H=get-host;$H.ui.rawui;}"^|findstr /b WindowSize'
  5. ) do set /a "_win_wid=%%A, _win_hei=%%B"
  6. set "str=hello world."
  7. set /a "y=_win_hei/2,x=_win_wid/2-6"
  8. set "_RGB_FACE=255;0;0"
  9. <nul set /p "=%_ESC%[38;2;%_RGB_FACE%m"
  10. <nul set /p "=%_ESC%[%y%;%x%H%str%
  11. set "str=VT100 on Windows 10"
  12. set /a "y=_win_hei/2+1,x=_win_wid/2-9"
  13. set "_RGB_FACE=0;255;0"
  14. <nul set /p "=%_ESC%[38;2;%_RGB_FACE%m"
  15. <nul set /p "=%_ESC%[%y%;%x%H%str%
  16. pause>nul
复制代码

作者: enpany    时间: 2020-8-14 14:03

回复 3# a20150604


    WIN7下有没有效呢?
作者: a20150604    时间: 2020-8-14 18:05

回复 4# enpany

仅 Win 10 有效
来个简单特效
  1. @echo off & setlocal enabledelayedexpansion
  2. REM VT100 参考
  3. REM https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
  4. for /F %%a in ('echo prompt $E^| cmd') do set "_ESC=%%a"
  5. REM 窗口尺寸也可以不指定
  6. mode 100,40
  7. REM 移动周期: 正整数, 越小, 移动越快
  8. set /a "cycle=2000"
  9. set "rgb_red=%_ESC%[38;2;255;0;0m"
  10. set "rgb_grn=%_ESC%[38;2;0;255;0m"
  11. set "rgb_blu=%_ESC%[38;2;0;0;255m"
  12. REM set       "str=bbs.bathome.net"
  13. set       "str=%rgb_red%bbs.%rgb_grn%bathome%rgb_blu%.net"
  14. set "str_clear=%rgb_red%bbs.%rgb_grn%       %rgb_blu%.net"
  15. for /f "tokens=3,4 delims=, " %%A in (
  16.     'powershell -command "&{$H=get-host;$H.ui.rawui;}"^|findstr /b WindowSize'
  17. ) do set /a "_win_wid=%%A, _win_hei=%%B, x_ubound=_win_wid-14" & REM 14 为 str 的长度 - 1
  18. set /a "y= !random! %% _win_hei + 1, x= !random! %% x_ubound + 1"
  19. set /a "dx=dy=1"
  20. REM 隐藏光标 Hide the cursor
  21. <nul set /p "=%_ESC%[?25l"
  22. for /l %%a in (-9999999 1 9999999) do (
  23.     set /a "m=%%a %% cycle"
  24.     if !m!==0 (
  25.         <nul set /p "=%_ESC%[!y!;!x!H%str_clear%"
  26.         if !y! geq !_win_hei! set /a "dy=-1"
  27.         if !y! leq 1 set /a "dy=1"
  28.         if !x! geq !x_ubound! set /a "dx=-1"
  29.         if !x! leq 1 set /a "dx=1"
  30.         set /a "y+=dy,x+=dx"
  31.         <nul set /p "=%_ESC%[!y!;!x!H%str%"
  32.     )
  33. )
复制代码

作者: enpany    时间: 2020-8-14 18:40

回复 5# a20150604


    膜拜大佬




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2