AutoXYWH() - Move control automatically when GUI resized

Post your working scripts, libraries and tools for AHK v1.1 and older
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: AutoXYWH() - Move control automatically when GUI resized

Post by m3user » 12 Jun 2015, 14:54

Thanks. One question: I use a Scintilla wrapper class where hwnd of the control is retrieved as sci.hwnd.

I tried

Code: Select all

AutoXYWH("wh", sc.hwnd)
; or
AutoXYWH("wh", "ahk_id " sc.hwnd)
for resizing it it but it does not work. Can anybody suggest how to do this? Thanks!

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AutoXYWH() - Move control automatically when GUI resized

Post by tmplinshi » 12 Jun 2015, 21:05

@m3user
Try AutoXYWH("wh", "Scintilla1"). If this doesn't work, use AU3_Spy.exe to get the control info.

m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: AutoXYWH() - Move control automatically when GUI resized

Post by m3user » 13 Jun 2015, 06:41

Thanks tmplinshi, the ClassNN is Scintilla1 but unfortunately it doesn't work.

Guest1234

Re: AutoXYWH() - Move control automatically when GUI resized

Post by Guest1234 » 21 Jun 2015, 16:34

Let me start out by saying thank you for this script. It's very nice. I seem to remember having issues with Anchor when I tried it a while back but this works like a dream. One thing that I'd like to point out, though really it's inconsequential, is that the function doesn't work correctly when #Warn is in the script. It complains that "This variable has not been assigned a value. Options (a local variable)". I've worked around this by simply initializing Options at the top of the function. Again, most users will not experience this. But again, thank you so much for this script.

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: AutoXYWH() - Move control automatically when GUI resized

Post by haichen » 06 Jul 2015, 11:29

I would like to have an easy way to add controls into a row without thinking how to add the correct factors for w and x.
In the example there are no height corrections. So this code is only useful for Buttons I think.
Here is my try:

Code: Select all



Gui, +Resize
Gui, Margin , 5, 5
Gui, Add, Button,  w90 h30 vBtn1, Button1
Gui, Add, Button, x+ w90 h30 vBtn2, Button2
Gui, Add, Button, x+  w50 h30 vBtn3, Button3
Gui, Add, Button, x+  w90 h30 vBtn4, Button4
Gui, Add, Button, x+  w90 h30 vBtn5, Button5
Gui, Add, Button, x+  w130 h30 vBtn6, Button6
Gui, Show,  h50 w550, Buttons in a row
return

GuiClose:
ExitApp
return

Guisize:
;for six buttons
Btns := []
Loop, 6
   Btns.Insert("btn" . A_Index)
   
AutoXYWH("r", Btns*)   
;AutoXYWH("r h", Btns*)   
;AutoXYWH("r y", Btns*)   
;AutoXYWH("r", "Btn1","Btn2","Btn3","Btn4","Btn5","Btn6")   
return



; =================================================================================
; 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
;   cList   - variadic list of ControlIDs
;             ControlID can be a control HWND, associated variable name, ClassNN or displayed text.
;             The later (displayed text) is possible but not recommend since not very reliable 
; Examples:
;   AutoXYWH("xy", "Btn1", "Btn2")
;   AutoXYWH("w0.5 h 0.75", hEdit, "displayed text", "vLabel", "Button1")
;   AutoXYWH("*w0.5 h 0.75", hGroupbox1, "GrbChoices")
; ---------------------------------------------------------------------------------
; Version: 2015-5-29 / Added 'reset' option (by tmplinshi)
;          2014-7-03 / toralf
;          2014-1-2  / tmplinshi
; requires AHK version : 1.1.13.01+
; =================================================================================
AutoXYWH(DimSize, cList*){       ; http://ahkscript.org/boards/viewtopic.php?t=1079
  static cInfo := {}
;MsgBox, % "dimsize" DimSize 
  If (DimSize = "reset")
    Return cInfo := {}


; ==========
  IF (DimSize = "r")
  {	
	MI := cList.MaxIndex()
	Row:=[]
	Row.Insert("w" . 1/cList.MaxIndex() )
	Loop, % cList.MaxIndex()-1
		Row.Insert("x" . A_Index/cList.MaxIndex() . " w" . 1/cList.MaxIndex() )	
  }
; ==========	
 
  For i, ctrl in cList {
    ctrlID := A_Gui ":" ctrl
    If ( cInfo[ctrlID].x = "" ){
        GuiControlGet, i, %A_Gui%:Pos, %ctrl%
        MMD := InStr(DimSize, "*") ? "MoveDraw" : "Move"
		; ==========
		DimSize := IsObject(Row) ? Row[i] : DimSize
		; ==========
        fx := fy := fw := fh := 0
        For i, dim in (a := StrSplit(RegExReplace(DimSize, "i)[^xywh]")))
            If !RegExMatch(DimSize, "i)" dim "\s*\K[\d.-]+", f%dim%)
              f%dim% := 1
        cInfo[ctrlID] := { x:ix, fx:fx, y:iy, fy:fy, w:iw, fw:fw, h:ih, fh:fh, gw:A_GuiWidth, gh:A_GuiHeight, a:a , m:MMD}
    }Else If ( cInfo[ctrlID].a.1) {
        dgx := dgw := A_GuiWidth  - cInfo[ctrlID].gw  , dgy := dgh := A_GuiHeight - cInfo[ctrlID].gh
        For i, dim in cInfo[ctrlID]["a"]
            Options .= dim (dg%dim% * cInfo[ctrlID]["f" dim] + cInfo[ctrlID][dim]) A_Space
        GuiControl, % A_Gui ":" cInfo[ctrlID].m , % ctrl, % Options
} } }
May be you can add something like this to AUTXYWH()

Thanks a lot for this everyday used function!

Edit:Correct some very stupid Errors..
Edit:Now this works for h and y

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: AutoXYWH() - Move control automatically when GUI resized

Post by haichen » 08 Jul 2015, 12:28

Buttons and Edits nicely aligned in two rows:
Width and height of the edits are changing with the gui.
The Buttons only changing the width and y-Position.

Code: Select all


Gui, +Resize
Gui, Margin , 5, 5
Gui, Add, Edit,    w90 h30 vEdts1, Edits1
Gui, Add, Edit, x+ w90 h30 vEdts2, Edits2
Gui, Add, Edit, x+ w50 h30 vEdts3, Edits3
Gui, Add, Edit, x+ w90 h30 vEdts4, Edits4
Gui, Add, Edit, x+ w90 h30 vEdts5, Edits5
Gui, Add, Edit, x+ w130 h30 vEdts6, Edits6

Gui, Add, Button, x5 y50 w90 h30 vBtn1, Button1
Gui, Add, Button, x+ y50 w90 h30 vBtn2, Button2
Gui, Add, Button, x+ y50 w50 h30 vBtn3, Button3
Gui, Add, Button, x+ y50 w90 h30 vBtn4, Button4
Gui, Add, Button, x+ y50 w90 h30 vBtn5, Button5
Gui, Add, Button, x+ y50 w130 h30 vBtn6, Button6
Gui, Show,  h150 w550, Edits in a row
return

GuiClose:
ExitApp
return

Guisize:
;for six Edits
edts := []
Btns := []
Loop, 6
   edts.Insert("edts" . A_Index), Btns.Insert("btn" . A_Index)


AutoXYWH("r h", edts*)   
AutoXYWH("r y", Btns*)   

;AutoXYWH("r h", "edts1","edts2","edts3","edts4","edts5","edts6")   
return



; =================================================================================
; 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
;   cList   - variadic list of ControlIDs
;             ControlID can be a control HWND, associated variable name, ClassNN or displayed text.
;             The later (displayed text) is possible but not recommend since not very reliable 
; Examples:
;   AutoXYWH("xy", "Btn1", "Btn2")
;   AutoXYWH("w0.5 h 0.75", hEdit, "displayed text", "vLabel", "Button1")
;   AutoXYWH("*w0.5 h 0.75", hGroupbox1, "GrbChoices")
; ---------------------------------------------------------------------------------
; Version: 2015-5-29 / Added 'reset' option (by tmplinshi)
;          2014-7-03 / toralf
;          2014-1-2  / tmplinshi
; requires AHK version : 1.1.13.01+
; =================================================================================
AutoXYWH(DimSize, cList*){       ; http://ahkscript.org/boards/viewtopic.php?t=1079
  static cInfo := {}
 
  If (DimSize = "reset")
    Return cInfo := {}
  ;==========
  dms:= strReplace(DimSize,"r","")
  ;==========
  For i, ctrl in cList {
    ctrlID := A_Gui ":" ctrl
    If ( cInfo[ctrlID].x = "" ){
        GuiControlGet, i, %A_Gui%:Pos, %ctrl%
        MMD := InStr(DimSize, "*") ? "MoveDraw" : "Move"
       
        ; ==========
        Row := (i=1) ? "w" . 1/cList.MaxIndex() : "x" . (i-1)/cList.MaxIndex() . " w" . 1/cList.MaxIndex() . " "
        IF (InStr(DimSize, "r"))  
         DimSize:=dms . " " . Row . " r"
		; ==========
        

        fx := fy := fw := fh := 0
        For i, dim in (a := StrSplit(RegExReplace(DimSize, "i)[^xywh]")))
            If !RegExMatch(DimSize, "i)" dim "\s*\K[\d.-]+", f%dim%)
              f%dim% := 1
        cInfo[ctrlID] := { x:ix, fx:fx, y:iy, fy:fy, w:iw, fw:fw, h:ih, fh:fh, gw:A_GuiWidth, gh:A_GuiHeight, a:a , m:MMD}
    }Else If ( cInfo[ctrlID].a.1) {
        dgx := dgw := A_GuiWidth  - cInfo[ctrlID].gw  , dgy := dgh := A_GuiHeight - cInfo[ctrlID].gh
        For i, dim in cInfo[ctrlID]["a"]
            Options .= dim (dg%dim% * cInfo[ctrlID]["f" dim] + cInfo[ctrlID][dim]) A_Space
        GuiControl, % A_Gui ":" cInfo[ctrlID].m , % ctrl, % Options
} } }

User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: AutoXYWH() - Move control automatically when GUI resized

Post by BGM » 19 May 2016, 09:55

toralf,

I am having troubles. It works when I include it and run it as a script, but when I compile it, I get errors.

With autoxywh in the first post, I get:

Code: Select all

Call to nonexistent function.
For i, dim in (a := StrSplit(RegExReplace(DimSize, "i)[^xywh]")))
and also with the version you posted on July 1, 2014:

Code: Select all

Error: Call to nonexistent function.
Specifically: ACH := StrSplit(CH, "`n"), ACN := StrSplit(CN, "`n")
It doesn't like the strsplit() part.
I'm running AutoHotkey 1.1.23.03 Unicode on Windows 7 Pro x64

This happens if I just include the library, I don't even have to call the function.

User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: AutoXYWH() - Move control automatically when GUI resized

Post by BGM » 19 May 2016, 20:14

I figured it out with GeekDude's help.

I neglected to update the bin file (or my compilation script did, anyway).

It works!

highend
Posts: 47
Joined: 24 Nov 2014, 16:57

Re: AutoXYWH() - Move control automatically when GUI resized

Post by highend » 24 Jul 2016, 07:06

Thanks a lot for this function!

I have two groupboxes (GB1 + GB2), each containing an edit element.
Resizing the groupboxes works fine and the same goes for the edit controls.
But...
I want the GB2 to move in y direction depending on how GB1 is resized so that they don't overlap.
Both edit controls should resize in their appropriate groupbox as well.

The problem is, that GB2 + e2 get to large for the gui's window when you resize the control too much (in height).

Is there anything I could do to not let that happen?

Sample code:

Code: Select all

#NoEnv
#Persistent
#SingleInstance force

#Include, AutoXYWH.ahk

Gui, +Resize
Gui, Margin, , 20
Gui, Add, GroupBox, x10 y5 w200 h100 Section vGB1, GB1
Gui, Add, Edit, xs+10 ys+21 ve1 w150 h70
Gui, Add, GroupBox, x10 y+m w200 h100 Section vGB2, GB2
Gui, Add, Edit, xs+10 ys+21 ve2 w150 h70
Gui, Show
return

GuiSize:
    If (A_EventInfo = 1) ; The window has been minimized.
        Return
    AutoXYWH("*wh", "GB1")
    AutoXYWH("*ywh", "GB2")
    AutoXYWH("wh", "e1")
    AutoXYWH("ywh", "e2")
return

wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: AutoXYWH() - Move control automatically when GUI resized

Post by wolf_II » 24 Jul 2016, 07:19

Try this:

Code: Select all

GuiSize:
    If (A_EventInfo = 1) ; The window has been minimized.
        Return
    AutoXYWH("*wh0.5", "GB1")
    AutoXYWH("*y0.5wh0.5", "GB2")
    AutoXYWH("wh0.5", "e1")
    AutoXYWH("y0.5wh0.5", "e2")
return
I hope that helps.

highend
Posts: 47
Joined: 24 Nov 2014, 16:57

Re: AutoXYWH() - Move control automatically when GUI resized

Post by highend » 24 Jul 2016, 08:22

@wolf_II

With these commands it works as expected :)

Thanks a lot!

ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: AutoXYWH() - Move control automatically when GUI resized

Post by ahklearner » 24 Sep 2016, 02:45

i am wondering why it is not positioning y of edit boxes, when i have added multiple boxes, it should be like y+10 once height and width are resized.

Code: Select all

#Include, AutoXYWH.ahk

Gui, +Resize
Gui, Add, Edit, ve1 w150 h30
Gui, Add, Edit, ve2 x+10 w150 h30
Gui, Add, Edit, ve3 x10 w150 h30
Gui, Add, Button, vb1 gResize, Resize
Gui, Show
return

Resize:
	GuiControl, Move, e1, h30
	GuiControl, Move, e2, h30
	GuiControl, Move, e3, h30
	AutoXYWH("reset") ; Needs to reset if you changed the Control size manually.
return
 
GuiSize:
	If (A_EventInfo = 1) ; The window has been minimized.
		Return

AutoXYWH("wh", "e1")
AutoXYWH("y", "b1")

AutoXYWH("wh", "e2")
AutoXYWH("y", "b2")

AutoXYWH("wh", "e3")
AutoXYWH("y", "b3")

return
It would be better if it calculates width according to percentage of the gui width changed.
and please guide me how to change height of 3rd edit control according to gui height, but not changing height of 1st and 2nd edit box.

Thanks in advance for your help and support.

wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: AutoXYWH() - Move control automatically when GUI resized

Post by wolf_II » 24 Sep 2016, 03:04

Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
#Include, AutoXYWH.ahk

Gui, +Resize +MinSize
Gui, Add, Edit, ve1 w150 h30
Gui, Add, Edit, ve2 x+10 w150 h30
Gui, Add, Edit, ve3 x10 w150 h30
Gui, Add, Button, vb1, Test
Gui, Show
return

GuiSize:
	If (A_EventInfo = 1) ; The window has been minimized.
		Return

    AutoXYWH("w0.5h0.5", "e1")
    AutoXYWH("x0.5w0.5h0.5", "e2")
    AutoXYWH("y0.5w0.5h0.5", "e3")
    AutoXYWH("*y", "b1")

return
I hope that helps.

ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: AutoXYWH() - Move control automatically when GUI resized

Post by ahklearner » 24 Sep 2016, 03:18

ahklearner wrote:but not changing height of 1st and 2nd edit box.
Wondering :bravo:

ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: AutoXYWH() - Move control automatically when GUI resized

Post by ahklearner » 24 Sep 2016, 03:55

Code: Select all

    AutoXYWH("w0.5", "e1")
    AutoXYWH("x0.5w0.5", "e2")
    AutoXYWH("y+10.5w0.5h0.5", "e3")
    AutoXYWH("*y", "b1")
for height i tried this, but the next control is not Y+10.

a lil explanation would be very helpful.

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AutoXYWH() - Move control automatically when GUI resized

Post by tmplinshi » 24 Sep 2016, 04:59

I don't understand how you want to deal with Y+10.

Lets say...

Gui_Height = 100
Control_Y = 30

With AutoXYWH("y", "Control"), when Gui_Height changes to 200, the Control_Y will be 130. Similarly Gui_Height=300 then Control_Y=230.
But...
With AutoXYWH("y+10", "Control"), when Gui_Height changes to 200, what's the value of Control_Y you expecting? And Gui_Height=300 then Control_Y=?

Percentage changing is the only valid way, isn't it? Not + or -.

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: AutoXYWH() - Move control automatically when GUI resized

Post by haichen » 08 Oct 2016, 07:54

Code: Select all

    }Else If ( cInfo[ctrlID].a.1) {
        dgx := dgw := A_GuiWidth  - cInfo[ctrlID].gw  , dgy := dgh := A_GuiHeight - cInfo[ctrlID].gh
        For i, dim in cInfo[ctrlID]["a"]
            Options .= dim (dg%dim% * cInfo[ctrlID]["f" dim] + cInfo[ctrlID][dim]) A_Space
        GuiControl, % A_Gui ":" cInfo[ctrlID].m , % ctrl, % Options
}
shouldn't there be an Options:="" before the loop?

wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: AutoXYWH() - Move control automatically when GUI resized

Post by wolf_II » 08 Oct 2016, 08:21

There is an Options := "" before the loop. It is done by AHK itself. All local variables inside a function are initialized with an empty string.

see Local

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: AutoXYWH() - Move control automatically when GUI resized

Post by haichen » 08 Oct 2016, 09:39

You're of course right. Only cInfo was static.
Thank you, and sorry.

drawback
Posts: 34
Joined: 11 Aug 2016, 11:31

Re: AutoXYWH() - Move control automatically when GUI resized

Post by drawback » 12 Oct 2016, 14:12

Are there any license restrictions (for using it in a commercial application) for this one?

Post Reply

Return to “Scripts and Functions (v1)”