Get Owner Window Coordinates Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Get Owner Window Coordinates

Post by Renfro » 14 Apr 2021, 13:36

I'm trying to get the coordinates of a parent window, so that I can then use WinMove to move the current (child) window relative to the parent window's position.

I did some hunting around and found some code examples which allowed me to put together the following:

Code: Select all

ID := DllCall("GetParent", UInt,WinExist("A")), ID := !ID ? WinExist("A") : ID
;WinGetClass, Class, ahk_id %id%
;WinGetTitle, Title, ahk_id %id%
;MsgBox, %ID%

WinGetPos, X, Y, , , A  ; "A" to get the active window's pos.
WinGetPos, pX, pY, , , ahk_id %ID%  ;  to get the parent window's pos.
MsgBox, The active window is at %X%`, %Y%`n`nThe parent window is at %pX%`, %pY%`
But it only gives the current child window coordinates, never the parent window to which it belongs. So in the message box the values are always identical for both active window and parent window.

How can I get the coordinates of the parent window to which the active window belongs?
Last edited by Renfro on 15 Apr 2021, 03:05, edited 1 time in total.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get Parent Window Coordinates

Post by teadrinker » 14 Apr 2021, 14:26

Renfro wrote: How can I get the coordinates of the parent window to which the active window belongs?
If a window has the parent, it can't be active:

Code: Select all

Gui, +hwndhGui
Gui, Add, Text, hwndhText, Hello, World!
Gui, Show, w300 h100
In this examle the child window has id hText, the parent hGui.
Perhaps you mean something else by "parent"?

Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Re: Get Parent Window Coordinates

Post by Renfro » 14 Apr 2021, 14:52

Oh, maybe I'm using the wrong terminology.

I am talking about a window that "belongs" to an application but is not the main window of that application. In this case it's a popup window in Firefox, but it is not the main browser window. I want to move the popup window based on where the main Firefox window is located.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get Parent Window Coordinates

Post by teadrinker » 14 Apr 2021, 15:10

It is called an owner. An example:

Code: Select all

Gui, Main:New, +hwndhMain
Gui, Add, Text,, I'm an owner, my ID is %hMain%
Gui, Show, w300 h300
Gui, Owned:New, +OwnerMain +hwndhOwned
Gui, Add, Text,, I'm owned by Main GUI, my ID is %hOwned%
Gui, Add, Button, x60 y+20 w130 h24 gGetOwner, Get Owner Window
Gui, Show, w250 h100
Return

GetOwner:
   hOwner := DllCall("GetWindowLong" . (A_PtrSize = 4 ? "" : "Ptr"), "Ptr", hOwned, "Int", GWLP_HWNDPARENT := -8, "Ptr")
   WinGetPos, X, Y,,, ahk_id %hOwner%
   MsgBox, % "My owner ID is " . Format("{:#x}", hOwner) . "`nIts coords: x " . X . " y " . Y
   Return

MainGuiClose:
   ExitApp

Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Re: Get Parent Window Coordinates

Post by Renfro » 14 Apr 2021, 16:34

Thank you for recognizing that I might be using the wrong terminology and providing the correct name. I didn't even realize that I was using a term that meant something different to what I thought it meant, so the heads up is appreciated.

Your code example is great! It does exactly what I was trying to achieve, and it's laid out in a very clear manner.

I have just one remaining issue.

While your example works well with the included GUI, when I try to run it on an actual application popup window I get an owner ID of "0" and blank x and y coordinates. It's not a code issue, it's just a "me" issue (I can't figure out how to apply your code correctly). I was trying to make the GetOwner code section check the currently focused window when I press a hotkey, but I just get a msgbox with empty values for ID and coordinates.

How do I run the GetOwner code for the currently focused window?

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get Parent Window Coordinates  Topic is solved

Post by teadrinker » 14 Apr 2021, 17:03

An example with the "real" window:

Code: Select all

Run, notepad,,, PID
WinWait, ahk_pid %PID%
ControlSetText, Edit1, % "My ID is " . WinExist()
WinMenuSelectItem,,, 3&, 2&
WinWait, ahk_class #32770 ahk_pid %PID%

hWnd := WinExist("A")
hOwner := DllCall("GetWindowLong" . (A_PtrSize = 4 ? "" : "Ptr"), "Ptr", hWnd, "Int", GWLP_HWNDPARENT := -8, "Ptr")
MsgBox, % "Owner ID is " . Format("{:#x}", hOwner)

Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Re: Get Owner Window Coordinates

Post by Renfro » 15 Apr 2021, 03:18

Thank you. I've got it working now.

Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Re: Get Owner Window Coordinates

Post by Renfro » 15 Apr 2021, 17:01

Hi @teadrinker,

The code you provided has been working great, but I've come across one instance where it doesn't work.

Maybe this is because it's a different window type, I'm not sure. It has the exact same class as all the other popup windows that I've been successfully working with (ahk_class MozillaDialogClass) but I noticed that this particular popup window (despite looking similar to the others) has its own taskbar button, where as the others windows did not have taskbar buttons. It does not have maximize or minimize buttons on its title bar though, and its ahk_exe is the same as the web browser from which it was launched, so it's definitely part of the web browser (whose coordinates I am trying to get when that particular popup window has focus) and not a totally separate stand-alone app.

I tried looking > here < to see if there was some other kind of check that I could do instead of GWLP_HWNDPARENT but I was in way over my head with that stuff and didn't understand any of it.

Is there a way to get this particular window to work with a similar method that you previously provided for other owned windows?

Thanks!

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get Owner Window Coordinates

Post by teadrinker » 15 Apr 2021, 17:38

Renfro wrote: Maybe this is because it's a different window type, I'm not sure. It has the exact same class as all the other popup windows that I've been successfully working with (ahk_class MozillaDialogClass)
Sorry, I don't use FF, I'm not sure what are the windows you are talking about. How can I open such window?

Post Reply

Return to “Ask for Help (v1)”