[Function] WinGetClientPos()

Post your working scripts, libraries and tools
dd900
Posts: 76
Joined: 27 Oct 2013, 16:03

[Function] WinGetClientPos()

01 Nov 2013, 18:11

Working example and Function

Code: Select all

Gui New, +HwndhGUI +LabelGui_ +Resize
Gui %hGUI%:Show, w500 h500
MsgBox The Gui client area will now be covered with a new black gui
Gui New, +HwndhGUI2 -Caption +ToolWindow +AlwaysOnTop
Gui %hGUI2%:Color, 000000
Client := WinGetClientPos( hGUI )
Gui %hGUI2%:Show, % "x" Client.X " y" Client.Y " w" Client.W " h" Client.H
OnMessage( 0x03, "GuiChanged" )
OnMessage( 0x05, "GuiChanged" )
return

Gui_Close:
	ExitApp

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 }
}

GuiChanged() {
	global hGUI,hGUI2
	Client := WinGetClientPos( hGUI )
	Gui %hGUI2%:Show, % "x" Client.X " y" Client.Y " w" Client.W " h" Client.H
}
I've seen other functions like this, but none ( that I found ) gave both the position and size.I was aiming for functionality similar to WinGetPos.
Last edited by dd900 on 02 Nov 2013, 20:11, edited 3 times in total.
lexikos
Posts: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [Function] WinGetClientPos()

01 Nov 2013, 18:14

FYI, you can shorten

Code: Select all

    VarSetCapacity(pos, 8)
    NumPut(x, pos, 0)
    NumPut(y, pos, 4)
to

Code: Select all

    VarSetCapacity(pos, 8, 0)
dd900
Posts: 76
Joined: 27 Oct 2013, 16:03

Re: [Function] WinGetClientPos()

01 Nov 2013, 18:18

Updated function. Thank you.
just me
Posts: 5658
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [Function] WinGetClientPos()

02 Nov 2013, 04:47

You may use the address of theRECT structure retrieved by GetClientRect() directly as the second parameter of ClientToScreen(); it will only change the first two members (i.e. left & top).
Also, the DllCall() type of pointers in AHK 1.1 is "Ptr".
dd900
Posts: 76
Joined: 27 Oct 2013, 16:03

Re: [Function] WinGetClientPos()

02 Nov 2013, 09:57

Updated function. Thank you.
User avatar
Frosti
Posts: 153
Joined: 27 Oct 2017, 14:30
GitHub: Ixiko

Re: [Function] WinGetClientPos() - now with dpi correction

06 Jul 2018, 11:19

I find this function very useful and that's why I added a dpi correction.

Code: Select all

global dpifactor:=DPIFactor()

Gui New, +HwndhGUI +LabelGui_ +Resize
Gui %hGUI%:Show, w500 h500
MsgBox The Gui client area will now be covered with a new black gui
Gui New, +HwndhGUI2 -Caption +ToolWindow +AlwaysOnTop
Gui %hGUI2%:Color, 000000
Client := WinGetClientPos( hGUI )
Gui %hGUI2%:Show, % "x" Client.X " y" Client.Y " w" Client.W " h" Client.H
OnMessage( 0x03, "GuiChanged" )
OnMessage( 0x05, "GuiChanged" )
return

Gui_Close:
	ExitApp

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" ) // dpifactor
	h := NumGet( size, 12, "Int" ) // dpifactor
	return { X: x, Y: y, W: w, H: h }
}

GuiChanged() {
	global hGUI,hGUI2
	Client := WinGetClientPos( hGUI )
	Gui %hGUI2%:Show, % "x" Client.X " y" Client.Y " w" Client.W " h" Client.H
}

DPIFactor() {																												;-- determines the Windows setting to the current DPI factor

	RegRead, DPI_value, HKEY_CURRENT_USER, Control Panel\Desktop\WindowMetrics, AppliedDPI
	; the reg key was not found - it means default settings
	; 96 is the default font size setting
	if (errorlevel=1) OR (DPI_value=96 )
		return 1
	else
		Return  DPI_Value/96

}
burque505
Posts: 690
Joined: 22 Jan 2017, 19:37

Re: [Function] WinGetClientPos()

06 Jul 2018, 12:57

Thanks for sharing, Frosti, works great here with Win7, 64-bit, scaled to 125%
Regards,
burque505

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot], sttrebo, swagfag and 47 guests