AutoXYWH() - Move control automatically when GUI resized

Post your working scripts, libraries and tools for AHK v1.1 and older
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

01 Jul 2014, 17:02

Could someone with experience run a speed comparison against anchor() please? I have no real exp.
ciao
toralf
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

03 Jul 2014, 07:35

And another version.
The previous seemed to be overkill to redraw every control in Class 'Button' and storing too much unneeded data in the cInfo object.
Thus, for this version you will have to specify a * in DimSize to MoveDraw the controls rather then Move, which is recommended for groupboxes.

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
;   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")
; ---------------------------------------------------------------------------------
; Release date: 2014-7-03          
; Author      : tmplinshi (mod by toralf)
; requires AHK version : 1.1.13.01+
; =================================================================================
AutoXYWH(DimSize, cList*){       ; http://ahkscript.org/boards/viewtopic.php?t=1079
  static cInfo := {}
  For i, ctrl in cList {
    ctrlID := A_Gui ":" ctrl
    If ( cInfo[ctrlID].x = "" ){
        GuiControlGet, i, %A_Gui%:Pos, %ctrl%
        GuiControlGet, Hwnd, %A_Gui%:Hwnd, %ctrl%
        MMD := InStr(DimSize, "*") ? "MoveDraw" : "Move"
        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
} } }
ciao
toralf
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

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

04 Feb 2015, 22:29

Yes, this is much neater than Anchor, thanks!

Nice to see it also works with HWNDS ;)

Code: Select all

#SingleInstance force

Gui, +Resize
Gui, Add, Edit  ,    w200    hwndhEdit
Gui, Add, Button, ys w40  hp vBtn1, 1
Gui, Add, Button, ys wp  hp hwndHwndBtn2, 2
Gui, Show
Return

GuiSize:
    AutoXYWH(hEdit, "wh")
    AutoXYWH("Btn1|" HwndBtn2, "hx")
Return

GuiClose:
ExitApp
Though personally I would make ctrl_list an indexed array - pipe delimited lists is a bit old-school surely?

Also, I used this to *almost* solve the issue of AHK not supporting scrollbars for GUIs.
The idea is to hijack a listview and insert images into the listview of a known height.
You then inject your controls, guis etc into the LV, and set the rows of the LV to the height of your GUI, and it handles the scrollbars for you.
You then use AutoXYWH to make the LV always fill the GUI.
Only problem is, the contents corrupt when the LV scrolls. If anyone can think of any solutions, please see my thread here: http://ahkscript.org/boards/viewtopic.p ... 205#p37205
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

05 Feb 2015, 04:56

Wouldn't your post better be placed in the ask for help section instead of highjacking this thread?
ciao
toralf
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

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

07 Feb 2015, 07:51

Hi, great function! I have one question:

When some gui control (for example Edit control) in a window gets resized, the function does not work properly (doesn't consider the changed size of control). The problem was the same with Anchor. Please try the example below - press the button and then resize.
Any solution to this? How could this be solved? Thanks!

Code: Select all

#include AutoXYWH.ahk

Gui, +Resize
Gui, Add, Edit, ve1 w150 h100
Gui, Add, Button, vb1 gResize, Resize
Gui, Show		
return

Resize:
GuiControl, Move, e1, h50
return

GuiSize:
AutoXYWH("wh", "e1")		
AutoXYWH("y", "b1")		
return
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

07 Feb 2015, 11:07

To m3user.
What you want is not supported by this function. You would need to change the function by e.g. adding a new parameter to reset the original size.
ciao
toralf
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

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

07 Feb 2015, 13:34

Thanks Toralf, I have no idea how to do this. Any help is appreciated. :-)
think
Posts: 136
Joined: 09 Feb 2014, 05:20

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

22 Feb 2015, 04:37

Thanks for this function, it finally sends Anchor to retirement. However I agree, it needs a reset option - would it be difficult to add?
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

22 Feb 2015, 08:26

In general it should be possible. Somehow the function needs to know when that will be. Since the second parameter is an array the info has to be in the first parameter. Which would also make sense since it would be a reset of the dimensions.
What would be your suggestion on how to provide the info in the first parameter? E.g. an 'r' or a '*'?

But before something gets developed that is not needed please help to define the details:
When would a reset be required in addition to the above case? Why would it need to be reset? And what needs to be reset?
ciao
toralf
think
Posts: 136
Joined: 09 Feb 2014, 05:20

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

23 Feb 2015, 09:13

The example posted above shows the issue clearly.

Reset could be called after any re-arrangement of the controls in gui window (typically by GuiControl, Move) by calling AutoXYWH("r", "e1") or similar after a resize or by simply calling AutoXYWH().
Guest

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

01 Mar 2015, 22:22

Nice job!
Is it possible add/improve the following functions?
1. Set minimum GUI size to prevent controls squeezing together.
2. Set positions of multiple controls. for example: AutoXYWH("xy", "Button1-30" , "Edit1-30")
3. Eliminate blinking during resizing.

Thanks!
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

01 Mar 2015, 22:48

1) can be done via Gui, MinSize
2) I do not understand. Please elaborate
3) not possible
ciao
toralf
Guest

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

02 Mar 2015, 08:45

Thank you toralf,
toralf wrote:2) I do not understand. Please elaborate
Imagine that I have a GUI has 30 buttons and 30 edit boxes.

Currently I'm using code like this:

Code: Select all

AutoXYWH("xy", "Button1" , "Button2","Button3" , "Button4".......)
AutoXYWH("xy", "Edit1" , "Edit2","Edit3" , "Edit4".......)
It will be nice it we can use the short code below:

Code: Select all

AutoXYWH("xy", "Button1-30" , "Edit1-30")
just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

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

02 Mar 2015, 09:04

Guest wrote:Imagine that I have a GUI has 30 buttons and 30 edit boxes.

Code: Select all

#NoEnv
Controls := []
Loop, 30
   Controls.Insert("Button" . A_Index)
Loop, 30
   Controls.Insert("Edit" . A_Index)
VariadicFunc(Controls*)
ExitApp

VariadicFunc(Params*) {
   For Each, Param In Params
      MsgBox, 0, Param %A_Index%, %Param%
}
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

02 Mar 2015, 14:08

Thanks just me.
AutoXYWH() is already a variadic function. Thus what just me wrote could directly be used with it.
ciao
toralf
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

02 Mar 2015, 14:11

Code: Select all

Controls := []
Loop, 30
   Controls.Insert("Button" . A_Index), Controls.Insert("Edit" . A_Index)
AutoXYWH("xy", Controls*)
ciao
toralf
danielo515
Posts: 58
Joined: 18 Feb 2015, 15:06

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

26 Apr 2015, 06:55

Does this function have problems with Named GUIs?

Something like
gui,mainGui:new

After I named the gui that way it stopped working.
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

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

26 Apr 2015, 07:22

I think it should not, but could you please post a short sample code that shows the problem?
ciao
toralf

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 144 guests