Skip to content

Instantly share code, notes, and snippets.

@theFluffyAsian
Forked from AWMooreCO/AdvancedWindowSnap.ahk
Last active June 17, 2020 09:26
Show Gist options
  • Select an option

  • Save theFluffyAsian/27fff52adafad8b778271e99755bc913 to your computer and use it in GitHub Desktop.

Select an option

Save theFluffyAsian/27fff52adafad8b778271e99755bc913 to your computer and use it in GitHub Desktop.
Advanced Window Snap is a script for AutoHotKey that expands upon Windows built-in window-snapping hotkeys.

Advanced Window Snap

Advanced Window Snap is a script for AutoHotKey that expands upon Windows built-in window-snapping hotkeys (which are Win + LEFT to snap an active window to the left half of a monitor and Win + RIGHT to snap a window to the right half of a monitor) by adding 14 additional snap methods and window sizing options.

Installation Steps

  1. Install AutoHotKey
  2. Copy or Download the UltraWideAdvancedWindowSnap.ahk file to your computer and double click it to run it.
  3. (Optional) To have the program run when you start up your computer, place the .ahk file into your computer's startup folder.
    • The Windows 7 Startup Folder can be accessed by mousing to Start > All Programs, then right-clicking on Startup and selecting "Open".
    • The Windows 8 Startup Folder can be accessed by tapping Win + R on your keyboard, then in the Open: field, type shell:startup then press Enter.
    • In windows 10:
      • The All Users Startup folder is found in the following path: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
      • The Current User Startup folder is located here: C:\Users\[User Name]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Advanced Window Snap Keybindings

Vertical Monitor Hotkeys:

Hotkey Behavior
Win + Alt + UP Window will snap to the top half of the screen.
Win + Alt + DOWN Window will snap to the bottom half of the screen.
Ctrl + Win + Alt + UP Window will snap to the top third of the screen.
Ctrl + Win + Alt + DOWN Window will snap to the bottom third of the screen.
Ctrl + Win + Numpad 0 Window will snap to the middle third of the screen.

UltraWide Monitor Hotkeys (Landscape):

These will work only if you have NumLock turned ON. These are ideal for Landscape Monitors. They split the screen into 9 regions in the same orientation as the number pad.

Hotkey Behavior
Win + Alt + Numpad 1 Window will snap to the bottom-left third of the screen
Win + Alt + Numpad 2 Window will snap to the bottom-middle third of the screen
Win + Alt + Numpad 3 Window will snap to the bottom-right third of the screen
Win + Alt + Numpad 4 Window will snap to the middle-left third of the screen
Win + Alt + Numpad 5 Window will snap to the middle-middle third of the screen
Win + Alt + Numpad 6 Window will snap to the middle-right third of the screen
Win + Alt + Numpad 7 Window will snap to the top-left third of the screen
Win + Alt + Numpad 8 Window will snap to the top-middle third of the screen
Win + Alt + Numpad 9 Window will snap to the top-right third of the screen

Modifier Hotkeys:

These will work only if you have NumLock turned ON. These are ideal for Portrait Monitors.

Hotkey Behavior
Win + Numpad 0 Window will stretch to the full length of the screen.
Alt + Numpad plus Window increases height by third of the screen height.
Alt + Numpad minus Window decreases height by third of the screen height. Or to minium.
Alt + Numpad times Window increases width by third of the screen width.
Alt + Numpad divide Window decrease to the bottom third of the screen. Or to minium.

Changelog

  • v1.00, 06/2020
    • Initial version
/**
* SnapActiveWindow resizes and moves (snaps) the active window to a given position.
* @param {string} winPlaceVertical The vertical placement of the active window.
* Expecting "bottom" or "middle", otherwise assumes
* "top" placement.
* @param {string} winPlaceHorizontal The horizontal placement of the active window.
* Expecting "left", "right", "middle", otherwise assumes
* window should span the "full" width of the monitor.
* @param {string} winSizeHeight The height of the active window in relation to
* the active monitor's height. Expecting "half" size,
* otherwise will resize window to a "third".
*/
SnapActiveWindow(winPlaceVertical, winPlaceHorizontal, winSizeHeight) {
WinGet activeWin, ID, A
activeMon := GetMonitorIndexFromWindow(activeWin)
SysGet, MonitorWorkArea, MonitorWorkArea, %activeMon%
if (winSizeHeight == "half") {
height := (MonitorWorkAreaBottom - MonitorWorkAreaTop)/2
} else if (winSizeHeight == "full") {
height := MonitorWorkAreaBottom
} else {
height := (MonitorWorkAreaBottom - MonitorWorkAreaTop)/3
}
if (winPlaceHorizontal == "left") {
posX := MonitorWorkAreaLeft
width := (MonitorWorkAreaRight - MonitorWorkAreaLeft)/3
} else if (winPlaceHorizontal == "right") {
posX := MonitorWorkAreaLeft + 2*(MonitorWorkAreaRight - MonitorWorkAreaLeft)/3
width := (MonitorWorkAreaRight - MonitorWorkAreaLeft)/3
} else if (winPlaceHorizontal == "middle") {
posX := MonitorWorkAreaLeft + (MonitorWorkAreaRight - MonitorWorkAreaLeft)/3
width := (MonitorWorkAreaRight - MonitorWorkAreaLeft)/3
} else {
posX := MonitorWorkAreaLeft
width := MonitorWorkAreaRight - MonitorWorkAreaLeft
}
if (winPlaceVertical == "bottom") {
posY := MonitorWorkAreaBottom - height
} else if (winPlaceVertical == "middle") {
posY := MonitorWorkAreaTop + height
} else {
posY := MonitorWorkAreaTop
}
WinMove,A,,%posX%,%posY%,%width%,%height%
}
/**
* ResizeActiveWindow resizes the active window to the full height on the monitor.
* @param {string} stretchDirection The direction in which to stretch the active window.
* Expects "vertical". No other function is supported yet.
*/
ResizeActiveWindow(stretchDirection) {
WinGet activeWin, ID, A
WinGetPos, posX, posY, Width, Height, A
activeMon := GetMonitorIndexFromWindow(activeWin)
SysGet, MonitorWorkArea, MonitorWorkArea, %activeMon%
if (stretchDirection == "vertical") {
height := MonitorWorkAreaBottom
width := Width
} else if (stretchDirection == "shrinkWidth") {
width := Width - MonitorWorkAreaRight/3
height := Height
} else if (stretchDirection == "expandWidth") {
width := Width + MonitorWorkAreaRight/3
height := Height
} else if (stretchDirection == "shrinkHeight") {
height := Height - MonitorWorkAreaBottom/3
width := Width
} else if (stretchDirection == "expandHeight") {
height := Height + MonitorWorkAreaBottom/3
width := Width
}
WinMove,A,,%posX%,0,%width%,%height%
}
/**
* GetMonitorIndexFromWindow retrieves the HWND (unique ID) of a given window.
* @param {Uint} windowHandle
* @author shinywong
* @link http://www.autohotkey.com/board/topic/69464-how-to-determine-a-window-is-in-which-monitor/?p=440355
*/
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%
}
; NineGrid
#!Numpad9::SnapActiveWindow("top","right","third")
#!Numpad8::SnapActiveWindow("top","middle","third")
#!Numpad7::SnapActiveWindow("top","left","third")
#!Numpad6::SnapActiveWindow("middle","right","third")
#!Numpad5::SnapActiveWindow("middle","middle","third")
#!Numpad4::SnapActiveWindow("middle","left","third")
#!Numpad3::SnapActiveWindow("bottom","right","third")
#!Numpad2::SnapActiveWindow("bottom","middle","third")
#!Numpad1::SnapActiveWindow("bottom","left","third")
#!Numpad0::ResizeActiveWindow("vertical")
#!NumpadDiv::ResizeActiveWindow("shrinkWidth")
#!NumpadMult::ResizeActiveWindow("expandWidth")
#!NumpadSub::ResizeActiveWindow("shrinkHeight")
#!NumpadAdd::ResizeActiveWindow("expandHeight")
; Directional Arrow Hotkeys
#!Up::SnapActiveWindow("top","full","half")
#!Down::SnapActiveWindow("bottom","full","half")
^#!Up::SnapActiveWindow("top","full","third")
^#!Down::SnapActiveWindow("bottom","full","third")
^#!Numpad0::SnapActiveWindow("middle","full","third")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment