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?
easy way to layout group contols in a groupbox
Re: easy way to layout group contols in a groupbox
Hello,
You can get the position of each control, and then use the minimum and maximum values to define your groupbox's dimensions.
You can get the position of each control, and then use the minimum and maximum values to define your groupbox's dimensions.
Re: easy way to layout group contols in a groupbox
There is also GetPos method.
https://www.autohotkey.com/docs/v2/lib/GuiControl.htm#GetPosUnlike ControlGetPos, this method applies DPI scaling to the returned coordinates (unless the -DPIScale option was used).
Re: easy way to layout group contols in a groupbox
Sorry, yes, for a GUI, I would use GuiControlGet as noted here.
- Noitalommi_2
- Posts: 329
- Joined: 16 Aug 2023, 10:58
Re: easy way to layout group contols in a groupbox
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.
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()
Re: easy way to layout group contols in a groupbox
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:
the Groupbox can then be added like this, for example:
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
}
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")