CMD: Set buffer height independently of window height

前端 未结 6 499
情话喂你
情话喂你 2020-12-05 06:17

I\'ve recently learned that I can control the size of the CMD window running my program with mode x,y. However I just as recently noticed that this sets the

6条回答
  •  不知归路
    2020-12-05 06:35

    The answer to this question didn't work as I got an error regarding variables sent to the PowerShell command, so I modified to work.

    The PowerShell command simply adds uiHeightBuffer to the uiHeight variable to show the scroll bar.


    Edit: Added the height buffer as the 3rd parameter and TRUE or FALSE as a 4th parameter to enable or disable the scrollbar.

    I found this useful when exiting a script with a small message when there's no need for the scrollbar in this case.

    Also to enable UTF-8 encoding you have to set the default at the start of the `:CMDSIZE' call and set it at the end, otherwise, the font size goes a bit funny.

    Omitting chcp 850 >NUL & and chcp 65001 >NUL & are what you'd do if you didn't want to affect this.

    This works in Version: 1909 of Windows 10.

    @echo off
    title %~nx0
    rem Based on this thread here: https://stackoverflow.com/a/13351373
    goto MYROUTINE
    :CMDSIZE
    chcp 850 >NUL & set "uiWidth=%1" & set "uiHeight=%2"
    mode %uiWidth%,%uiHeight%
    if %4==TRUE (set /a "uiHeightBuffer=uiHeight+%3")
    if %4==TRUE (powershell.exe -ExecutionPolicy Bypass -Command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeightBuffer%;$W.buffersize=$B}")
    if %4==FALSE (powershell.exe -ExecutionPolicy Bypass -Command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeight%;$W.buffersize=$B}")
    chcp 65001 >NUL & goto :EOF
    :MYROUTINE
    call :CMDSIZE 255 44 222 TRUE
    title Do your routine here...
    echo Do your routine here...
    echo/ & pause
    goto :EXITROUTINE
    :EXITROUTINE
    call :CMDSIZE 40 2 0 FALSE
    title Exiting routine message...
    echo Exiting routine message...
    set /p "=" NUL & exit
    

提交回复
热议问题