Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to properly get/set GUI size


  • Please log in to reply
6 replies to this topic
Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
Hi everybody,

I have a resizeable GUI, and I am storing the size in an INI, so that when reloaded, it will use the same dimensions.

The problem is that WinGetPos gets the full window dimensions, while the Gui Show, w%Width%... command ignores window caption and some other extra pixels.

So the result is, that the GUI gets a bit larger with every reload.

For starters, I wish there was a GuiGetPos that is more compatible with the dimensions of the GUI command, but as this is not available, I was wondering how I should I solve this.

Just reducing a given number of pixels, is probably a bad idea as the caption may take different shape in each OS or Windows theme.

Here is an example code:
#SingleInstance Force

Gui Add, Text, w200, Dimension Test
Gui Add, Edit, vMyEdit w200 r4, Tada
Gui Show

Loop 10 {
	Gui +LastFound
	WinGetPos X,Y,W,H		; Get window position and dimensions
	GuiControl ,,MyEdit, %X% %Y% %W% %H%
	Sleep 2000
	Gui Hide
	Gui Show, x%X% y%Y% w%W% h%H%
}

Return
ESC::ExitApp

Sector-Seven - Freeware tools built with AutoHotkey

aaffe
  • Members
  • 1045 posts
  • Last active: Jan 16 2014 01:32 PM
  • Joined: 17 May 2007
Dunno why but you have to scale down h and w, perhaps the frame isnt recognized via heigt and width:

#SingleInstance Force

Gui Add, Text, w200, Dimension Test
Gui Add, Edit, vMyEdit w200 r4, Tada
Gui, +LastFound
Gui Show

Loop 10 {
   Gui +LastFound
   WinGetPos,X,Y,W,H,A      ; Get window position and dimensions
   w-=6
   h-=25
   GuiControl ,,MyEdit, %X% %Y% %W% %H%
   Sleep 2000
   Gui Hide
   Gui Show, x%X% y%Y% w%W% h%H%
}

Return
ESC::ExitApp


VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
Gui, +LastFound

Gui, Show, [color=red]w250 h375[/color], Test

VarSetCapacity( rect, 16, 0 )

DllCall("GetClientRect", uint, MyGuiHWND := WinExist(), uint, &rect )

ClientW := NumGet( rect, 8, "int" )

ClientH := NumGet( rect, 12, "int" )

WinGetPos,,, Wid, Hei

Msgbox Your gui's client area is %ClientW% by %ClientH%`nbut the whole window is actually %Wid% by %Hei%


Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
Thank you VxE, just what I needed.
I think WinGetPos should have some extra boolean parameter so that people can choose to get this result instead of the full window.
Sector-Seven - Freeware tools built with AutoHotkey

Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005

I have just want to ask the same question here, then i find this thread, and 'i think the replies have been answered my problems, thank you all.


Based on VxE's reply, I now use this:
GuiGetSize( ByRef W, ByRef H, GuiID=1 ) {
	Gui %GuiID%:+LastFound
	VarSetCapacity( rect, 16, 0 )
	DllCall("GetClientRect", uint, MyGuiHWND := WinExist(), uint, &rect )
	W := NumGet( rect, 8, "int" )
	H := NumGet( rect, 12, "int" )
}

Sector-Seven - Freeware tools built with AutoHotkey

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
small suggestion:
Gui %GuiID%:+LastFound[color=red]Exist[/color]


Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
Skan, thanks, makes total sense.
Here is my new include.

/* 
;Tester
#SingleInstance Force
Gui Add, Text,w200, Hello
Gui Add, Edit,wp vMyEdit
Gui Show

Loop 3 {
	Sleep 2000
	Gui Hide
	Sleep 500
	GuiGetPos( X,Y,W,H )
	GuiControl ,,MyEdit, x%X% y%Y% w%W% h%H%
	Gui Show, x%X% y%Y% w%W% h%H%	
}

Return
ESC::ExitApp
*/

GuiGetSize( ByRef W, ByRef H, GuiID=1 ) {
	Gui %GuiID%:+LastFoundExist
	IfWinExist
	{
		VarSetCapacity( rect, 16, 0 )
		DllCall("GetClientRect", uint, MyGuiHWND := WinExist(), uint, &rect )
		W := NumGet( rect, 8, "int" )
		H := NumGet( rect, 12, "int" )
	}
}

GuiGetPos( ByRef X, ByRef Y, ByRef W, ByRef H, GuiID=1 ) {
	Gui %GuiID%:+LastFoundExist
	IfWinExist
	{
		WinGetPos X, Y
		VarSetCapacity( rect, 16, 0 )
		DllCall("GetClientRect", uint, MyGuiHWND := WinExist(), uint, &rect )
		W := NumGet( rect, 8, "int" )
		H := NumGet( rect, 12, "int" )
	}
}

Sector-Seven - Freeware tools built with AutoHotkey