Page 1 of 1

Getting Window Client position

Posted: 13 Nov 2023, 14:08
by joefiesta
If found the function WinGetClientPos() here: https://www.autohotkey.com/board/topic/98679-function-wingetclientpos/

I reduced it to the following, where I simply plug in (for simplicity in testing) the target window's HWND.

Client := WinGetClientPos(5310346)
msgbox % "x" Client.X " y" Client.Y " w" Client.W " h" Client.H
return

WinGetClientPos( Hwnd ) {
VarSetCapacity( size, 16, 0 )
DllCall( "GetClientRect", UInt, Hwnd, Ptr, &size )
DllCall( "ClientToScreen", UInt, Hwnd, Ptr, &size )
x := NumGet(size, 0, "Int")
y := NumGet(size, 4, "Int")
w := NumGet( size, 8, "Int" )
h := NumGet( size, 12, "Int" )
return { X: x, Y: y, W: w, H: h }
}


Apparently, if it ever worked, it was for 32-bit AHK.
I run Win10 64 bit.
Can someone tell me how to make this work?

Re: Getting Window Client position

Posted: 13 Nov 2023, 14:11
by iPhilip
Why not use the built-in WinGetClientPos?

Re: Getting Window Client position

Posted: 13 Nov 2023, 14:15
by gregster
I assume this is again a v1 question posted in the wrong forum?
Btw, for code boxes I would recommend code-tags instead of c-tags, which are meant for inline code.

Re: Getting Window Client position

Posted: 13 Nov 2023, 14:19
by iPhilip
If it's a v1 question, the solution is simply to replace UInt with Ptr in the DllCall's.

Re: Getting Window Client position

Posted: 13 Nov 2023, 14:22
by joefiesta
sorry, I was just about to edit. this after I noticed that, yes, it is in the WRONG forum. It is for V1. And, V1 has no WinGetClientPos.

Re: Getting Window Client position

Posted: 13 Nov 2023, 14:25
by joefiesta
@iPhillip: I tried as you suggested. It didn't help. New code:

Code: Select all

Client := WinGetClientPos(5310346)
msgbox  % "x" Client.X " y" Client.Y " w" Client.W " h" Client.H
return

WinGetClientPos( Hwnd ) {
   VarSetCapacity( size, 16, 0 )
   DllCall( "GetClientRect", Ptr, Hwnd, Ptr, &size )
   DllCall( "ClientToScreen", Ptr, Hwnd, Ptr, &size )
   x := NumGet(size, 0, "Int")
   y := NumGet(size, 4, "Int")
   w := NumGet( size, 8, "Int" )
   h := NumGet( size, 12, "Int" )
   return { X: x, Y: y, W: w, H: h }
}
@gregster How do I use Code tags? Are they one of the icons shown when using Full editor? edit: nevermind... i hovered over the icons and found which it is.

Re: Getting Window Client position

Posted: 13 Nov 2023, 14:30
by gregster
Okay, I moved the topic to v1 help.

You can insert code tags by using the fifth button from the left in the full editor. It either says Code or </>, depending on the used forum theme.

ctags.png
ctags.png (14.2 KiB) Viewed 691 times

Re: Getting Window Client position

Posted: 13 Nov 2023, 14:34
by iPhilip
@joefiesta, This works for me:

Code: Select all

Client := WinGetClientPos(WinExist("A"))
msgbox  % "x" Client.X " y" Client.Y " w" Client.W " h" Client.H  ; x0 y43 w1920 h997
return

WinGetClientPos( Hwnd ) {
   VarSetCapacity( size, 16, 0 )
   DllCall( "GetClientRect", Ptr, Hwnd, Ptr, &size )
   DllCall( "ClientToScreen", Ptr, Hwnd, Ptr, &size )
   x := NumGet(size, 0, "Int")
   y := NumGet(size, 4, "Int")
   w := NumGet( size, 8, "Int" )
   h := NumGet( size, 12, "Int" )
   return { X: x, Y: y, W: w, H: h }
}

Re: Getting Window Client position

Posted: 14 Nov 2023, 08:41
by just me

Code: Select all

Client := WinGetClientPos(5310346)
If the number is a window handle (HWND): Window handles are arbitrary. Each time a window is created it gets a new HWND which is only valid until the window is destroyed. Are you sure this HWND belongs to your target window?

Re: Getting Window Client position

Posted: 14 Nov 2023, 10:28
by joefiesta
This question is resolved. Thanks all.

@iPhillip : thanks for the experimenting. I don't know what I did wrong, but you were absolutely correct. It's work fine for me now.

@ Jkust me : I realize what you said about hwnd. If you notice, I mentioned the example was just overly simplified for testing, including not getting any window but rather one that, for me, always exists.