Advertisement
T3RRYT3RR0R

Batch Console Repositioning and Text Alignment

Apr 8th, 2020
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.11 KB | None | 0 0
  1. ::: Batch script to reposition console, includes macro to output text Aligned Right, Centre or Left.
  2.  
  3. ::: Updated to facilitate multiple resize / repositions from within the same script with ease, Using a tempory Batch file a a 'middle man'
  4. ::: The tempory batch is started with the new console properties, and then Calls the original Batch with parameters for
  5. ::: Console width and :Label to resume from
  6.  
  7. ::: Script fetches the current screen resolution and calculates the approximate X / Y coordinates needed to position
  8. ::: the console window in the centre of the screen.
  9.  
  10. ::: Example output in my answer here: https://stackoverflow.com/questions/24228294/batch-centre-resize-and-reposition/61046030#61046030
  11.  
  12. ::: Script updated to allow positioning of console at screen top left with any 3rd parameter
  13.  
  14. ::: Note : If called or started from cmd.exe or another batch, this script will end the parent Process.
  15.  
  16. @Echo Off & CD "%~dp0"
  17.    
  18.     Set "AlignFile=%~dpnx0"
  19.  
  20.     Setlocal DisableDelayedExpansion
  21.  
  22.     (Set LF=^
  23.  
  24.  
  25.     %= NewLine =%)
  26.  
  27.     Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  28.  
  29. %= Define console width and values for text alignment =%
  30.  
  31.     Set @Align_Centre=Set /A "%%H=(Console_Width / 2) - (Len / 2)"
  32.     Set @Align_Right=Set /A "%%H=(Console_Width - Len)"
  33.     Set @Align_Left=Set /A "%%H=0"
  34.  
  35. %= @Align Macro calculates string length then uses 2nd Parameter to Act on Alignment Calculation =%
  36. %= Macro appends spaces to the string depending on Alignment value / mode chosen to position, then output string to console. =%
  37.  
  38.     Set @Align=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  39.         For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
  40.             If not "!%%~G!"=="" (Set "TextOut=!%%~G!") Else (Set "TextOut=%%~G")%\n%
  41.             Set LenTrim=Start%\n%
  42.             For /L %%a in (1,1,!Console_Width!) Do (%\n%
  43.                 IF NOT "!LenTrim!"=="" (%\n%
  44.                     Set LenTrim=!TextOut:~0,-%%a!%\n%
  45.                     If "!LenTrim!"=="" Set "Len=%%a"%\n%
  46.                 ) %\n%
  47.             ) %\n%
  48.             IF /I "%%H"=="C" %@Align_Centre% %\n%
  49.             IF /I "%%H"=="R" %@Align_Right% %\n%
  50.             IF /I "%%H"=="L" %@Align_Left% %\n%
  51.             For /L %%# in (1,1,!%%H!) Do Set "TextOut= !TextOut!" %\n%
  52.             Echo(!TextOut!^&^& Endlocal %\n%
  53.         ) %\n%
  54.     ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  55.  
  56.     If Not "%~2"=="" (Set "Console_Width=%~2" & Goto :%~1) Else (Goto :main)
  57.  
  58. %= Subroutine to process output of wmic command into usable variables  for screen dimensions (resolution) =%
  59.  
  60.     :ChangeConsole <Lines> <Columns> <Label to Resume From> <If a 4th parameter is Defined, Aligns screen at top left>
  61. %= Get screen Dimensions =%
  62.     For /f "delims=" %%# in  ('"wmic path Win32_VideoController  get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
  63.         Set "%%#">nul
  64.     )
  65.  
  66. %= Calculation of X axis relative to screen resolution and console size =%
  67.  
  68.     Set /A CentreX= ( CurrentHorizontalResolution / 2 ) - ( %~2 * 4 )
  69.  
  70. %= Sub Optimal calculation of Y axis relative to screen resolution and console size =%
  71.     For /L %%A in (10,10,%1) DO Set /A VertMod+=1
  72.     Set /A CentreY= ( CurrentVerticalResolution / 4 ) - ( %~1 * Vertmod )
  73.  
  74. %= Optional 4th parameter can be used to align console at top left of screen instead of screen centre =%
  75.     If Not "%~4"=="" (Set /A CentreY=0,CentreX=-8)
  76.  
  77. %= .Vbs script creation and launch to reopen batch with new console settings, combines with =%
  78.     Set "Console_Width=%~2"
  79.  
  80. %= 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 =%
  81.         (
  82.         Echo.@Mode Con: lines=%~1 cols=%~2
  83.         Echo.@Title Res: %CurrentHorizontalResolution%x%CurrentVerticalResolution% X,Y Pos: %CentreX%,%CentreY% Con Size: Cols = %~2 Lines = %~1
  84.         Echo.@Call "%AlignFile%" "%~3" "%~2"
  85.         )>"%temp%\ChangeConsole.bat"
  86.  
  87.         (
  88.         Echo.Set objWMIService = GetObject^("winmgmts:\\.\root\cimv2"^)
  89.         Echo.Set objConfig = objWMIService.Get^("Win32_ProcessStartup"^)
  90.         Echo.objConfig.SpawnInstance_
  91.         Echo.objConfig.X = %CentreX%
  92.         Echo.objConfig.Y = %CentreY%
  93.         Echo.Set objNewProcess = objWMIService.Get^("Win32_Process"^)
  94.         Echo.intReturn = objNewProcess.Create^("%temp%\ChangeConsole.bat", Null, objConfig, intProcessID^)
  95.         )>"%temp%\Consolepos.vbs"
  96.  
  97. %= Starts the companion batch script to Change Console properties, ends the parent =%
  98.     Start "" "%temp%\Consolepos.vbs" & Exit
  99.  
  100. :main
  101.  
  102. %= If a 4rd parameter is used, Console will be positioned at top left of screen =%
  103.  
  104.     Call :ChangeConsole 45 50 Display_Text_1 top
  105.    
  106. :Display_Text_1
  107.    
  108.     For %%B in ("Show this" "in centre") do Set "Text=%%~B" & %@Align% Text C
  109.     For %%B in ("Show this" "on right") do Set "Text=%%~B" & %@Align% Text R
  110.     For %%B in ("Show this" "on left") do Set "Text=%%~B" & %@Align% Text L
  111.  
  112.     Echo.Example 1 Done. & Pause >nul
  113.  
  114.     Call :ChangeConsole 25 150 Display_Text_2
  115.    
  116. :Display_Text_2
  117.  
  118.     Set "string=<< %%A Left String%% \" & %@Align% string L
  119.     Set "string=|^ A Centred String ^|" & %@Align% string C
  120.     Set "string=/ A !Right String!>>" & %@Align% string R
  121.  
  122.     Echo.Example 2 Done. & Pause >nul
  123.  
  124.     (taskkill /pid WScript.exe /f /t) >nul 2>nul
  125.     Timeout 1 >nul
  126.     Del /F "%temp%\Consolepos.vbs" >nul 2>nul
  127.     Del /F "%temp%\ChangeConsole.bat" >nul 2>nul
  128.     exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement