- if you manually re-position the owned window over the owner, it remembers and new relationship.
Code: Select all
#NoEnv
#SingleInstance force
SetWinDelay, -1
run notepad.exe
WinWaitActive, ahk_class Notepad
WinGet, PID, PID, % "ahk_id " . owner:=WinExist() ; if all parameters are omitted, WinExist returns the ID the Last Found Window if it still exist; WInWait set the Last Found Window
GUI, +ToolWindow +Owner%owner% +Hwndowned
GUI, Show, w100 h300
OnLocationChangeMonitor(owner, owned, PID) ; INIT
WinWaitClose % "ahk_id " . owner ; ... or until the owner window does not exist
ExitApp
OnLocationChangeMonitor(_hWinEventHook, _event, _hwnd) { ; https://msdn.microsoft.com/en-us/library/windows/desktop/dd373885(v=vs.85).aspx
STATIC ox, oy, nx, ny, ownerAhkId, ownedAhkId, hWinEventHook
IF !_hwnd ; if the system sent the EVENT_OBJECT_LOCATIONCHANGE event for the caret:
Return ; https://msdn.microsoft.com/en-us/library/windows/desktop/dd318066(v=vs.85).aspx
IF !hWinEventHook ; register a event hook function for EVENT_OBJECT_LOCATIONCHANGE := "0x800B"
hWinEventHook := SetWinEventHook("0x800B", "0x800B",0, RegisterCallback("OnLocationChangeMonitor"),_hwnd,0,0)
, ownerAhkId := _hWinEventHook, ownedAhkId := _event, OnExit(Func("UnhookWinEvent").Bind(hWinEventHook))
WinGetPos, _x, _y,,, ahk_id %ownerAhkId%
WinGetPos, _x1, _y1,,, ahk_id %ownedAhkId%
IF (_x1!=ox || _y1!=oy)
nx:=_x-_x1, ny:=_y-_y1
WinMove, ahk_id %ownedAhkId%,, % ox:=_x-nx, % oy:=_y-ny ; set the position of the window owned by owner
}
SetWinEventHook(_eventMin, _eventMax, _hmodWinEventProc, _lpfnWinEventProc, _idProcess, _idThread, _dwFlags) {
DllCall("CoInitialize", "Uint", 0)
return DllCall("SetWinEventHook","Uint",_eventMin,"Uint",_eventMax,"Ptr",_hmodWinEventProc,"Ptr",_lpfnWinEventProc,"Uint",_idProcess,"Uint",_idThread,"Uint",_dwFlags)
} ; cf. https://autohotkey.com/boards/viewtopic.php?t=830
UnhookWinEvent(_hWinEventHook) {
DllCall("UnhookWinEvent", "Ptr", _hWinEventHook)
DllCall("CoUninitialize")
} ; cf. https://autohotkey.com/boards/viewtopic.php?t=830