Below is a function (including example) to determine if a window has been docked, as in when the window is snapped to the left/right side of the screen using the #{Left}/#{Right} shortcuts or stretched to the top and bottom of the screen using the #+{Up} shortcut. See this link for more information about the shortcuts.
Here is the function:
Code: Select all
; =============================================================================================================================
; IsWindowDocked(hwnd, ByRef dock)
; Function: Determines if a window is docked
; Tested with: AHK 1.1.26.00 (A32/U32/U64)
; Tested on: Win 7 (x64)
; Parameters: hwnd - the handle of the window being tested
; loc - the resulting location of the docked window:
; -1 if the window is docked on the left
; 0 if the window is docked on the top and bottom
; 1 if the window is docked on the right
; blank if the window is not docked
; Return values: true if the window is normal and docked
; false if the window is normal and not docked
; blank if the window is not normal, i.e. the window is maximized or minimized
; MSDN links: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633518(v=vs.85).aspx - GetWindowPlacement function
; https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85).aspx - WINDOWPLACEMENT structure
; =============================================================================================================================
IsWindowDocked(hwnd, ByRef loc) {
static SW_SHOWNORMAL := 1
VarSetCapacity(WP,44), NumPut(44,WP)
DllCall("GetWindowPlacement", Ptr, hwnd, Ptr, &WP)
sw := NumGet(WP, 8, "UInt") ; showCmds - current show state of the window
x0 := NumGet(WP, 28, "Int") ; rcNormalPosition - x coordinate of the upper-left corner of the window in its restored state
y0 := NumGet(WP, 32, "Int") ; rcNormalPosition - y coordinate of the upper-left corner of the window in its restored state
w0 := NumGet(WP, 36, "Int") - x0 ; rcNormalPosition - width of the window in its restored state
h0 := NumGet(WP, 40, "Int") - y0 ; rcNormalPosition - height of the window in its restored state
WinGetPos, x1, y1, w1, h1, ahk_id %hwnd% ; Get the position and size of the window in its current state
docked := sw = SW_SHOWNORMAL ? (x0 <> x1 || y0 <> y1 || w0 <> w1 || h0 <> h1) : "" ; Determine if the window is docked by comparing coordinates and sizes
loc := docked ? (x1 = 0 ? -1 : (x1 + w1 = A_ScreenWidth ? 1 : 0)) : "" ; Determine how the window is docked by examining the left/right side of the window
Return docked
}
Code: Select all
dock := {-1:{label:"left",keys:"#{Left}"}
, 0:{label:"top & bottom",keys:"#+{Up}"}
, 1:{label:"right",keys:"#{Right}"}}
Run, Notepad, , , pid
WinWaitActive, Untitled - Notepad ahk_pid %pid%
hwnd := WinExist("A")
MsgBox % "The window is " (IsWindowDocked(hwnd,index) ? "docked on the " dock[index].label : "not docked")
Send, % dock[-1].keys
MsgBox % "The window is " (IsWindowDocked(hwnd,index) ? "docked on the " dock[index].label : "not docked")
WinRestore, ahk_id %hwnd%
Send, % dock[0].keys
MsgBox % "The window is " (IsWindowDocked(hwnd,index) ? "docked on the " dock[index].label : "not docked")
Send, % dock[1].keys
MsgBox % "The window is " (IsWindowDocked(hwnd,index) ? "docked on the " dock[index].label : "not docked")
WinMaximize, ahk_id %hwnd%
MsgBox % "The window is " (IsWindowDocked(hwnd,index) ? "docked on the " dock[index].label : "not docked")
WinRestore, ahk_id %hwnd%
MsgBox % "The window is " (IsWindowDocked(hwnd,index) ? "docked on the " dock[index].label : "not docked")
WinClose, ahk_id %hwnd%
ExitApp
; =============================================================================================================================
; IsWindowDocked(hwnd, ByRef dock)
; Function: Determines if a window is docked
; Tested with: AHK 1.1.26.00 (A32/U32/U64)
; Tested on: Win 7 (x64)
; Parameters: hwnd - the handle of the window being tested
; loc - the resulting location of the docked window:
; -1 if the window is docked on the left
; 0 if the window is docked on the top and bottom
; 1 if the window is docked on the right
; blank if the window is not docked
; Return values: true if the window is normal and docked
; false if the window is normal and not docked
; blank if the window is not normal, i.e. the window is maximized or minimized
; MSDN links: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633518(v=vs.85).aspx - GetWindowPlacement function
; https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85).aspx - WINDOWPLACEMENT structure
; =============================================================================================================================
IsWindowDocked(hwnd, ByRef loc) {
static SW_SHOWNORMAL := 1
VarSetCapacity(WP,44), NumPut(44,WP)
DllCall("GetWindowPlacement", Ptr, hwnd, Ptr, &WP)
sw := NumGet(WP, 8, "UInt") ; showCmds - current show state of the window
x0 := NumGet(WP, 28, "Int") ; rcNormalPosition - x coordinate of the upper-left corner of the window in its restored state
y0 := NumGet(WP, 32, "Int") ; rcNormalPosition - y coordinate of the upper-left corner of the window in its restored state
w0 := NumGet(WP, 36, "Int") - x0 ; rcNormalPosition - width of the window in its restored state
h0 := NumGet(WP, 40, "Int") - y0 ; rcNormalPosition - height of the window in its restored state
WinGetPos, x1, y1, w1, h1, ahk_id %hwnd% ; Get the position and size of the window in its current state
docked := sw = SW_SHOWNORMAL ? (x0 <> x1 || y0 <> y1 || w0 <> w1 || h0 <> h1) : "" ; Determine if the window is docked by comparing coordinates and sizes
loc := docked ? (x1 = 0 ? -1 : (x1 + w1 = A_ScreenWidth ? 1 : 0)) : "" ; Determine how the window is docked by examining the left/right side of the window
Return docked
}
- iPhilip