Jump to content

Endless growing or shrinking GUI on GuiWidth save


Recommended Posts

Hi,

I am having a problem properly saving the Width of a resizable Gui.
When a user resizes the Gui it gets saved in an ini when the Gui closes to then restore the new Width upon reopening the app.

with GUICreate("myGui",300,200,Default,Default,$WS_SIZEBOX)
WinGetPos($hGUI) returns 314, and WinGetClientSize($hGUI) returns 298
when its then saved in the ini the gui keeps expanding or shrinking every time its opened by +14 or -2

I figure it has to do with borders etc, but i also guess borders depend on the window theme and whatnot or is user specific, so i can't just do $GuiWidth = $GetGuiWidth[arr] -14 or +2 right?

is there a proper way of doing this?

Thanks in advance,
Aapjuh

Link to comment
Share on other sites

Can anyone confirm i can't just do save ($GuiWidth = $aWinPos[2] - 14)?

I didn't find what i was looking for in that topic, or i missed something.
The function in the topic has static Gui Width and height values, but a resized Gui gets a value that can only be pulled by WinGetPos or WinGetClientSize which are +14 or -2 respectively, so in staid of 200 it pulls 214/198. which if saved to an ini keeps expanding or shrinking the app next time its opened.

The function _WinSetClientSize() seems to read the border as 8px while WinGetPos adds 14px, 14/2=7 not 8, also no clue what thats about else it would have been easy enough to do $Guiwidth = aGuiPos[2] - ($Border * 2).

Am i missing something?

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode",1)

;take the Gui width and height from the ini
Global $GuiWidth = IniRead(@ScriptDir & "/Test Settings.ini","Settings","GuiWidth",200)
Global $GuiHeight = IniRead(@ScriptDir & "/Test Settings.ini","Settings","GuiHeight",200)

Global $hGui, $aClientSize, $aWinPos
$hGui = GUICreate("Example", $GuiWidth, $GuiHeight,Default,Default,$WS_SIZEBOX)
GUISetOnEvent($GUI_EVENT_CLOSE,"Close_Exit")
GUISetOnEvent($GUI_EVENT_RESIZED,"Resize_Labels")

;gui is shown ofc else the user wouldn't have anything to resize
GUISetState(@SW_SHOW,$hGUI)

$aClientSize = WinGetClientSize($hGui)

MsgBox(0, 'Setting Styles', "Width of window's client area : " & $aClientSize[0] & @CRLF & "Height of window's client area: " & $aClientSize[1] & @CRLF)
        
While 1
    Sleep(100)
Wend

Func _WinSetClientSize($sTitle, $sText, $iWidth, $iHeigth, $iLeft = Default, $iTop = Default)
    Local $hWin = WinGetHandle($sTitle, $sText)
    If @error Then Return SetError(1, 0, $hWin)
    Local $aClientSize = WinGetClientSize($hWin)
    Local $aWinPos = WinGetPos($hWin)
    Local $iBorder = Int(($aWinPos[2] - $aClientSize[0]) / 2)
    Local $iBar = $aWinPos[3] - $aClientSize[1] - $iBorder
    If $iLeft = "" Or $iLeft = Default Then $iLeft = $aWinPos[0]
    If $iTop = "" Or $iTop = Default Then $iTop = $aWinPos[1]
    ;WinMove($hWin, "", $iLeft, $iTop, $iWidth + ($iBorder * 2), $iHeigth + $iBorder + $iBar, 0)
    ;
    ;*****This is where i run into problems, window will shrink by 2 every time the app is opened and closed******
    ;
    $GuiWidth = $iWidth - ($iBorder * 2)
    $GuiHeight = $iHeigth - ($iBorder * 2)
    ;
    ;
    Return $hWin
EndFunc   ;==>_WinSetClientSize

;getting the pos (width and height) after the user resized the window
Func Resize_Labels()
    $ResizedPos = WinGetPos($hGui)
    _WinSetClientSize($hGUI,"",$ResizedPos[2],$ResizedPos[3])
EndFunc

;Delete Gui and close
Func Close_Exit()
    ;Save the new Gui width and height in the ini
    IniWrite(@ScriptDir & "/Test Settings.ini","Settings","GuiWidth",$GuiWidth)
    IniWrite(@ScriptDir & "/Test Settings.ini","Settings","GuiHeight",$GuiHeight)
    GUIDelete($hGUI)
    Exit
EndFunc

 

Edited by Aapjuh
Link to comment
Share on other sites

  • 2 weeks later...

@Aapjuh If you're only interested in the client area of the GUI, why bother with borders, title etc... ?
The script below should do it, restoring the same client area after you resize and close

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

; read Gui width and height from ini file (client size)
Local $GuiWidth = IniRead(@ScriptDir & "\Test Settings.ini", "Settings", "GuiWidth", 300)
Local $GuiHeight = IniRead(@ScriptDir & "\Test Settings.ini", "Settings", "GuiHeight", 200)
ConsoleWrite("$GuiWidth = " & $GuiWidth & "   $GuiHeight = " & $GuiHeight & @crlf)

Local $hGui = GUICreate("Resize me", $GuiWidth, $GuiHeight, Default, Default, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX))

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Local $aClientSize = WinGetClientSize($hGui)
            ; write Gui width and height into ini file (client size)
            IniWrite(@ScriptDir & "\Test Settings.ini", "Settings", "GuiWidth", $aClientSize[0])
            IniWrite(@ScriptDir & "\Test Settings.ini", "Settings", "GuiHeight", $aClientSize[1])
            ConsoleWrite("$aClientSize[0] = " & $aClientSize[0] & "   $aClientSize[1] = " & $aClientSize[1] & @crlf)
            GUIDelete($hGUI)
            Exit
    EndSwitch
WEnd

 

Edited by pixelsearch
typo
Link to comment
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
 Share

×
×
  • Create New...