AutoXYWH() gui resize converted to v2

Post your working scripts, libraries and tools.
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

AutoXYWH() gui resize converted to v2

27 Feb 2023, 08:14

I have been learning v2 by converting some of the functions I used frequently in v1. Sharing.

Code: Select all

; =================================================================================
; Function: AutoXYWH
;   Move and resize control automatically when GUI resizes.
; Parameters:
;   DimSize - Can be one or more of x/y/w/h  optional followed by a fraction
;             add a '*' to DimSize to 'MoveDraw' the controls rather then just 'Move', this is recommended for Groupboxes
;             add a 't' to DimSize to tell AutoXYWH that the controls in cList are on/in a tab3 control
;   cList   - variadic list of GuiControl objects
;
; Examples:
;   AutoXYWH("xy", "Btn1", "Btn2")
;   AutoXYWH("w0.5 h 0.75", hEdit, "displayed text", "vLabel", "Button1")
;   AutoXYWH("*w0.5 h 0.75", hGroupbox1, "GrbChoices")
;   AutoXYWH("t x h0.5", "Btn1")
; ---------------------------------------------------------------------------------
; Version: 2023-02-25 / converted to v2 (Relayer)
;          2020-5-20 / small code improvements (toralf)
;          2018-1-31 / added a line to prevent warnings (pramach)
;          2018-1-13 / added t option for controls on Tab3 (Alguimist)
;          2015-5-29 / added 'reset' option (tmplinshi)
;          2014-7-03 / mod by toralf
;          2014-1-02 / initial version tmplinshi
; requires AHK version : v2+
; =================================================================================

AutoXYWH(DimSize, cList*)   ;https://www.autohotkey.com/boards/viewtopic.php?t=1079
{
	static cInfo := map()

	if (DimSize = "reset")
		Return cInfo := map()

	for i, ctrl in cList
	{
		ctrlObj := ctrl
		ctrlObj.Gui.GetPos(&gx, &gy, &gw, &gh)
		if !cInfo.Has(ctrlObj)
		{
			ctrlObj.GetPos(&ix, &iy, &iw, &ih)
			MMD := InStr(DimSize, "*") ? "MoveDraw" : "Move"
			f := map( "x", 0
					, "y", 0
					, "w", 0
					, "h", 0 )

			for i, dim in (a := StrSplit(RegExReplace(DimSize, "i)[^xywh]"))) 
				if !RegExMatch(DimSize, "i)" . dim . "\s*\K[\d.-]+", &tmp)
					f[dim] := 1
				else
					f[dim] := tmp

			if (InStr(DimSize, "t"))
			{
				hWnd := ctrlObj.Hwnd
				hParentWnd := DllCall("GetParent", "Ptr", hWnd, "Ptr")
				RECT := buffer(16, 0)
				DllCall("GetWindowRect", "Ptr", hParentWnd, "Ptr", RECT)
				DllCall("MapWindowPoints", "Ptr", 0, "Ptr", DllCall("GetParent", "Ptr", hParentWnd, "Ptr"), "Ptr", RECT, "UInt", 1)
				ix := ix - NumGet(RECT, 0, "Int")
				iy := iy - NumGet(RECT, 4, "Int")
			}
			cInfo[ctrlObj] := {x:ix, fx:f["x"], y:iy, fy:f["y"], w:iw, fw:f["w"], h:ih, fh:f["h"], gw:gw, gh:gh, a:a, m:MMD}
		}
		else
		{
			dg := map( "x", 0
					 , "y", 0
					 , "w", 0
					 , "h", 0 )
			dg["x"] := dg["w"] := gw - cInfo[ctrlObj].gw, dg["y"] := dg["h"] := gh - cInfo[ctrlObj].gh
			ctrlObj.Move(  dg["x"] * cInfo[ctrlObj].fx + cInfo[ctrlObj].x
						 , dg["y"] * cInfo[ctrlObj].fy + cInfo[ctrlObj].y
						 , dg["w"] * cInfo[ctrlObj].fw + cInfo[ctrlObj].w
						 , dg["h"] * cInfo[ctrlObj].fh + cInfo[ctrlObj].h  )
			if (cInfo[ctrlObj].m = "MoveDraw")
				ctrlObj.Redraw()
		}
	}
}
autoexec
Posts: 24
Joined: 20 Feb 2023, 22:38

Re: AutoXYWH() gui resize converted to v2

29 Apr 2024, 22:33

I modified the code with a few improvements.

Code: Select all

; original by tmplinshi, modified by toralf, Alguimist: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=1079
; converted to v2 by Relayer: https://www.autohotkey.com/boards/viewtopic.php?f=83&t=114445

AutoXYWH(DimSize, cList*)
{
	Static cInfo := Map()
	If DimSize = 'reset'
		Return cInfo := Map()
	For Ctrl in cList
	{
		Ctrl.Gui.GetPos(,, &gw, &gh)
		If !cInfo.Has(Ctrl)
		{
			Ctrl.GetPos(&x, &y, &w, &h)
			fx := fy := fw := fh := 0
			For dim in StrSplit(RegExReplace(DimSize, 'i)[^xywh]'))
				f%dim% := RegExMatch(DimSize, 'i)' dim '\s*\K[\d.-]+', &m) ? m[] : 1
			If InStr(DimSize, 't')
			{
				Hwnd := DllCall('GetParent', 'Ptr', Ctrl.Hwnd, 'Ptr')
				DllCall('GetWindowRect', 'Ptr', Hwnd, 'Ptr', RECT := Buffer(16, 0))
				DllCall('MapWindowPoints', 'Ptr', 0, 'Ptr', DllCall('GetParent', 'Ptr', Hwnd, 'Ptr'), 'Ptr', RECT, 'UInt', 2)
				x -= NumGet(RECT, 'Int') * 96 // A_ScreenDPI
				y -= NumGet(RECT, 4, 'Int') * 96 // A_ScreenDPI
			}
			cInfo[Ctrl] := Map('x', x, 'fx', fx, 'y', y, 'fy', fy, 'w', w, 'fw', fw, 'h', h, 'fh', fh, 'gw', gw, 'gh', gh, 'm', !!InStr(DimSize, '*'))
		}
		Else
		{
			dgw := gw - cInfo[Ctrl]['gw'], dgh := gh - cInfo[Ctrl]['gh']
			Ctrl.Move(cInfo[Ctrl]['fx'] ? dgw * cInfo[Ctrl]['fx'] + cInfo[Ctrl]['x'] : unset
				, cInfo[Ctrl]['fy'] ? dgh * cInfo[Ctrl]['fy'] + cInfo[Ctrl]['y'] : unset
				, cInfo[Ctrl]['fw'] ? dgw * cInfo[Ctrl]['fw'] + cInfo[Ctrl]['w'] : unset
				, cInfo[Ctrl]['fh'] ? dgh * cInfo[Ctrl]['fh'] + cInfo[Ctrl]['h'] : unset)
			If cInfo[Ctrl]['m']
				Ctrl.Redraw()
		}
	}
}

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: songdg and 25 guests