[Windows] 視窗水平分割教學 – 水平視窗排列 – 使用AutoHotKey

使用情境

Windows要水平分割視窗竟然會如此困難即便到了Windows 10。

原生半調子解法

以Win10來說,工作列使用水平排列(Show windows stacked),可將現有視窗做水平排列,但相信我絕對不會是你要的。


使用AutoHotKey解法

首先認識AutoHotKey這套Open Source軟體,可以開啟.ahk檔案執行script。

軟體的基本使用可以參考:The Beginner’s Guide to Using an AutoHotkey Script

再來引用水平視窗切割(Vertical Windows Snapping)的Script,優化自:Snapping Windows Vertically on Portrait Display

; dir 0 = top part
; dir 1 = bottom part
; size 0 = half of screen
; size 1 = third of screen

ResizeWin(dir = 1, size = 0)
{
    WinGet activeWin, ID, A
    activeMon := GetMonitorIndexFromWindow(activeWin)

    SysGet, MonitorWorkArea, MonitorWorkArea, %activeMon%

    w := MonitorWorkAreaRight - MonitorWorkAreaLeft
    if (size == 0)
        h := (MonitorWorkAreaBottom - MonitorWorkAreaTop)/2
    else
        h := (MonitorWorkAreaBottom - MonitorWorkAreaTop)/3

    if (dir == 1)
        t := MonitorWorkAreaBottom - h
    else
        t := MonitorWorkAreaTop

    WinMove,A,,%MonitorWorkAreaLeft%,%t%,%w%,%h%
}

GetMonitorIndexFromWindow(windowHandle)
{
    ; Starts with 1.
    monitorIndex := 1

    VarSetCapacity(monitorInfo, 40)
    NumPut(40, monitorInfo)

    if (monitorHandle := DllCall("MonitorFromWindow", "uint", windowHandle, "uint", 0x2)) 
        && DllCall("GetMonitorInfo", "uint", monitorHandle, "uint", &monitorInfo) 
    {
        monitorLeft   := NumGet(monitorInfo,  4, "Int")
        monitorTop    := NumGet(monitorInfo,  8, "Int")
        monitorRight  := NumGet(monitorInfo, 12, "Int")
        monitorBottom := NumGet(monitorInfo, 16, "Int")
        workLeft      := NumGet(monitorInfo, 20, "Int")
        workTop       := NumGet(monitorInfo, 24, "Int")
        workRight     := NumGet(monitorInfo, 28, "Int")
        workBottom    := NumGet(monitorInfo, 32, "Int")
        isPrimary     := NumGet(monitorInfo, 36, "Int") & 1

        SysGet, monitorCount, MonitorCount

        Loop, %monitorCount%
        {
            SysGet, tempMon, Monitor, %A_Index%

            ; Compare location to determine the monitor index.
            if ((monitorLeft = tempMonLeft) and (monitorTop = tempMonTop)
                and (monitorRight = tempMonRight) and (monitorBottom = tempMonBottom))
            {
                monitorIndex := A_Index
                break
            }
        }
    }

    return %monitorIndex%
}

#!Up::ResizeWin(0)
#!Down::ResizeWin(1)
#+Up::ResizeWin(0)
#+Down::ResizeWin(1)
^#!Up::ResizeWin(0,1)
^#!Down::ResizeWin(1,1)

安裝AutoHotKey -> 用上述script產生.ahk檔 -> 點擊執行並確認有無在工作列執行

Script使用方法

Win + Alt + Up - Snap window to top half of active screen
Win + Shift + Up
Win + Alt + Down - Snap window to bottom half of active screen
Win + Shift + Down
Ctrl + Win + Alt + Up - Snap window to top third of active screen
Ctrl + Win + Alt + Down - Snap window to bottom half of active screen

Leave a Reply

Your email address will not be published. Required fields are marked *