Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

GroupBox-Add/wrap around existing controls.

  • Please log in to reply
8 replies to this topic
  • Members
  • 255 posts
  • Last active: Nov 08 2015 03:30 PM
  • Joined: 15 Oct 2007
This Demo includes a simple function that will add a GroupBox and wrap it around existing controls in a GUI. Written for AHK Basic
Gui, +LastFound
Gui, Add, Text, vLabel1, A Label
Gui, Add, Text, x162 yMargin vLabel2, Nother Label
Gui, Add, Edit, Section vMyEdit1 xMargin, This is a Control
Gui, Add, Edit, vMyEdit2 ys x162, This is a Control
Gui, Add, CheckBox, Section vCheck1 xMargin, CheckBox 1
Gui, Add, CheckBox, vCheck2 ys x160, CheckBox 2
GroupBox("GB1", "Testing", GBTHeight, 10, "Label1|Label2|MyEdit1|MyEdit2|Check1|Check2")
Gui, Add, Text, Section xMargin, This is un-named
Gui, Add, DropDownList, xMargin vDDL, LIne 1|Line 2| Line 3
GroupBox("GB2", "Another Test", GBTHeight, 10, "This is un-named|DDL")
Gui, Add, Text, yS, This is a control
Gui, Add, DateTime, vMyDateTime w127
GroupBox("GB3", "Test", GBTHeight, 10, "Static4|MyDateTime")
Gui, Add, Text, vMyText xMargin, Some text to read.
Gui, Add, Button, Section x60, Button 1
Gui, Add, Button, ys x+10, Button 2
GroupBox("GB4", "Buttons Too", GBTHeight, 10, "Button 1|Button 2")
GroupBox("GB5", "Around Another GroupBox", GBTHeight, 10, "MyText|Button 1|Button 2|GB4", 298)
Gui, Show, , GroupBox Test



;************************** 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
	Local maxX, maxY, minX, minY, xPos, yPos ;all else assumed Global
	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
		if(GBY<minY) ;Check for minimum Y
		if(GBX+GBW>maxX) ;Check for maximum X
		if(GBY+GBH>maxY) ;Check for maximum Y

		;Move the control to make room for the GroupBox
		yPos:=GBY+TitleHeight+Margin ;fixed margin
		GuiControl, Move, %A_LoopField%, x%xPos% y%yPos%
	;re-purpose the GBW and GBH variables
		GBW:=maxX-minX+2*Margin ;calculate width for GroupBox
		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%

This made GroupBoxing a little bit easier for me.

Edit: Fixed y Margin 2/08/2012


  • Members
  • 475 posts
  • Last active: Oct 21 2014 11:08 AM
  • Joined: 31 Jul 2008
Nice. :)

  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010
Very nice piece of code! :D Thank you.

  • Members
  • 255 posts
  • Last active: Nov 08 2015 03:30 PM
  • Joined: 15 Oct 2007
You're welcome. Hope it is/has been useful.


  • Members
  • 265 posts
  • Last active: Jan 04 2016 02:24 AM
  • Joined: 19 Nov 2010
Thanks for the great function dmatch, I always grouped my controls manually, never came across your thread.
Just one questions, the Margin option seems to work on left, right and bottom, not the top part, is it fixable?

It works fine on Ahk_l
Here is a shortened version of the same function:
GroupBox(GBvName,Title,TitleHeight,Margin,Piped_CtrlvNames,FixedWidth="",FixedHeight="") {
	Local maxX, maxY, minX, minY, xPos, yPos
	minX:=99999, minY:=99999, maxX:=0, maxY:=0
	Loop, Parse, Piped_CtrlvNames, |, %A_Space%
		GuiControlGet, GB, Pos, %A_LoopField%
		minX:= GBX<minX ? GBX : minX
		minY:= GBY<minY ? GBY : minY
		maxX:= GBX+GBW>maxX ? GBX+GBW : maxX
		maxY:= GBY+GBH>maxY ? GBY+GBH : maxY
		xPos:= GBX+Margin
		yPos:= GBY+TitleHeight
		GuiControl, Move, %A_LoopField%, x%xPos% y%yPos%
	GBW:= FixedWidth ? FixedWidth : maxX-minX+2*Margin
	GBH:= FixedHeight ? FixedHeight : maxY-MinY+TitleHeight+Margin
	Gui, Add, GroupBox, v%GBvName% x%minX% y%minY% w%GBW% h%GBH%, %Title%

emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
mwahahahaha, that "Code (Expand):" thing is gone.
fixed margin:
GroupBox(GBvName,Title,TitleH,Margin,Piped_CtrlvNames,FixedWidth="",FixedHeight="") {
   Local maxX:=maxY:=0,minX:=minY:=99999,xPos, yPos                         ;Force some  vars to start with a default value
   Loop, Parse, Piped_CtrlvNames, |, %A_Space%                              ;Loop the list of Controls
      { GuiControlGet, GB, Pos, %A_LoopField%                               ;Get position and size of each control in list. 
      GuiControl,Move,%A_LoopField%,% "x" Margin+GBX " y" TitleH+Margin+GBY ;Make space for the GroupBox
      minX := GBX<minX ? GBX : minX,  maxX := GBX+GBW>maxX ? GBX+GBW : maxX ;check for minimum, and maximum X
      minY := GBY<minY ? GBY : minY,  maxY := GBY+GBH>maxY ? GBY+GBH : maxY ;check for minimum, and maximum Y
   } GBW:= FixedWidth  ? FixedWidth  : Margin+maxX-minX+Margin              ;calculate width for GroupBox
   GBH:= FixedHeight ? FixedHeight : Margin+TitleH+maxY-MinY+Margin         ;calculate height for GroupBox 
   Gui, Add, GroupBox, v%GBvName% x%minX% y%minY% w%GBW% h%GBH%, %Title%    ;Add the GroupBox 

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum

  • Members
  • 255 posts
  • Last active: Nov 08 2015 03:30 PM
  • Joined: 15 Oct 2007
Fixed y Margin in original posted code. I had been using extra Title Height instead of applying proper Margin at the top.

Changed GBTHeight to 10 instead of 22 in demo.




  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
Nice and useful, thanks for sharing,

  • Members
  • 212 posts
  • Last active: Jan 05 2016 08:08 PM
  • Joined: 22 Jun 2012

removed. Why there's no delete option?

Edited by zcooler, 11 October 2014 - 02:57 PM.