GroupBox-Add/wrap around existing controls

Post your working scripts, libraries and tools for AHK v1.1 and older
digimystigi
Posts: 4
Joined: 31 Oct 2017, 16:21
Contact:

GroupBox-Add/wrap around existing controls

31 Oct 2017, 16:32

So, this is a direct continuation from an archived post:
https://autohotkey.com/board/topic/7106 ... -controls/

Member "dmatch" created a function that creates a GroupBox that automatically wraps around "child" elements (specified in the function call) and shifts those elements to be inside the GroupBox.

As I started to play with the code, I found that I didn't want to have to specify grandchild elements when adding an existing GroupBox to the new one. So I updated the code to parse the list and automatically add the child elements of any GroupBoxes included in the child elements of the new GroupBox.

Code: Select all

;************************** GroupBox *******************************
;
;	Adds and wraps a GroupBox around a group of controls in
;	the default Gui. Use the Gui Default command if needed.
;	For instance:
;
;		Gui, 2:Default
;
;	sets the default Gui to Gui 2.
;
;	Add the controls you want in the GroupBox to the Gui using
;	the "v" option to assign a variable name to each control. *
;	Then immediately after the last control for the group
;	is added call this function. It will add a GroupBox and
;	wrap it around the controls.
;
;	Example:
;
;	Gui, Add, Text, vControl1, This is Control 1
;	Gui, Add, Text, vControl2 x+30, This is Control 2
;	GroupBox("GB1", "Testing", 20, 10, "Control1|Control2")
;	Gui, Add, Text, Section xMargin, This is Control 3
;	GroupBox("GB2", "Another Test", 20, 10, "This is Control 3")
;	Gui, Add, Text, yS, This is Control 4
;	GroupBox("GB3", "Third Test", 20, 10, "Static4")
;	Gui, Show, , GroupBox Test
;
;	* The "v" option to assign Control ID is not mandatory. You
;	may also use the ClassNN name or text of the control.
;
;	Author: dmatch @ AHK forum
;	Date: Sept. 5, 2011
;
;********************************************************************

GroupBox(GBvName			;Name for GroupBox control variable
		,Title				;Title for GroupBox
		,TitleHeight		;Height in pixels to allow for the Title
		,Margin				;Margin in pixels around the controls
		,Piped_CtrlvNames	;Pipe (|) delimited list of Controls
		,FixedWidth=""		;Optional fixed width
		,FixedHeight=""		;Optional fixed height
		,Kin=true)		; Added this option to disable the new functionality.  Default is to auto-include grandchildren.
{
	Local maxX, maxY, minX, minY, xPos, yPos ;all else assumed Global
	minX:=99999
	minY:=99999
	maxX:=0
	maxY:=0

;Start of new code
	static GBArray := Object()
	GBArray.Insert(GBvName)
	GBArray%GBvName% := Piped_CtrlvNames

	If Kin
	{
		PCNInclude =
		Loop, Parse, Piped_CtrlvNames, |, %A_Space%
		{
			i := A_LoopField
			For index, element in GBArray
			{
				If i = %element%
				{
					;MsgBox % "Found " i "`nElements include: " GBArray%i%
					PCNInclude := PCNInclude "|" GBArray%i%
				}
			}
		}
	}

	Piped_CtrlvNames := Piped_CtrlvNames PCNInclude
;end of new code

	Loop, Parse, Piped_CtrlvNames, |, %A_Space%
	{
		;Get position and size of each control in list.
		GuiControlGet, GB, Pos, %A_LoopField%
		;creates GBX, GBY, GBW, GBH
		if(GBX<minX) ;check for minimum X
			minX:=GBX
		if(GBY<minY) ;Check for minimum Y
			minY:=GBY
		if(GBX+GBW>maxX) ;Check for maximum X
			maxX:=GBX+GBW
		if(GBY+GBH>maxY) ;Check for maximum Y
			maxY:=GBY+GBH
		;MsgBox % A_LoopField

		;Move the control to make room for the GroupBox
		xPos:=GBX+Margin
		yPos:=GBY+TitleHeight+Margin ;fixed margin
		GuiControl, Move, %A_LoopField%, x%xPos% y%yPos%
	}
	;re-purpose the GBW and GBH variables
	if(FixedWidth)
		GBW:=FixedWidth
	else
		GBW:=maxX-minX+2*Margin ;calculate width for GroupBox
	if(FixedHeight)
		GBH:=FixedHeight
	else
		GBH:=maxY-MinY+TitleHeight+2*Margin ;calculate height for GroupBox ;fixed 2*margin

	;Add the GroupBox
	Gui, Add, GroupBox, v%GBvName% x%minX% y%minY% w%GBW% h%GBH%, %Title%
	return
}
I hope this helps anyone that is looking to use GroupBoxes in their GUI layouts.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: GroupBox-Add/wrap around existing controls

31 Oct 2017, 19:30

Thanks, digimystigi. I especially like the static array code and Piped_CtrlvNames. That should come in handy.
Regards,
burque505
digimystigi
Posts: 4
Joined: 31 Oct 2017, 16:21
Contact:

Re: GroupBox-Add/wrap around existing controls

13 Nov 2017, 12:35

I thought it rather amusing, personally, that I was able to use a hack of AutoHotKey in variable handling; I can use "GBArray" for both an array and a pseudo-array because the first is the Object and the second is just "part of a variable name".

As said, though, I honestly cannot take credit for this function; dmatch gets the credit here. I'm just an amateur coder and never would have been able to come up with this. I was just tired of manually creating, adjusting and fine-tuning the GroupBoxes after *every single GUI change!* and found this on the archived board. In playing with it, I just thought it would be more "efficient" [for the GUI design] if I didn't have to specify the grandchildren in the new GroupBox definition.

Anyone have any thoughts on the code? Any improvements you can think of?
https://github.com/digimystigi/AHK_GroupBox/ if anyone is interested. I've already posted some updates to the code.
User avatar
tidbit
Posts: 1273
Joined: 29 Sep 2013, 17:15
Location: USA

Re: GroupBox-Add/wrap around existing controls

13 Nov 2017, 18:06

I did this too, forever ago. The download is dead but djsz posted a fix toward the bottom:
https://autohotkey.com/board/topic/89155-groupbox2/

I still use it fairly often (a shortened version. all the comments+spacing was to help someone understand it).
rawr. fear me.
*poke*
Is it December 21, 2012 yet?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 109 guests