批量加载屏幕,没有毛刺/滞后

时间:2015-10-27 23:14:33

标签: batch-file cmd

以下是我的批量加载脚本

Echo                                Loading
Echo                                ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo                                ºÛ               º
Echo                                ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ping localhost -n 2 > nul
cls
Echo                                Loading
Echo                                ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo                                ºÛÛ              º
Echo                                ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ping localhost -n 2 > nul
cls
Echo                                Loading
Echo                                ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo                                ºÛÛÛ             º
Echo                                ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
exit

使用cls代码擦除屏幕会使屏幕看起来像"闪烁"。有没有更好的方法来清除此批处理文件的屏幕? TIA

1 个答案:

答案 0 :(得分:3)

远离完美的小故障选项:不要重新绘制整个屏幕,而是重新绘制加载线(但这必须是屏幕中的最后一条画线)。

基本思路是输出以回车结尾但没有换行的行,这样光标就会移动到行的开头,再次在同一行上写入。

@echo off
    setlocal enableextensions enabledelayedexpansion

    Rem Get a carriage return character
    set "CR=" & for /f %%a in ('copy /Z "%~f0" nul') do if not defined CR set "CR=%%a"
    rem The progress bar
    set "fill=[##########]"

    cls
    echo(:: computers were created to give humans time to think while waiting ....

    rem For each character in the fill
    for /l %%a in (2 1 11) do (
        rem Calculate the right part of the bar
        set "spaces=!fill:~%%a!"

        rem Output the left and right parts of the bar and carriage return
        <nul set/p ".=:: Loading something big !fill:~0,%%a!!spaces:#= !!CR!"

        rem Pause for a second
        ping -n 2 "" > nul
    )
    echo(
    echo(:: Done

如果你不能改变你的代码/设计,那么最后一行是唯一重新绘制的,至少尝试将绘制操作封装在一个块中(括在括号中的代码),所以在每个绘制操作命令中只解析一次,并在开始画之前准备好一切。它不会避免故障,但不太明显

@echo off
    setlocal enableextensions enabledelayedexpansion

    for /l %%a in (0 10 100) do (
        call :loadingScreen %%a
        >nul ping -n 2 "" 
    )
    echo(:: Done
    goto :eof

:loadingScreen percent
    setlocal enableextensions enabledelayedexpansion

    rem Prepare everything 
    set    "sb=+----------+"
    set "fill=^|##########^|"
    set    "eb=+----------+"
    set /a "chars=2+%~1/10"
    set "spaces=!fill:~%chars%!"
    set "loadBar=!fill:~0,%chars%!!spaces:#= !"

    rem Time to paint
    (   cls
        echo(:: computers were created to give humans time to think while waiting .... %time%
        echo(
        echo(                           %sb%
        echo(     Loading something big %loadBar%
        echo(                           %eb%
        echo(
    ) 
    goto :eof