JohnnySemicolon
JohnnySemicolon

Reputation: 265

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?

Upvotes: 6

Views: 11291

Answers (3)

rojo
rojo

Reputation: 24466

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

Upvotes: 4

Konrad
Konrad

Reputation: 371

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 

Upvotes: 2

anishsane
anishsane

Reputation: 20970

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

Upvotes: 2

Related Questions