Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to maximize current window

I built a batch program that I am currently tweaking to make it more readable/user friendly.

I would like my .bat file to automatically be set to maximize in the .bat file itself.

I read about START /MAX online, but that just opens a new instance of a command prompt window. I don't want to have two .bat files just to maximize one.

I know the windows keys to maximize is ALT+SPACE then X. I got the idea maybe I could use some kind of SendKeys in batch scripting to automate that? I didn't have any luck finding information online.

Is there anyway to program this to maximize within the same .bat instance?

like image 409
JohnnySemicolon Avatar asked Jun 15 '16 07:06

JohnnySemicolon


People also ask

How do you make a batch file that opens a program maximized?

If we didn't start the batch with a max parameter we need to execute this line. start /MAX start the command after it, in maximized form. cmd /c execute cmd.exe and /c means exit afterwards. %0 max .

How do I maximize a command prompt window?

Inside the command prompt, press both ALT + Enter keys. You can see that this now makes the command prompt enter into full screen mode, as the following image shows. 3. You can press ALT + Enter again to switch back to the smaller, resizable window mode.

How do I resize a batch window?

If you just open the batch file, click on the window, and then click "properties", and then to "layout", and scroll down to "Window Size", you can edit it from there. It will also stay that way every time you open that specific batch file, so it's pretty handy.


Video Answer


3 Answers

The console window is not measured by w*h in pixels, but in rows and columns. In part, the physical dimensions will be dependent upon the font face and size defined by the user.

The simplest solution is to increase the number of rows and / or columns using the mode command. You can also increase the scroll buffer using a PowerShell helper. As follows is a batch function I've used a few times to manipulate all these values.

:consize <columns> <lines> <scrolllines>
:: change console window dimensions and buffer
mode con: cols=%1 lines=%2
powershell -noprofile "$W=(get-host).ui.rawui; $B=$W.buffersize; $B.height=%3; $W.buffersize=$B"
goto :EOF

The :consize function goes at the bottom of your script, after the final exit /b or goto :EOF at the end of your main script runtime. See this page for more examples of batch functions.

Example usage:

call :consize 80 33 10000

... would expand the window to 80 columns and 33 lines, then expand the vertical scroll buffer to 10,000 lines.


Here's a more complete Batch + PowerShell hybrid script that will move the window to 0,0 then change its width and height to the max columns and rows the screen will accommodate. I had to trial-and-error the $max.Height and $max.Width values a little, so I'm unsure how different display resolutions and different font sizes will affect the script. It ought to be close enough for government work, though.

<# : batch portion
@echo off & setlocal

call :maximize

rem /* ###############################
rem    Your main batch code goes here.
rem    ############################### */

goto :EOF

:maximize
set "scrollLines=10000"
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF

rem // end batch / begin PowerShell hybrid code #>

# Moving the window to coordinates 0,0 requires importing a function from user32.dll.
add-type user32_dll @'
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
        int x, int y, int cx, int cy, uint uFlags);
'@ -namespace System

# Walk up the process tree until we find a window handle
$id = $PID
do {
    $id = (gwmi win32_process -filter "ProcessID='$id'").ParentProcessID
    $hwnd = (ps -id $id).MainWindowHandle
} while (-not $hwnd)

# This is where the window moves!
[void][user32_dll]::SetWindowPos($hwnd, [IntPtr]::Zero, 0, 0, 0, 0, 0x41)

# Maximize the window
$console = (get-host).ui.rawui
$max = $console.MaxPhysicalWindowSize
$max.Height -= 1  # account for the titlebar
$max.Width -= 5  # account for the scrollbar
$buffer = $max
$buffer.Height = $env:scrollLines
$console.BufferSize = $buffer
$console.WindowSize = $max
like image 133
rojo Avatar answered Sep 28 '22 17:09

rojo


I was thinking about this method:

@echo off
set mytitle=%random%
title %mytitle%
echo WScript.sleep 1000 > %temp%\max-me.vbs
echo WScript.CreateObject("WScript.Shell").AppActivate "%mytitle%" >> %temp%\max-me.vbs
echo WScript.CreateObject("WScript.Shell").SendKeys "%% n" >> %temp%\max-me.vbs
cscript %temp%\max-me.vbs >nul
echo further code goes here...
pause

However, for some reason, sendkeys does not seem to work with cmd windows. Below mechanism works, but then you lose the existing window & its exit status.

@echo off
if not defined iammaximized (
    set iammaximized=1
    start /max "" "%0" "%*"
    exit
)
echo further code goes here...
pause
like image 25
anishsane Avatar answered Sep 28 '22 18:09

anishsane


There is a little tool called "cmdow.exe" (may be downloaded ad sourceforge.net). But be carefull, this tool allows you to do a lot of things which you might not want to do.

In order to maximize the currend cmd window:

cmdow @ /max

In order to restore the current cmd window again:

cmdow @ /res 
like image 35
Konrad Avatar answered Sep 28 '22 17:09

Konrad