Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Control Anchoring v4 for resizing windows


  • Please log in to reply
78 replies to this topic
polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
This function defines how controls should be automatically positioned relatively to the new dimensions of a GUI when resized (see example).

Download

Atomhrt
  • Members
  • 124 posts
  • Last active: Nov 13 2006 09:18 PM
  • Joined: 02 Sep 2004

* Just updated, it works much better now :)


I use it and another function almost just like it to resize one of my GUI's. It has 29 controls (that are run through the functions) including one listview and four tabs. Works great! :D

One thing that I did because I'm so lazy, is changed the order of the params to the function...
From:
Anchor(Control, Anchors="", x=0, y=0, w=0, h=0)
To:
Anchor(x=0, y=0, w=0, h=0, Control, Anchors="")

This made it a bit easier to create the call for most of the controls since it somewhat follows the format of most of my GUI, Add statements. That is, the x-y-h-w coords first usually followed by a variable name. Using a few find and replace statements with my editor could then do most of the work. Hope that makes sense... :roll:
I am he of whom he speaks!

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

One thing that I did because I'm so lazy, is changed the order or params to the function...

The open source is there so you can customize it to your needs :wink:. Glad you liked it anyway.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Great script. It should probably be one of the things put in the forthcoming library of standard include files.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
I updated the function again. This time it's even easier to use.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Terrapin
  • Members
  • 107 posts
  • Last active: Feb 06 2007 03:38 PM
  • Joined: 15 Aug 2005
Thank You, Titan.

Bob
When it comes to Binary, there are 10 types of people. Those who get it and those who don't. :)

AGU
  • Guests
  • Last active:
  • Joined: --

It should probably be one of the things put in the forthcoming library of standard include files.

Does this library already exist?
___________________
Cheers
AGU

AGU
  • Guests
  • Last active:
  • Joined: --
It's me again. As I'm using an older version of this function in BBCodeWriter I thought about updating to your new one. But I don't get the point of how to use it. Could you give me a short example?

I meanwhile took your old function and replaced GuiControl, Move with GuiControl, MoveDraw to workaround a displaying error when resizing the GUI in x-direction.

This is your older one:
Anchor(Control, Anchors="", x=0, y=0, w=0, h=0)
  {  
    ; Thanks to Titan for this function
    ; Details under http://www.autohotkey.com/forum/viewtopic.php?p=26778
    
    ; Control         : Variable assigned to the control (e.g. "MyEdit")
    ; Anchors         : How control to be moved (e.g. "xy" for position or "wh" for size)
    ; x, y, w, h      : respective values of control 
    ;                 (note you can omit values that aren't needed by the Anchors by putting any value)
    
    global GuiW, GuiH
    
    If (not Anchors)
      Anchors = xy
    If Anchors contains x
      GuiControl, MoveDraw, % Control, % "x" x+(A_GuiWidth-GuiW)
    If Anchors contains y
      GuiControl, MoveDraw, % Control, % "y" y+(A_GuiHeight-GuiH)
    If Anchors contains w
      GuiControl, MoveDraw, % Control, % "w" w+(A_GuiWidth-GuiW)
    If Anchors contains h
      GuiControl, MoveDraw, % Control, % "h" h+(A_GuiHeight-GuiH)
  }

I call it this way:
...
      Anchor("EdtComment", "wh", 0, 0, EdtW, EdtH)
      Anchor("GrpMenuBorder", "w", 0, 0, GrpW, 0)
      Anchor("ChkSig", "y", 0, ChkY, 0, 0)
      Anchor("DDLSig", "y", 0, DDLY, 0, 0)
      Anchor("BtnEditSig", "y", 0, BSgY, 0, 0)
      Anchor("BtnDelSig", "y", 0, BSgY, 0, 0)
      Anchor("BtnSend", "xy", BG1X, BG3Y, 0, 0)
      Anchor("BtnPreview", "xy", BG2X, BG3Y, 0, 0)
      Anchor("BtnReset", "xy", BG3X, BG3Y, 0, 0)
      Anchor("BtnPinned", "x", BG4X, 0, 0, 0)
      ...

How would I have to call your updated function? I tried to remove all anchors to make the syntax compatible to your new function but it didn't work.
Anchor("EdtComment", 0, 0, EdtW, EdtH)
_______________
Cheers
AGU

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Replace the 0's with a blank value (i.e. ""). If you want to force a MoveDraw, specify an extra parameter of 1. Using your code the correct version would be: Anchor("EdtComment", "", "", EdtW, EdtH, 1).

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


AGU
  • Guests
  • Last active:
  • Joined: --
Thx Titan. I made the changes and it works great.

btw. wouldn't it be better to set Draw to 1? Because I spotted this displaying error when resizing a GUI in x-direction when using a normal move instead of a MoveDraw.

Posted Image

Therefore I predefined Draw to 1:
Anchor(Control, x=0, y=0, w=0, h=0, Draw=1) 
  {
    ; Thanks to Titan for this function
    ; Details under http://www.autohotkey.com/forum/viewtopic.php?p=26778
    
    ; Control         : Variable assigned to the control (e.g. "MyEdit")
    ; x, y, w, h      : the anchor positions/dimensions
    ; Draw            : false/0 to use Move otherwise leave blank for a MoveDraw 
  
    global GuiW, GuiH
    IfEqual, GuiW
      {
        SetEnv, GuiW, %A_GuiWidth%
      }
    IfEqual, GuiH
      {
        SetEnv, GuiH, %A_GuiHeight%
      }
    pos = xywh
    Loop, Parse, pos
      {
        If (%A_LoopField% != "") 
          {
            If A_LoopField in x,w
              {
                v := %A_LoopField% + (A_GuiWidth - GuiW)
              }
            Else 
              {
                v := %A_LoopField% + (A_GuiHeight - GuiH)
              }
            move := move . A_LoopField . v
          } 
        IfNotEqual, Draw, 0
          {
            SetEnv, MDraw, Draw
          }
      }
    GuiControl, Move%MDraw%, %Control%, %move%
    Return, ErrorLevel
  }
__________________
Cheers
AGU

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Setting Draw to 1 means a MoveDraw will be used instead. MoveDraw causes flickering with other controls like Edit so the standard Draw is better. If you have more buttons in your GUI then you can set Draw to 1 in the first line of the function without having to change anything else.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
In version 2 you don't need to define GuiW or GuiH for multiple GUI's. I've also combined the x/y/w/h params so it's similar to GuiControl - see the example.
Older versions are still available for download.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
Titan,
I got late in testing out this nice function... its very well-made and works great.. thanks for posting!

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Thanks. I have a bad habbit of not commenting my scripts and using short/cryptic variable names so I have to apologize if parts were hard to understand. Atleast this means they're smaller in size thus include-friendly :roll:

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Version 3 is much easier to use. The function does all the calculations itself so resizing with multiple windows and relatively positioned controls are not a problem at all. For example you can call Anchor("MyEdit", "wh") without having to specify the width, height or window number.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit