Attach(hCtrl = "",
aDef = "")

Determines how a control is resized with its parent.


  • hWnd of the control if aDef is not empty.
  • hWnd of the parent to be reset if aDef is empty.  If you are using the function with only one parent, you don’t have to specify its handle, its enough to call Attach without any parameters.  With multiple parents you need to specify which one you want to reset.
  • Handler name, if parameter is string and aDef is empty.  Handler will be called after the function has finished moving controls for the parent.  Handler receives hWnd of the parent as its only argument.


Attach definition string.  Space separated list of attach options.  If omitted, function working depends on hCtrl parameter.  You can use following elements in the definition string:

  • ”x,y,w,h” letters along with coefficients, decimal numbers which can also be specified in m/n form (see example below).
  • ”r”.  Use “r1” (or “r”) option to redraw control immediately after repositioning, set “r2” to delay redrawing 100ms for the control (prevents redrawing spam).
  • ”p” (for “proportional”) is the special coefficient.  It will make control’s dimension always stay in the same proportion to its parent (so, pin the control to the parent).  Although you can mix pinned and non-pinned controls and dimensions that is rarely what you want.  You will generally want to pin every control in the parent.
  • ”+” or “-” enable or disable function for the control.  If control is hidden, you may want to disable the function for performance reasons, especially if control is container attaching its children.  Its perfectly OK to leave invisible controls attached, but if you have lots of them you can use this feature to get faster and more responsive updates.  When you want to show disabled hidden control, make sure you first attach it back so it can take its correct position and size while in hidden state, then show it.  “+” must be used alone while “-” can be used either alone or in Attach definition string to set up control as initially disabled.


Function monitors WM_SIZE message to detect parent changes.  That means that it can be used with other eventual container controls and not only top level windows.

You should reset the function when you programmatically change the position of the controls in the parent control.  Depending on how you created your GUI, you might need to put “autosize” when showing it, otherwise resetting the Gui before its placement is changed will not work as intented.  Autosize will make sure that WM_SIZE handler fires.  Sometimes, however, WM_SIZE message isn’t sent to the window.  One example is for instance when some control requires Gui size to be set in advance in which case you would first have “Gui, Show, w100 h100 Hide” line prior to adding controls, and only Gui, Show after controls are added.  This case will not trigger WM_SIZE message unless AutoSize is added.


Attach(h, "w.5 h1/3 r2")    ;Attach control's w, h and redraw it with delay.
Attach(h, "-")              ;Disable function for control h but keep its definition. To enable it latter use "+".
Attach(h, "- w.5")          ;Make attach definition for control but do not attach it until you call Attach(h, "+").
Attach()                    ;Reset first parent. Use when you have only 1 parent.
Attach(hGui2)               ;Reset Gui2.
Attach("Win_Redraw")        ;Use Win_Redraw function as a Handler. Attach will call it with parent's handle as argument.
Attach(h, "p r2")           ;Pin control with delayed refreshing.

; This is how to do delayed refresh of entire window.
; To prevent redraw spam which can be annoying in some cases,
; you can choose to redraw entire window only when user has finished resizing it.
; This is similar to r2 option for controls, except it works with entire parent.

Attach("OnAttach")          ;Set Handler to OnAttach function

OnAttach( Hwnd ) {
    global hGuiToRedraw := hwnd
    SetTimer, Redraw, -100


Working sample:

#SingleInstance, force
    Gui, +Resize
    Gui, Add, Edit, HWNDhe1 w150 h100
    Gui, Add, Picture, HWNDhe2 w100 x+5 h100, pic.bmp

    Gui, Add, Edit, HWNDhe3 w100 xm h100
    Gui, Add, Edit, HWNDhe4 w100 x+5 h100
    Gui, Add, Edit, HWNDhe5 w100 yp x+5 h100

    gosub SetAttach                 ;comment this line to disable Attach
    Gui, Show, autosize

    Attach(he1, "w.5 h")
    Attach(he2, "x.5 w.5 h r")
    Attach(he3, "y w1/3")
    Attach(he4, "y x1/3 w1/3")
    Attach(he5, "y x2/3 w1/3")