2016-01-04 42 views
0

我目前正試圖通過批處理文件打開兩個窗口,然後調整大小並移動它們(成爲分屏)。打開單獨的窗口很容易:批處理文件打開窗口並調整它們的大小

@echo off 

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

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

exit 

但我找不到調整方式並移動我正在打開的窗口。我寧願不必使用任何第三方程序。我試着翻閱start /?幫助菜單,並不相信任何選項對我都有用。

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 . . . 
+5

http://stackoverflow.com/questions/10392620/how-can-a-batch-file-run-a-program-and-set-the-position-and-size-of-the-window Might be你在尋找什麼 – Zak

+0

@Zak,接受的答案特別說:「不幸的是,這並不能完全控制你的確切窗口尺寸/位置,但它應該記住最後的尺寸/位置。」和接下來的3個答案都使用第三方程序,我的問題指定不使用第三方程序。 –

+1

這是第一次沒有問過。 Zak的領導已經[如何批處理文件運行程序並設置窗口的位置和大小?](http://stackoverflow.com/questions/10392620/)另一個是[執行時指定命令提示符的大小一個批處理文件](http://stackoverflow.com/questions/27120267/)。還有一種方法會利用回答中提供的信息[命令提示符和cmd之間的區別是什麼?](http://stackoverflow.com/questions/34068830/) - 批量添加特定控制檯窗口的註冊表項並打開一個使用'start'的控制檯窗口。 – Mofi

回答

1

如果您安裝了PowerShell(您可能會這樣做),則可以使用user32.dll來移動和調整窗口。曾幾何時,我需要一個腳本來完成您所需要的工作,並且我找到this並對其進行調整以適合我的需求。然後,我使用this將其轉換爲批處理/ powershell混合,這樣我只需雙擊該文件即可運行PowerShell腳本。

<# : 
:: 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" 

請注意,我用Program Files版本IEXPLORE的,而不是Program Files (x86)版本,因爲一個不能移動或調整的某些原因。

相關問題