If you ever restore a window and immediately move it or resize it, the function below might be of interest to you. The function WinRestorePlus() combines the WinRestore and WinMove commands into one function, i.e. it restores a window directly to a new set of coordinates and/or dimensions. The key benefit is that it does so without any flicker.
Here is the function:
Code: Select all
; =============================================================================================================================
; WinRestorePlus(hwnd:="", X:="", Y:="", W:="", H:="")
; Function: Restore a window to a new set of coordinates/dimensions, regardless of its state (maximized, minimized, or normal).
; Combines the WinRestore and WinMove commands into one function with the benefit of no flicker.
; If the window is a normal window, this function will simply move/resize the window.
; If no parameters are specified, this function restores the active window, as in WinRestore, A.
; Tested with: AHK 1.1.26.01 (A32/U32/U64)
; AHK 2.0-a081-cad307c (U32/U64)
; Tested on: Win 7 (x64)
; Parameters: hwnd - (Optional) The handle of the window being restored. If not specified, the active window will be restored.
; X - (Optional) The x coordinate of the upper-left corner of the target window's new location*
; Y - (Optional) The y coordinate of the upper-left corner of the target window's new location*
; W - (Optional) The new width of the target window*
; H - (Optional) The new height of the target window*
; *If a coordinate is not specified, the corresponding value of the window in its restored state is used.
; Return values: nonzero if the window was successfully restored
; zero if the window was not successfully restored
; 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/ms633544(v=vs.85).aspx - SetWindowPlacement function
; https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85).aspx - WINDOWPLACEMENT structure
; =============================================================================================================================
WinRestorePlus(hwnd:="", X:="", Y:="", W:="", H:="") {
hwnd := hwnd = "" ? WinExist("A") : hwnd
VarSetCapacity(WP, 44, 0), NumPut(44, WP, "UInt")
DllCall("User32.dll\GetWindowPlacement", "Ptr", hwnd, "Ptr", &WP)
Lo := NumGet(WP, 28, "Int") ; X coordinate of the upper-left corner of the window in its original restored state
To := NumGet(WP, 32, "Int") ; Y coordinate of the upper-left corner of the window in its original restored state
Wo := NumGet(WP, 36, "Int") - Lo ; Width of the window in its original restored state
Ho := NumGet(WP, 40, "Int") - To ; Height of the window in its original restored state
L := X = "" ? Lo : X ; X coordinate of the upper-left corner of the window in its new restored state
T := Y = "" ? To : Y ; Y coordinate of the upper-left corner of the window in its new restored state
R := L + (W = "" ? Wo : W) ; X coordinate of the bottom-right corner of the window in its new restored state
B := T + (H = "" ? Ho : H) ; Y coordinate of the bottom-right corner of the window in its new restored state
NumPut(9, WP, 8, "UInt") ; SW_RESTORE = 9
NumPut(L, WP, 28, "Int")
NumPut(T, WP, 32, "Int")
NumPut(R, WP, 36, "Int")
NumPut(B, WP, 40, "Int")
Return DllCall("User32.dll\SetWindowPlacement", "Ptr", hwnd, "Ptr", &WP)
}
Code: Select all
WinSize := 400
MsgBox,
( Join
First, let's create a Notepad window and move it`n
to the bottom-right of the screen.
)
Run, Notepad, , , pid
WinWaitActive, ahk_pid %pid%
hwnd := WinExist("A")
SysGet, MonitorPos, MonitorWorkArea
WinMove, , , MonitorPosRight-MonitorPosLeft-WinSize, MonitorPosBottom-MonitorPosTop-WinSize, WinSize, WinSize
MsgBox Now, let's maximize it.
WinMaximize
MsgBox,
( Join
Now, let's restore and move the window using the built-in WinRestore and WinMove commands.`n`n
Note that the window is restored to the bottom-right and then quickly moved to the upper-left.
The flicker is visible even when a delay of -1 is used in the SetWinDelay command.
)
WinRestore
WinMove, 0, 0
MsgBox,
( Join
Now, let's try with the WinRestorePlus() function.`n`n
First, let's move the window to the bottom-right of the screen.
)
WinMove, , , MonitorPosRight-MonitorPosLeft-WinSize, MonitorPosBottom-MonitorPosTop-WinSize, WinSize, WinSize
MsgBox Now, let's maximize it again.
WinMaximize
MsgBox,
( Join
Now, let's restore and move the window using the WinRestorePlus() function.`n`n
Note that this is done in a single step, with no flicker, as the window is restored to the new position.
)
WinRestorePlus(hwnd, 0, 0)
MsgBox Now that we are done, let's close the window and quit.
WinClose
ExitApp
; =============================================================================================================================
; WinRestorePlus(hwnd:="", X:="", Y:="", W:="", H:="")
; Function: Restore a window to a new set of coordinates/dimensions, regardless of its state (maximized, minimized, or normal).
; Combines the WinRestore and WinMove commands into one function with the benefit of no flicker.
; If the window is a normal window, this function will simply move/resize the window.
; If no parameters are specified, this function restores the active window, as in WinRestore, A.
; Tested with: AHK 1.1.26.01 (A32/U32/U64)
; AHK 2.0-a081-cad307c (U32/U64)
; Tested on: Win 7 (x64)
; Parameters: hwnd - (Optional) The handle of the window being restored. If not specified, the active window will be restored.
; X - (Optional) The x coordinate of the upper-left corner of the target window's new location*
; Y - (Optional) The y coordinate of the upper-left corner of the target window's new location*
; W - (Optional) The new width of the target window*
; H - (Optional) The new height of the target window*
; *If a coordinate is not specified, the corresponding value of the window in its restored state is used.
; Return values: nonzero if the window was successfully restored
; zero if the window was not successfully restored
; 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/ms633544(v=vs.85).aspx - SetWindowPlacement function
; https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85).aspx - WINDOWPLACEMENT structure
; =============================================================================================================================
WinRestorePlus(hwnd:="", X:="", Y:="", W:="", H:="") {
hwnd := hwnd = "" ? WinExist("A") : hwnd
VarSetCapacity(WP, 44, 0), NumPut(44, WP, "UInt")
DllCall("User32.dll\GetWindowPlacement", "Ptr", hwnd, "Ptr", &WP)
Lo := NumGet(WP, 28, "Int") ; X coordinate of the upper-left corner of the window in its original restored state
To := NumGet(WP, 32, "Int") ; Y coordinate of the upper-left corner of the window in its original restored state
Wo := NumGet(WP, 36, "Int") - Lo ; Width of the window in its original restored state
Ho := NumGet(WP, 40, "Int") - To ; Height of the window in its original restored state
L := X = "" ? Lo : X ; X coordinate of the upper-left corner of the window in its new restored state
T := Y = "" ? To : Y ; Y coordinate of the upper-left corner of the window in its new restored state
R := L + (W = "" ? Wo : W) ; X coordinate of the bottom-right corner of the window in its new restored state
B := T + (H = "" ? Ho : H) ; Y coordinate of the bottom-right corner of the window in its new restored state
NumPut(9, WP, 8, "UInt") ; SW_RESTORE = 9
NumPut(L, WP, 28, "Int")
NumPut(T, WP, 32, "Int")
NumPut(R, WP, 36, "Int")
NumPut(B, WP, 40, "Int")
Return DllCall("User32.dll\SetWindowPlacement", "Ptr", hwnd, "Ptr", &WP)
}
Code: Select all
WinSize := 400
MsgBox "
( Join
First, let's create a Notepad window and move it`n
to the bottom-right of the screen.
)"
Run "Notepad", , , pid
WinWaitActive "ahk_pid " pid
hwnd := WinExist("A")
MonitorGetWorkArea(, MonitorPosLeft, MonitorPosTop, MonitorPosRight, MonitorPosBottom)
WinMove MonitorPosRight-MonitorPosLeft-WinSize, MonitorPosBottom-MonitorPosTop-WinSize, WinSize, WinSize
MsgBox "Now, let's maximize it."
WinMaximize
MsgBox "
( Join
Now, let's restore and move the window using the built-in WinRestore and WinMove commands.`n`n
Note that the window is restored to the bottom-right and then quickly moved to the upper-left.
The flicker is visible even when a delay of -1 is used in the SetWinDelay command.
)"
WinRestore
WinMove 0, 0
MsgBox "
( Join
Now, let's try with the WinRestorePlus() function.`n`n
First, let's move the window to the bottom-right of the screen.
)"
WinMove MonitorPosRight-MonitorPosLeft-WinSize, MonitorPosBottom-MonitorPosTop-WinSize, WinSize, WinSize
MsgBox "Now, let's maximize it again."
WinMaximize
MsgBox "
( Join
Now, let's restore and move the window using the WinRestorePlus() function.`n`n
Note that this is done in a single step, with no flicker, as the window is restored to the new position.
)"
WinRestorePlus(hwnd, 0, 0)
MsgBox "Now that we are done, let's close the window and quit."
WinClose
ExitApp
; =============================================================================================================================
; WinRestorePlus(hwnd:="", X:="", Y:="", W:="", H:="")
; Function: Restore a window to a new set of coordinates/dimensions, regardless of its state (maximized, minimized, or normal).
; Combines the WinRestore and WinMove commands into one function with the benefit of no flicker.
; If the window is a normal window, this function will simply move/resize the window.
; If no parameters are specified, this function restores the active window, as in WinRestore, A.
; Tested with: AHK 1.1.26.01 (A32/U32/U64)
; AHK 2.0-a081-cad307c (U32/U64)
; Tested on: Win 7 (x64)
; Parameters: hwnd - (Optional) The handle of the window being restored. If not specified, the active window will be restored.
; X - (Optional) The x coordinate of the upper-left corner of the target window's new location*
; Y - (Optional) The y coordinate of the upper-left corner of the target window's new location*
; W - (Optional) The new width of the target window*
; H - (Optional) The new height of the target window*
; *If a coordinate is not specified, the corresponding value of the window in its restored state is used.
; Return values: nonzero if the window was successfully restored
; zero if the window was not successfully restored
; 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/ms633544(v=vs.85).aspx - SetWindowPlacement function
; https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85).aspx - WINDOWPLACEMENT structure
; =============================================================================================================================
WinRestorePlus(hwnd:="", X:="", Y:="", W:="", H:="") {
hwnd := hwnd = "" ? WinExist("A") : hwnd
VarSetCapacity(WP, 44, 0), NumPut(44, WP, "UInt")
DllCall("User32.dll\GetWindowPlacement", "Ptr", hwnd, "Ptr", &WP)
Lo := NumGet(WP, 28, "Int") ; X coordinate of the upper-left corner of the window in its original restored state
To := NumGet(WP, 32, "Int") ; Y coordinate of the upper-left corner of the window in its original restored state
Wo := NumGet(WP, 36, "Int") - Lo ; Width of the window in its original restored state
Ho := NumGet(WP, 40, "Int") - To ; Height of the window in its original restored state
L := X = "" ? Lo : X ; X coordinate of the upper-left corner of the window in its new restored state
T := Y = "" ? To : Y ; Y coordinate of the upper-left corner of the window in its new restored state
R := L + (W = "" ? Wo : W) ; X coordinate of the bottom-right corner of the window in its new restored state
B := T + (H = "" ? Ho : H) ; Y coordinate of the bottom-right corner of the window in its new restored state
NumPut(9, WP, 8, "UInt") ; SW_RESTORE = 9
NumPut(L, WP, 28, "Int")
NumPut(T, WP, 32, "Int")
NumPut(R, WP, 36, "Int")
NumPut(B, WP, 40, "Int")
Return DllCall("User32.dll\SetWindowPlacement", "Ptr", hwnd, "Ptr", &WP)
}
- iPhilip