easy way to layout group contols in a groupbox

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Spitzi
Posts: 315
Joined: 24 Feb 2022, 03:45

easy way to layout group contols in a groupbox

Post by Spitzi » 09 Mar 2024, 03:10

Hi there.

When creating a gui, Is there an easy way to have groupbox adapt it's size to the controls that are in it?

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: easy way to layout group contols in a groupbox

Post by mikeyww » 09 Mar 2024, 06:05

Hello,

You can get the position of each control, and then use the minimum and maximum values to define your groupbox's dimensions.

XMCQCX
Posts: 258
Joined: 14 Oct 2020, 23:44

Re: easy way to layout group contols in a groupbox

Post by XMCQCX » 09 Mar 2024, 06:41

There is also GetPos method.
Unlike ControlGetPos, this method applies DPI scaling to the returned coordinates (unless the -DPIScale option was used).
https://www.autohotkey.com/docs/v2/lib/GuiControl.htm#GetPos

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: easy way to layout group contols in a groupbox

Post by mikeyww » 09 Mar 2024, 06:51

Sorry, yes, for a GUI, I would use GuiControlGet as noted here.

User avatar
Noitalommi_2
Posts: 329
Joined: 16 Aug 2023, 10:58

Re: easy way to layout group contols in a groupbox

Post by Noitalommi_2 » 09 Mar 2024, 11:07

Hi.

Apart from determining the size for each control individually, you could throw everything into a gui, let the gui determine the size, and then take the size of that.

Code: Select all

#Requires AutoHotkey 2.0

Dummy := Gui("-Caption")
Dummy.AddButton(Pos1 := "", Content1 := "dfghfdgh `n dsgsdgdsfgdsf")
Dummy.AddButton(Pos2 := "yp", Content2 := "dfghdfghdfghdfghgfh")
Dummy.AddButton(Pos3 := "xs", Content3 :="hfgddfghfdhgfdhd")
Dummy.AddText(Pos4 := "yp", Content4 :="hfgddfghhgfdfghdfghdfghdfgfdhgfdhdf")
Dummy.AddButton(Pos5 := "yp", Content5 := "dfghdfgh `n dghfdfgh `n dfsdgdsgsdf")
Dummy.AddButton(Pos6 := "xs", Content6 := "dfghdfgh `n dghfdfgh `n dfsdgdsgsdf `n dghfdfgh `n dfsdgdsgsdf")
Dummy.show()
Dummy.GetPos(,, &W, &H)
MsgBox "this is the size needed"
Dummy.destroy()

MyGui := Gui()
MyGui.AddGroupBox("w" W " h" H+MyGui.MarginY*3, "Box")
MyGui.AddButton("x" MyGui.MarginX*2 " y" MyGui.MarginY*4 " Section", Content1)
MyGui.AddButton(Pos2, Content2)
MyGui.AddButton(Pos3, Content3)
MyGui.AddText(Pos4, Content4)
MyGui.AddButton(Pos5, Content5)
MyGui.AddButton(Pos6, Content6)
MyGui.Show()

Spitzi
Posts: 315
Joined: 24 Feb 2022, 03:45

Re: easy way to layout group contols in a groupbox

Post by Spitzi » 14 Mar 2024, 02:59

Thank you @Noitalommi_2 and @mikeyww. Both approaches work quite nicely

In the end I went with the GetPos-Solution and created a function to calculate the bounding rect around some controls. Maybe it helps out somebody else:

Code: Select all

; determines the bounding rectangle of controls on a gui
; - aGui: the gui where the controls are
; - controlNames: an Array of strings with the names of the controls
; returns
; - by reference brX, brY ... : the returned values of the bounding rect
; - true if everything worked, false if an error occured
ControlBoundingRect(aGui, controlNames, &brX, &brY, &brX2, &brY2, &brW, &brH) {

	; initialize values with first control position
	try ControlGetPos(&brX, &brY, &brW, &brH, aGui[controlNames[1]].Hwnd, aGui.Hwnd)
	catch
		return false
	brX2 := brX + brW
	brY2 := brY + brH

	; go through all controls
	for name in controlNames {
		MsgBox(name)

		try ControlGetPos(&cX, &cY, &cW, &cH, aGui[name].Hwnd, aGui.Hwnd)
		catch
			return false

		brX := cX < brX ? cX : brX
		brY := cY < brY ? cY : brY
		brX2 := cX + cW > brX2 ? cX + cW : brX2
		brY2 := cY + cH > brY2 ? cy + cH : brY2
	}

	brW := brX2 - brX
	brH := brY2 - brY
	return true
}

the Groupbox can then be added like this, for example:

Code: Select all

if ControlBoundingRect(HM_Gui, ["id", "idText", "creator", "creatorText",  "usedBy"], &brX, &brY, &brX2, &brY2, &brW, &brH)
	HM_Gui.Add("GroupBox", "x" brX-HM_Gui.MarginX " y" brY-HM_Gui.MarginY " w" brW+2*HM_Gui.MarginX " h" brH+2*HM_Gui.MarginY, "This is a Group Box")

Post Reply

Return to “Ask for Help (v2)”