Sign in to follow this  
Followers 0
Polska

GUI Resizing Help

3 posts in this topic

I am currently working on a project to automate repetitive tasks at work. Data is entered into a form and the design requires that certain fields and other controls only display when certain controls are selected (primarily check boxes).

Due to this I need a manageable way to re-size the form window on a check box event. I experimented with wrapping the GUI code in a function so the entire form is redrawn when the check box is checked or unchecked. This has turned out to not be very maintainable as I need to save all currently entered data and other control states when recreating the form with the new size (and adding new controls).

Ideally I would simply be able to change the existing window size and move existing controls down as needed to make room for new ones (and vice versa). Searching the forums here I found mention of the WinMove() function which at first glance appeared to offer this functionality. When called like this:

WinMove( "FormTitle", "", Default, Default, $newWidth, $newHeight )

A strange thing happens, however. When doubling the height of the form window (as determined by GetClientWindow()) it only gets marginally larger, and when halving it back to the original height it is smaller than when it started.

Here is the original size:

Posted Image

Posted Image

Here is the doubled size:

Posted Image

Posted Image

And then halved, when it should return to the original:

Posted Image

Posted Image

Here is the relevant code:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt( 'GUIOnEventMode', 1 )
Global Const $TITLE = "Expand"

; GUI Code
Global $Window = GUICreate($TITLE, 429, 36, 200, 124)
Global $Checkbox1 = GUICtrlCreateCheckbox("Expand 1", 8, 8, 97, 17)
GUISetState(@SW_SHOW)

; Global state variables associated with the GUI
Global $WindowSize   = WinGetClientSize( $TITLE )
Global $WindowWidth  = $WindowSize[0]
Global $WindowHeight = $WindowSize[1]

; Register callbacks
GUICtrlSetOnEvent( $Checkbox1, "Checkbox1" )

Func Checkbox1()
    If GUICtrlRead( $Checkbox1 ) = $GUI_CHECKED Then
        DebugWindow()
        $WindowHeight *= 2
        UpdateWindow()
        DebugWindow()
    Else
        DebugWindow()
        $WindowHeight /= 2
        UpdateWindow()
        DebugWindow()
    EndIf
EndFunc

Func DebugWindow()
    MsgBox( 0, "Debug", "Window Size: " & @CRLF & @CRLF & _
        "Window width: " & $WindowWidth & @CRLF & _
        "Window height: " & $WindowHeight )
EndFunc

Func UpdateWindow()
    WinMove( $TITLE, "", Default, Default, $WindowWidth, $WindowHeight )
EndFunc

; Main idle loop
While 1
    Sleep(1000)
WEnd

I'm sure this is a case of my doing something wrong, and if anyone has any ideas I would greatly appreciate any input. Alternately, if anyone has used another, more viable method for handling window resizing I am also open to suggestions.

Thanks in advance!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

WinMove() uses window coordinates, not client coordinates!! If you want to have the resize based on the client coordinates you have to do some math, this should work:

Func _ClientResize($hWnd, $iWidth, $iHeight)
    $rClient = _WinAPI_GetClientRect($hWnd)
    $rWindow = _WinAPI_GetWindowRect($hWnd)
    $iDiffX = (DllStructGetData($rWindow, "right") - DllStructGetData($rWindow, "left")) - DllStructGetData($rClient, "right")
    $iDiffY = (DllStructGetData($rWindow, "bottom") - DllStructGetData($rWindow, "top")) - DllStructGetData($rClient, "bottom")
    WinMove($hWnd, "", DllStructGetData($rWindow, "left"), DllStructGetData($rWindow, "top"), $iWidth + $iDiffX, $iHeight + $iDiffY)
EndFunc

Remember to add "#Include <WinAPI.au3>" so the _WinAPI*() work

:mellow:

Edit: I'm not sure why I used _WinAPI_GetWindowRect, you could just get the size the normal way with WinGetPos()

Edited by AdmiralAlkex

Share this post


Link to post
Share on other sites

That makes perfect sense given what was happening and I expected that this might be the case.

I guess I had assumed I could use the height and width returned by WinGetClientSize() as it is in the same family of functions, and was referenced as related in the documentation.

Thanks for leading me in the right direction!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0