Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: Batch console property mastery for windows 10
- ::: Resize & Position the Console at (top left) or centre
- ::: *!UPDATE!* 18th april 2020 Console centering algorithm adjusted to correctly centre for all resolutions.
- ::: Position text at Left / Centre / Right of console
- ::: Color text
- @Echo Off & CD "%~dp0"
- Set "AlignFile=%~dpnx0"
- Setlocal DisableDelayedExpansion
- (Set LF=^
- %= NewLine =%)
- Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
- %= Define console width and values for text alignment =%
- Set @Align_Centre=Set /A "%%H=(Console_Width / 2) - (Len / 2)"
- Set @Align_Right=Set /A "%%H=(Console_Width - Len)"
- Set @Align_Left=Set /A "%%H=0"
- %= @Align Macro calculates string length then uses 2nd Parameter to Act on Alignment Calculation =%
- %= Macro appends spaces to the string depending on Alignment value / mode chosen to position, then output string to console. =%
- Set @Align=for /L %%n in (1 1 2) do if %%n==2 (%\n%
- For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
- If not "!%%~G!"=="" (Set "TextOut=!%%~G!") Else (Set "TextOut=%%~G")%\n%
- Set LenTrim=Start%\n%
- For /L %%a in (1,1,!Console_Width!) Do (%\n%
- IF NOT "!LenTrim!"=="" (%\n%
- Set LenTrim=!TextOut:~0,-%%a!%\n%
- If "!LenTrim!"=="" Set "Len=%%a"%\n%
- ) %\n%
- ) %\n%
- IF /I "%%H"=="C" %@Align_Centre% %\n%
- IF /I "%%H"=="R" %@Align_Right% %\n%
- IF /I "%%H"=="L" %@Align_Left% %\n%
- For /L %%# in (1,1,!%%H!) Do Set "TextOut= !TextOut!" %\n%
- Echo(!Color!!TextOut!!white!^&^& Endlocal %\n%
- ) %\n%
- ) ELSE setlocal enableDelayedExpansion ^& set argv=,
- REM Color Macro Variables
- Set "@Color=Call :Color "
- Setlocal EnableDelayedExpansion
- Set /A Red=31,Green=32,Yellow=33,dark.blue=34,Purple=35,light.Blue=36,White=0,Grey=90,Pink=91,Beige=93,Aqua=94,Magenta=95,Teal=96
- For %%A in (Red,Green,Yellow,dark.blue,Purple,light.Blue,White,Grey,Pink,Beige,Aqua,Magenta,Teal) do Call Set "%%A=[!%%A!m"
- Setlocal DisableDelayedExpansion
- Set "@Hold=Call :ColorLetters "Next." & Echo. & Pause>nul"
- If Not "%~3"=="" (
- Set "AlignFile=%~3"
- Set "Console_Width=%~2"
- Goto :%~1
- ) Else (Goto :main)
- %= Subroutine to process output of wmic command into usable variables for screen dimensions (resolution) =%
- :ChangeConsole <Lines> <Columns> <Label to Resume From> <If a 4th parameter is Defined, Aligns screen at top left>
- Setlocal EnableDelayedExpansion
- %= Get screen Dimensions =%
- For /f "delims=" %%# in ('"wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
- Set "%%#">nul
- )
- %= Calculation of X axis relative to screen resolution and console size. Resolution scales to Max Columns - 170 =%
- Set /A XresScale=CurrentHorizontalResolution / 170
- Set /A HorzCentre=CurrentHorizontalResolution / 2
- Set /A CentreX= HorzCentre - ( ( %~2 * XresScale ) / 2 )
- %= Calculation of Y axis relative to screen resolution and console size. Resolution scales to Max Lines - 40 =%
- Set /A YresScale= CurrentVerticalResolution / 40
- Set /A VertCentre=CurrentVerticalResolution / 2
- Set /A CentreY= VertCentre - ( ( %~1 * YresScale ) / 2 )
- %= Optional 4th parameter can be used to align console at top left of screen instead of screen centre =%
- If Not "%~4"=="" (Set /A CentreY=0,CentreX=-8)
- %= .Vbs script creation and launch to reopen batch with new console settings, combines with =%
- Set "Console_Width=%~2"
- %= Creates a batch file to reopen the main script using Call with parameters to define properties for console change and the label to resume from =%
- (
- Echo.@Mode Con: lines=%~1 cols=%~2
- Echo.@Title Res: %CurrentHorizontalResolution%x%CurrentVerticalResolution% X,Y Pos: %CentreX%,%CentreY% Con Size: Cols = %~2 Lines = %~1
- Echo.@Call "%AlignFile%" "%~3" "%~2" "%AlignFile%"
- )>"%temp%\ChangeConsole.bat"
- (
- Echo.Set objWMIService = GetObject^("winmgmts:\\.\root\cimv2"^)
- Echo.Set objConfig = objWMIService.Get^("Win32_ProcessStartup"^)
- Echo.objConfig.SpawnInstance_
- Echo.objConfig.X = %CentreX%
- Echo.objConfig.Y = %CentreY%
- Echo.Set objNewProcess = objWMIService.Get^("Win32_Process"^)
- Echo.intReturn = objNewProcess.Create^("%temp%\ChangeConsole.bat", Null, objConfig, intProcessID^)
- )>"%temp%\Consolepos.vbs"
- %= Starts the companion batch script to Change Console properties, ends the parent =%
- Start "" "%temp%\Consolepos.vbs" & Exit
- :Color
- Setlocal EnableDelayedExpansion
- Set "Color=!%~1!"
- ( Endlocal & Set "Color=%Color%" )
- Exit /B
- :Colorwords
- Setlocal EnableDelayedExpansion
- Set #A=31
- For %%A in (%*) do (
- Set "Word=%%~A"
- Call :ColorPrint "!Word!"
- <nul set /p=[30m.[0m
- )
- Endlocal
- Exit /B
- :ColorLetters
- Setlocal EnableDelayedExpansion
- Set #A=31
- For %%A in (%*) do (
- Set "Word=%%~A"
- For %%B In (a b c d e f g h i j k l m n o p q r s t u v w x y z . [ ] ) do Set "Word=!Word:%%~B=%%~B !
- Call :ColorPrint "!Word!"
- <nul set /p=[30m.[0m
- )
- Endlocal
- Exit /B
- :ColorPrint
- For %%C in (%~1) do (
- <nul set /p=[!#A!m%%~C
- Set /A #A+=1
- IF "!#A!"=="37" (Set #A=31)
- )
- Exit /B
- :main
- %= Remainder of Script examples the usage of Subroutines and macro's =%
- %= If a 4rd parameter is used, Console will be positioned at top left of screen =%
- Call :ChangeConsole 50 50 Display_Text_1 top
- :Display_Text_1
- %@Color% red & For %%B in ("Show this" "in centre") do Set "Text=%%~B" & %@Align% Text C
- %@hold%
- %@Color% green & For %%B in ("Show this" "on right") do Set "Text=%%~B" & %@Align% Text R
- %@hold%
- %@Color% light.blue & For %%B in ("Show this" "on left") do Set "Text=%%~B" & %@Align% Text L
- %@hold%
- Call :ChangeConsole 40 150 Display_Text_2
- :Display_Text_2
- %@Color% pink & Set "string=<< %%A Left String%% \" & %@Align% string L
- %@hold%
- Call :ChangeConsole 30 175 Display_Text_3
- :Display_Text_3
- %@Color% teal & Set "string=|^ A Centred String ^|" & %@Align% string C
- %@hold%
- Call :ChangeConsole 20 30 Display_Text_4
- :Display_Text_4
- %@Color% magenta & Set "string=/ A !Right String!>>" & %@Align% string R
- %@hold%
- (taskkill /pid WScript.exe /f /t) >nul 2>nul
- Timeout 1 >nul
- Del /F "%temp%\Consolepos.vbs" >nul 2>nul
- Del /F "%temp%\ChangeConsole.bat" >nul 2>nul
- exit /b
Advertisement
Add Comment
Please, Sign In to add comment