[Function] IsWindowDocked

Post your working scripts, libraries and tools for AHK v1.1 and older
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

[Function] IsWindowDocked

21 Jul 2017, 13:43

Hi folks,

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
}
Here is an self-contained example:

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
}
I hope you find it useful.

- iPhilip
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
trump

Re: [Function] IsWindowDocked

04 Aug 2017, 10:05

Very useful.

Just one thing, how to dock a window using DllCall?

Thanks iPhilip
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: [Function] IsWindowDocked

04 Aug 2017, 18:47

I don't think that's possible. In my search online, I saw several comments as follows:
Unfortunately not, Microsoft in their wisdom did not provide any public API for the Aero Snap functionality.
Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: [Function] IsWindowDocked

04 Aug 2017, 21:19

iPhilip wrote:
Unfortunately not, Microsoft in their wisdom did not provide any public API for the Aero Snap functionality.
:lol: Typical Microsoft.

I'm curious what practicality there is for knowing if a window is snapped or not. What did you make this for?
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: [Function] IsWindowDocked

04 Aug 2017, 21:58

I am working on a script that involves moving windows temporarily and restoring them at the end to their original conditions. Thus, I was curious to see if it was possible to detect a docked window, since that is a possible initial state. As it turns out, detecting such a window is relatively straightforward but redocking it requires activating the window and sending the appropriate key sequence.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Guest67

Re: [Function] IsWindowDocked

10 Aug 2017, 22:57

This might be a workaround on how to restore Aerop-Snap.
https://inputsimulator.codeplex.com/
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: [Function] IsWindowDocked

16 Aug 2017, 10:58

Hi Guest67,

I am not sure what you mean by
Guest67 wrote:... restore Aerop-Snap.
but, as the above example shows, WinRestore restores any docked window.

Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Guest67

Re: [Function] IsWindowDocked

17 Aug 2017, 13:41

WinRestore: "Unminimizes or unmaximizes the specified window if it is minimized or maximized."
That's not what I meant by restoring sock/snap state.
I meant how to restore snap/dock, if any, on a gui startup? WinMove can resize a window to look like it's docked, but it's not the same thing.
Since there is nothing on msdn that talks about restoring aero-snap/dock then keyboard simulation is the only workaround I can think of.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Epoch, gwarble, mcd and 59 guests