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
toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
That's a great improvement.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I looked at the code of version 3.
I assume there is a bug in this line:
StringRight, wy, wy, InStr(wy, ",") - 1
This will return a wrong number for wy is the gui is not the last in the list. Since the code only trims away the left part of the win string, the windows on the right of the window in question are still in the string. Am I wrong?
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Another possible bug:
The code is designed to allow to use more then one GUI. But controls might be duplicate in the GUIs, Thus the pos array should not only list the control sizes by the control name, but in combination with the GUI id. That would it make it unequivocal, thus saver.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I would suggest:
Anchor(ctrl, a, draw = false) { ; v3.2 by Titan

    static pos



    If !InStr(pos, "|" . A_Gui . "_§$§_" . ctrl . ":") {

        GuiControlGet, p, Pos, %ctrl%

        pX -= %A_GuiWidth%

        pW -= %A_GuiWidth%

        pY -= %A_GuiHeight%

        pH -= %A_GuiHeight%

        pos = %pos%|%A_Gui%_§$§_%ctrl%:%pX%,%pW%,%pY%,%pH%

      }

    Loop, Parse, pos, |

        If InStr(A_LoopField, A_Gui . "_§$§_" . ctrl . ":"){

            StringSplit, Field, A_LoopField, :

            StringSplit, CtrlSize, Field2, `,

            Break

          }



    c = xwyh

    Loop, Parse, c

        If InStr(a, A_LoopField) {

            e := CtrlSize%A_Index%

            If A_Index < 3

                e += %A_GuiWidth%

            Else

                e += %A_GuiHeight%

            m = %m% %A_LoopField%%e%

          }



    If draw

        d = Draw

    GuiControl, Move%d%, %ctrl%, %m%

  }


Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

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

But controls might be duplicate in the GUIs

I never knew this was allowed... should be an easy fix as you pointed it out though. I'll make this update when I get back home kthx.

autohotkey.com/net Site Manager

 

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


toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005

But controls might be duplicate in the GUIs

I never knew this was allowed...

I meant that in each GUI there can be a Button1. Since they are in different GUIs it should be allowed.

I'm wondering if the code can be optimized for speed.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
It can even be made simpler. And faster, I assume. See my above post.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Thanks for the suggestions. I've updated it to v3.1

autohotkey.com/net Site Manager

 

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


toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Have you recognized the changes I applied in the above code? It is already version 3.2.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
It parses twice to makes extra calculations and store values in memory for anchors that might not even be used. This means it would take longer than it should. I've used your suggestions to optimize the original script and make all control references window specific. Thanks.

autohotkey.com/net Site Manager

 

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


toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005

It parses twice to makes extra calculations

Why do you think so? It only has one loop now. It actually is less calculation.
For each control the current POS is stored with the Width/Height substracted only at initialisation. The initial window size doesn't have to be memorized in an extra list and it saves one calculation in the subsequent calls of anchor for e. Plus the handling of the win list is not needed.

and store values in memory for anchors that might not even be used.

Which? It stores less values then before. The win list doesn't exist anymore.

This means it would take longer than it should.

Why?

I've used your suggestions to optimize the original script and make all control references window specific. Thanks.

Thanks. The 3.2 version has the same feature.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

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

It only has one loop now.

You have "Loop, Parse, , pos, |" and "Loop, Parse, c".

It actually is less calculation.

The calculations, pX, pW, pY and pH don't all have to be done before hand because only one or two anchors are ever used at once.

For each control the current POS is stored with the Width/Height substracted only at initialisation. The initial window size doesn't have to be memorized in an extra list and it saves one calculation in the subsequent calls of anchor for e. Plus the handling of the win list is not needed.

It's simpler to save the window dimensions to calculate the needed control positions.

... and store values in memory for anchors that might not even be used.

pX, pW, pY and pH don't have to be created because the math can be done in one go and added to "m" for GuiControl.

This means it would take longer than it should.

Extra calculations + extra variables + another parse = more time :?

autohotkey.com/net Site Manager

 

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


toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I'm sorry. I was basing my judgement on the 3.1 code I suggested. I looked at your code again:
Anchor(ctrl, a, draw = false) { ; v3.1 by Titan
	static win, pos
	pre = `n%A_Gui%:
	If !InStr(win, pre)
		win = %win%%pre%%A_GuiWidth%,%A_GuiHeight%
	StringTrimLeft, wy, win, InStr(win, pre) + 2
	StringLeft, wx, wy, InStr(wy, ",") - 1
	StringRight, wy, wy, InStr(wy, ",") - 1
This will not work if in the string win more then one window is stored and the window that calls Anchor is not the last in the string! Since StringRight will then put a lot of garbage in wy.
If !InStr(pos, pre . ctrl) {
		GuiControlGet, p, Pos, %ctrl%
		pos = %pos%%pre%%ctrl%,%pX%,%pW%,%pY%,%pH%,
	}
	StringTrimLeft, p, pos, InStr(pos, pre . ctrl) + StrLen(ctrl) + 1
	c = xwyh
	Loop, Parse, c
		If InStr(a, A_LoopField) {
			StringGetPos, e, p, `,, L%A_Index%
			StringTrimLeft, e, p, e + 1
			StringLeft, e, e, InStr(e, ",") - 1
			If A_Index < 3
				e += A_GuiWidth - wx
			Else e += A_GuiHeight - wy
The math with wx and wy is done every time Anchor is called, which can be very often. While in my code the wx and wy is substracted from the values only once. Which reduces the number of math actions.
			m = %m%%A_LoopField%%e%
		}
	If draw
		d = Draw
	GuiControl, Move%d%, %ctrl%, %m%
}

You say, that my code uses extra calculations. I doubt that.
1) It is less math, if you cound the number of times simple math is performed. I'm counting "e += A_GuiWidth - wx" as two math operations.
2) My code uses an extra parse loop, but your code uses much more string manipulation. It would need a test to see which is faster. And BTW: The parse loop can be avoided and replaced with your code, but then a comma has to follow the pH in the pos string.
3) One benefit of my code is that the original window size doesn't have to be fetched each time Anchor is called, it is already in the pos of the control. Which in total saves 3 string manipulations (which might be buggy as mentioned above) each time Anchor is called (but adding 4 simple math operations for each control during first call). I guess the math math in the beginning is shorter in time then doing the string maipulation each time. But I might be wrong since all these are very simple operations which might be done by the OS in no time.

Sorry, for being stuborn. Discard my comments if they offend you.

A compromize would be:
Anchor(ctrl, a, draw = false) { ; v3.3 by Titan 
    static pos 

    If !InStr(pos, "|" . A_Gui . "_§$§_" . ctrl . ":") { 
        GuiControlGet, p, Pos, %ctrl% 
        pX -= %A_GuiWidth% 
        pW -= %A_GuiWidth% 
        pY -= %A_GuiHeight% 
        pH -= %A_GuiHeight% 
        pos = %pos%|%A_Gui%_§$§_%ctrl%:%pX%,%pW%,%pY%,%pH%, 
      } 
    StringTrimLeft, CtrlSize, pos, InStr(pos, "|" . A_Gui . "_§$§_" . ctrl) + StrLen("|" . A_Gui . "_§$§_" . ctrl) + 1
    StringSplit, CtrlSize, CtrlSize, `, 

    c = xwyh 
    Loop, Parse, c 
        If InStr(a, A_LoopField) { 
            e := CtrlSize%A_Index% 
            If A_Index < 3 
                e += %A_GuiWidth% 
            Else 
                e += %A_GuiHeight% 
            m = %m% %A_LoopField%%e% 
          } 

    If draw 
        d = Draw 
    GuiControl, Move%d%, %ctrl%, %m% 
  } 
*not tested*
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Thanks, your feedback and constructive criticism is very helpful.

Like you proved, saving the window dimensions isn't needed. I have adopted your format of calculating positions and simplified it. Please see the new 3.2 version along with the updated example.

autohotkey.com/net Site Manager

 

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


toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Your 3.2 version is great. Thanks for accepting my remarks. You have even lifted it to the next higher level. I will definetely use it in my next scripts.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.