0

I am currently trying to open two windows through a batch file and then resize and move them (to be splitscreen). Opening the separate windows is easy:

@echo off

cd "C:\Program Files (x86)\Internet Explorer"
start iexplore.exe 

cd "C:\Program Files (x86)\Mozilla Firefox"
start firefox.exe 

exit

but I can't find a way to resize and move the windows I am opening. I would prefer not having to use any third party programs. I tried looking through the start /? help menu and don't believe any of the options are of use to me.

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>start /?
Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.
    path        Starting directory.
    B           Start application without creating a new window. The
                application has ^C handling ignored. Unless the application
                enables ^C processing, ^Break is the only way to interrupt
                the application.
    I           The new environment will be the original environment passed
                to the cmd.exe and not the current environment.
    MIN         Start window minimized.
    MAX         Start window maximized.
    SEPARATE    Start 16-bit Windows program in separate memory space.
    SHARED      Start 16-bit Windows program in shared memory space.
    LOW         Start application in the IDLE priority class.
    NORMAL      Start application in the NORMAL priority class.
    HIGH        Start application in the HIGH priority class.
    REALTIME    Start application in the REALTIME priority class.
Press any key to continue . . .
Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
  • 6
    http://stackoverflow.com/questions/10392620/how-can-a-batch-file-run-a-program-and-set-the-position-and-size-of-the-window Might be what you're looking for – Zak Jan 04 '16 at 18:11
  • @Zak , the accepted answer specifically says "Unfortunately, this doesn't give you full control of your exact window size/positioning, but it should remember last size/positioning." and the next 3 answers all use 3rd party programs and my question specified not using 3rd party programs. – Jordan.J.D Jan 04 '16 at 18:55
  • 1
    This was not asked the first time. Zak lead already to [How can a batch file run a program and set the position and size of the window?](http://stackoverflow.com/questions/10392620/) Another one is [Specify the size of command prompt when executing a batch file](http://stackoverflow.com/questions/27120267/). One more method would make use of information provided in answer on [what's the difference between command prompt and cmd?](http://stackoverflow.com/questions/34068830/) - batch adds registry entry for a specific console window and opens such a console window using `start`. – Mofi Jan 04 '16 at 19:15
  • 1
    But it is impossible to run a batch file which resizes or repositions any process window using only standard commands of Windows and not PowerShell or third-party tools as there is simply no standard Windows command line application which can resize and/or reposition a window. – Mofi Jan 04 '16 at 19:19
  • 2
    This is one of only two times I will recommend using PowerShell on a question with the [batch-file] tag. – SomethingDark Jan 04 '16 at 19:30
  • 1
    Binary registry value `Window_Placement` of registry key `HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main` defines IE window status, size and position and could be set before starting IE (and restored to previous data after starting). Firefox stores window data in `%APPDATA%\Profiles\*\sessionstore.js` (profile folder name is in `profiles.ini`) which could be manipulated before starting Firefox (and restored after starting). This would work only if both browsers are always started with new application windows instead of using already running instance. – Mofi Jan 04 '16 at 19:39
  • 1
    http://stackoverflow.com/a/16126938/1683264 ... might be what you're looking for. – rojo Jan 04 '16 at 20:59
  • Have you evfer heard of [tag:autohotkey]? I know it is a third-party tool, but it is perfect for window manipulations... – aschipfl Jan 04 '16 at 21:01

1 Answers1

3

If you have PowerShell installed (and you likely do), you can use user32.dll to move and adjust windows. Once upon a time, I needed a script for work that does exactly what you need and I found this and adjusted it to suit my needs. I then used this to convert it to a batch/powershell hybrid so that I only had to double-click the file to get the PowerShell script to run.

<# :
:: Based on https://gist.github.com/coldnebo/1148334
:: Converted to a batch/powershell hybrid via http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
@echo off
setlocal
cls
set "POWERSHELL_BAT_ARGS=%*"
if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF
#>

# Add the relevant section of the Win32 API to the PowerShell session 
# Allows windows to be moved and resized
Add-Type @"
    using System;
    using System.Runtime.InteropServices;

    public class Win32 { 
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    }
"@

################################################################################
# Moves and resizes the window based the broswer
#
# Arguments: $browser - the browser being moved and resized
# Returns:   None
################################################################################
Function MoveAndResize ($browser)
{
    # $browser_path is the full path to the browser
    # $screen_x is the horizontal location of the window on the screen
    # $screen_y is the vertical location of the window on the screen
    # $win_x is the width of the target window
    # $win_y is the height of the target window
    Switch($browser){
        InternetExplorer{
            $browser_path="C:\Program Files\Internet Explorer\IEXPLORE.EXE"
            $screen_x = 0
            $screen_y = 0
            $win_x = 960
            $win_y = 1080
            break
        }
        Firefox{
            $browser_path="C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
            $screen_x = 960
            $screen_y = 0
            $win_x = 960
            $win_y = 1080
            break
        }
        default {continue}
    }

    # Start the desired browser
    Start-Process $browser_path

    # Wait one second until the browser is fully loaded
    Start-Sleep -S 1

    # Find the running process where the application path matches $browser_path
    $browser = (Get-Process | where {$_.Path -eq $browser_path}).MainWindowHandle

    [Win32]::MoveWindow($browser, $screen_x, $screen_y, $win_x, $win_y, $true)
}

MoveAndResize "InternetExplorer"
MoveAndResize "Firefox"

Note that I'm using the Program Files version of iexplore instead of the Program Files (x86) version because that one wouldn't move or resize for some reason.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55