Issue using WinGetPos - GUI resizes without change by user. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MrCompTech
Posts: 5
Joined: 13 Jan 2021, 13:54

Issue using WinGetPos - GUI resizes without change by user.

Post by MrCompTech » 05 Oct 2022, 18:28

Without manually or programmatically moving or resizing the GUI it keeps getting bigger up to a, presumably, Windows 10 limit.

The goal of the script is to be able to move and/or resize the gui, close/destroy the gui, then reopen the GUI at the same location and at the same size it was when closed/destroyed.

On starting the script it is the correct preset size. Then alternating display of the GUI using the hotkey and then Alt-F4 to close the GUI, the GUI grows larger in both width and height each iteration.

First to fourth display of the GUI:
Width 400 / 1032 / 2612 / 3876
Height 155 / 476 / 1278 / 2196

Code: Select all

#Persistent

Xcoord := 769
Ycoord := 389
Width  := 400
Height := 155

#6:: 
    Gui, destroy
    Gui, +Resize
    Gui, Add, Text, x12 y9 w360 h130, It is important to take care of the patient to be followed by the doctor. 
    Gui, Show, x%Xcoord% y%Ycoord% w%Width% h%Height%, Test Window
Return

GuiClose:
    WinGetPos, Xcoord, Ycoord, Width, Height, Test Window
    Clipboard := Width . "`r" . Height
    Gui, destroy
Return

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Issue using WinGetPos - GUI resizes without change by user.

Post by gregster » 05 Oct 2022, 19:05

In the GuiClose: subroutine you are filling the variables width and height with "the width and height of the target window" (according to the WinGetPos docs), overwriting your original values for the variables width and height (400 and 155) which you set in the auto-execute section of the script.

Then, when recreating that GUI, you are using those same (updated) variables with Gui, Show which sets the dimensions of the client area of a window:
https://www.autohotkey.com/docs/commands/Gui.htm#Show wrote:Wn: Specify for n the width (in pixels) of the window's client area (the client area excludes the window's borders, title bar, and menu bar).

Hn: Specify for n the height of the window's client area, in pixels.
This means, the whole new window will be bigger than these newly used client area dimensions (which were actually dimesnions of a complete window). That's why the window keeps getting bigger, each time you call Gui, Show with updated variables.
To prevent that you could use different variable names, if you really need width and height of the closed window for something.

If you want to keep the window dimensions, why destroy and re-create the GUI each time? Just hide and unhide it instead.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Issue using WinGetPos - GUI resizes without change by user.

Post by Xtra » 05 Oct 2022, 22:52

Looks like you want to keep the gui position to open where it was last closed.
Easy fix:
change:

Code: Select all

WinGetPos, Xcoord, Ycoord, Width, Height, Test Window
to

Code: Select all

WinGetPos, Xcoord, Ycoord,,, Test Window
I agree with Gregster you could hide the gui instead of destroying.

MrCompTech
Posts: 5
Joined: 13 Jan 2021, 13:54

Re: Issue using WinGetPos - GUI resizes without change by user.

Post by MrCompTech » 06 Oct 2022, 14:21

Thanks for responding.
grester, thanks for pointing out the why of the issue.
Xtra, in trying to work up this script I was able to only track the x and y and leave out the width and height and the script will show/destroy the gui as it is moved.

But, the goal of the script is to be able to move and/or resize the gui, then close/destroy it, then reopen the GUI at the same location it wasw moved to and at the same size it was when closed/destroyed.

The manual does state that Show works on the client area and that WinGetPos work on the target window. I find this to be vague, but it is was it is. The manual also says "the client area excludes the window's borders, title bar, and menu bar". But, at least to me, that doesn't account for and increase of 258% in width over one iteration and 300% in height.

I have tried decreasing the value of WinGetPos by an appropriate proportions, but the end result is still the same it just take more iterations to succumb to the same conclusion.

All the above included I am still left looking for a solution.

MrCompTech
Posts: 5
Joined: 13 Jan 2021, 13:54

Re: Issue using WinGetPos - GUI resizes without change by user.

Post by MrCompTech » 06 Oct 2022, 14:54

I just came across this article on How-to-Geek, https://www.howtogeek.com/howto/28663/create-a-hotkey-to-resize-windows-to-a-specific-size-with-autohotkey/ .

At first glance this seems like it may lead to a solution. But I would prefer to use only one hotkey to perform both the initial show of the GUI as well as subsequent closes and shows. But I think this may need to hide instead of destroying the gui. Not sure, my coffee will likely help fix the problem :dance:

Suggestions are welcome !

MrCompTech
Posts: 5
Joined: 13 Jan 2021, 13:54

Re: Issue using WinGetPos - GUI resizes without change by user.  Topic is solved

Post by MrCompTech » 06 Oct 2022, 17:11

A couple of large coffee and a few aspirins and I've finally got it.

As usual, a solution often is about changing the way of thinking about as much as it is gaining knowledge. Thanks to grester and Xtra for reminding of that.

What was needed was to stop thinking that the GUI definition and display/hide had to be in the same flow.

Code: Select all

#Persistent

Xcoord := 769
Ycoord := 389
Width  := 400
Height := 155

Gui, +Resize
Gui, Add, Text, x12 y9 w360 h130, It is important to take care of the patient to be followed by the doctor. 
Gui, Show, x%Xcoord% y%Ycoord% w%Width% h%Height%, MyGUI
MyGUI := WinExist("Test Window")

#6::
    WinShow MyGUI
    WinMove Xcoord, Ycoord, Width, Height, MyGUI
Return

#8::
    ExitApp
Return

GuiClose:
    WinGetPos, Xcoord, Ycoord, Width, Height, MyGUI
    WinHide MyGUI
Return

Post Reply

Return to “Ask for Help (v1)”