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 post
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 post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Jemboy
      Hi,
      For an internal project I want to write internal script to mail some special request to an external party.
      To make it a little bit universal I decided that the user should be able to write or edit the e-mail signature.
      Because I wanted to put a logo into the e-mail, I decided to use RichEdit.

      I I found the following code by  @UEZ and adapted it a little to save and load.
      When I start the script and edit the text only, I can save and load the signature.rtf.
      However the moment I resize one of the images (with the mouse), the script wil only save the edited image.
      Somehow resizing an image deletes the other content of the RichEdit object.
      Does anyone know a solution for me?
      P.s. you need to delete signature.rtf to reset the file.
      #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <GuiRichEdit.au3> #include <WindowsConstants.au3> Example() Func Example() Local $hGui, $iMsg, $idBtnExit, $hRichEdit $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 520, 550, -1, -1) $hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 500, 490, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) $idBtnExit = GUICtrlCreateButton("Exit", 10, 510, 40, 30) $GoodRead = False If FileExists(@ScriptDir & "\signature.rtf") Then $ResSFF = _GUICtrlRichEdit_StreamFromFile($hRichEdit, @ScriptDir & "\signature.rtf") If $ResSFF=true then $GoodRead = True EndIf If $GoodRead=False Then _GUICtrlRichEdit_InsertText($hRichEdit, "Inserting image..." & @LF & @LF) _GUICtrlRichEdit_InsertText($hRichEdit, @LF & "JPG image scaled:" & @LF & @LF) _GUICtrlRichEdit_InsertBitmap($hRichEdit, "c:\Program Files (x86)\AutoIt3\Examples\GUI\mslogo.jpg", "\qc", "\picw6747\pich1058\picwgoal6690\pichgoal1860\") ;\qc = centered _GUICtrlRichEdit_InsertText($hRichEdit, @LF & @LF & "PNG image:" & @LF & @LF) _GUICtrlRichEdit_InsertBitmap($hRichEdit, "c:\Program Files (x86)\AutoIt3\Examples\GUI\Torus.png") _GUICtrlRichEdit_InsertText($hRichEdit, @LF & @LF & "Done.") EndIf GUISetState(@SW_SHOW) While True Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idBtnExit _GUICtrlRichEdit_StreamToFile($hRichEdit, @ScriptDir & "\signature.rtf") _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes GUIDelete() Exit EndSwitch WEnd EndFunc ;==>Example Func _GUICtrlRichEdit_InsertBitmap($hWnd, $sFile, $sFormatFunctions = "\", $sBitmapFunctions = "\", $iBgColor = Default) ;coded by UEZ build 2016-02-16 If Not FileExists($sFile) Then Return SetError(0, 0, 1) If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(0, 0, 2) _GDIPlus_Startup() Local $hImage = _GDIPlus_ImageLoadFromFile($sFile) If @error Then _GDIPlus_Shutdown() Return SetError(0, 0, 3) EndIf Local Const $aDim = _GDIPlus_ImageGetDimension($hImage) Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($aDim[0], $aDim[1]), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap) If $iBgColor = Default Then $iBgColor = 0xFF000000 + _WinAPI_SwitchColor(_GUICtrlRichEdit_GetBkColor($hWnd)) EndIf _GDIPlus_GraphicsClear($hGfx, $iBgColor) _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage, 0, 0, $aDim[0], $aDim[1]) _GDIPlus_GraphicsDispose($hGfx) Local $binStream = _GDIPlus_StreamImage2BinaryString($hBitmap, "BMP") If @error Then _GDIPlus_ImageDispose($hImage) _GDIPlus_ImageDispose($hBitmap) _GDIPlus_Shutdown() Return SetError(0, 0, 4) EndIf Local $binBmp = StringMid($binStream, 31) Local Const $binRtf = "{\rtf1\viewkind4" & $sFormatFunctions & " {\pict{\*\picprop}" & $sBitmapFunctions & "dibitmap " & $binBmp & "}\par}" ;check out http://www.biblioscape.com/rtf15_spec.htm _GUICtrlRichEdit_AppendText($hWnd, $binRtf) $binStream = 0 $binBmp = 0 _GDIPlus_ImageDispose($hImage) _GDIPlus_ImageDispose($hBitmap) _GDIPlus_Shutdown() Return 1 EndFunc ;==>_GUICtrlRichEdit_InsertBitmap Func _GDIPlus_StreamImage2BinaryString($hBitmap, $sFormat = "JPG", $iQuality = 80, $bSave = False, $sFileName = @ScriptDir & "\Converted.jpg") ;coded by UEZ 2013 build 2014-01-25 (based on the code by Andreik) Local $sImgCLSID, $tGUID, $tParams, $tData Switch $sFormat Case "JPG" $sImgCLSID = _GDIPlus_EncodersGetCLSID($sFormat) $tGUID = _WinAPI_GUIDFromString($sImgCLSID) $tData = DllStructCreate("int Quality") DllStructSetData($tData, "Quality", $iQuality) ;quality 0-100 Local $pData = DllStructGetPtr($tData) $tParams = _GDIPlus_ParamInit(1) _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData) Case "PNG", "BMP", "GIF", "TIF" $sImgCLSID = _GDIPlus_EncodersGetCLSID($sFormat) $tGUID = _WinAPI_GUIDFromString($sImgCLSID) Case Else Return SetError(1, 0, 0) EndSwitch Local $hStream = _WinAPI_CreateStreamOnHGlobal() ;http://msdn.microsoft.com/en-us/library/ms864401.aspx If @error Then Return SetError(2, 0, 0) _GDIPlus_ImageSaveToStream($hBitmap, $hStream, DllStructGetPtr($tGUID), DllStructGetPtr($tParams)) If @error Then Return SetError(3, 0, 0) Local $hMemory = _WinAPI_GetHGlobalFromStream($hStream) ;http://msdn.microsoft.com/en-us/library/aa911736.aspx If @error Then Return SetError(4, 0, 0) Local $iMemSize = _MemGlobalSize($hMemory) If Not $iMemSize Then Return SetError(5, 0, 0) Local $pMem = _MemGlobalLock($hMemory) $tData = DllStructCreate("byte[" & $iMemSize & "]", $pMem) Local $bData = DllStructGetData($tData, 1) _WinAPI_ReleaseStream($hStream) ;http://msdn.microsoft.com/en-us/library/windows/desktop/ms221473(v=vs.85).aspx _MemGlobalFree($hMemory) If $bSave Then Local $hFile = FileOpen($sFileName, 18) If @error Then Return SetError(6, 0, $bData) FileWrite($hFile, $bData) FileClose($hFile) EndIf Return $bData EndFunc ;==>_GDIPlus_StreamImage2BinaryString  
       
    • By XGamerGuide
      👋 Hey
      I want to call a function when something changes on an element in my GUI. That should work for a combo box (with $CBS_DROPDOWNLIST) when I select an item and for a text input when I type.
    • By NassauSky
      Hi all,
      The more projects I work on, the harder it is to find old scripts I created with certain techniques in them. I'd like to leave this here for others to get an idea from and it will be available now by keyword searching in this forum.
      This project needed an evenly distributed button grid setup on a touch screen with half decent sized buttons and spacing so the user would be less likely to press the key beside it. Here is one quick example to give anyone an idea of a way to handle it.
      #include <GUIConstants.au3> GUICreate("Button Grid", 1080, 100) ; will create a dialog box that when displayed is centered GUISetState(@SW_SHOW) ; will display an empty dialog box ;--Create and Position Controls Dim $btnActivated[1][2] ;Col 1 is button id, Col 2 is True/False used in 1 example as depressed Dim $btnArray[100][2] ;Col 1 is button id, Col 2 is True/False used in 1 example as depressed $num = 0 $btnActivated[0][0] = GUICtrlCreateButton("Activated", 0, 0, 100, 100) ;x,y,w,h $btnActivated[0][1] = True GUICtrlSetBkColor(-1,0x00ff00) GUICtrlSetColor(-1,0x005500) GUICtrlSetFont(-1,14) For $Y = 0 To 1 For $X = 0 To 7 ConsoleWrite( ($X * 100) + ($X*20) + 120 & @CRLF) If $Y=0 Then $btnArray[$num][0] = GUICtrlCreateButton($num + 1, ($X * 100) + ($X*20) + 120, 0, 100, 40) ;x,y,w,h Else $btnArray[$num][0] = GUICtrlCreateButton($num + 1, ($X * 100) + ($X*20) + 120, ($Y * 40) + $Y*20, 100, 40) ;x,y,w,h EndIf GUICtrlSetBkColor($btnArray[$num][0], 0xFFFFFF) $num += 1 Next Next ;--Set Button Names GUICtrlSetData($btnArray[0][0],"Autoit") GUICtrlSetData($btnArray[1][0],"Is") GUICtrlSetData($btnArray[2][0],"Cool") ; Run the GUI until the dialog is closed While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $btnActivated[0][0] $btnActivated[0][1] = NOT $btnActivated[0][1] If $btnActivated[0][1] Then GUICtrlSetBkColor($btnActivated[0][0],0x00ff00) GUICtrlSetColor($btnActivated[0][0],0x005500) GUICtrlSetData($btnActivated[0][0],"Activated") GUICtrlSetFont($btnActivated[0][0],14) Else GUICtrlSetBkColor($btnActivated[0][0],0xFF9999) GUICtrlSetColor($btnActivated[0][0],0x550000) GUICtrlSetData($btnActivated[0][0],"DeActivated") GUICtrlSetFont($btnActivated[0][0],13) EndIf Case $btnArray[0][0] If $btnActivated[0][1] Then MsgBox(0,"Notice","Btn1",1) EndIf Case $btnArray[1][0] If $btnActivated[0][1] Then MsgBox(0,"Notice","Btn2",1) EndIf Case $btnArray[2][0] If $btnActivated[0][1] Then MsgBox(0,"Notice","Btn3",1) EndIf EndSwitch WEnd I couldn't find one of my old projects that broke the button array into more columns that included the actual button name in the field. Feel free to share your own or share a more flexible version of this simple example.

    • By It_is_me_Me
      I am making some GUI with combobox for the Baudrates communications and I listed 9600, 57600, 115200. 
      I wrote the code to be defaulted to 57600 like this:
      GUICtrlSetData($comboBox_opticalBaudRate, "9600|57600|115200", "57600")
      But I wanted the GUI to remember the last item the user picks so the baudrates will NOT be defaulted to 57600.
      Example, I pick 9600 as my baudrates, then I close the GUI, after opening it, it always prompt 57600 since that is what I did in my code. But I want that if I choose 9600 and close the GUI, opening it again will show 9600 now instead of 57600.
      Is there a way to do it in GUI setting?
       
      Thanks.
    • By ashraful089
      #include <ButtonConstants.au3>
      #include <ComboConstants.au3>
      #include <GUIConstantsEx.au3>
      #include <WindowsConstants.au3>
      #Region ### START Koda GUI section ### Form=C:\Users\Engr. Ashraful\Videos\koda_1.7.3.0\Forms\ProgramPlayer\FormProgamToOpen.kxf
      $FormInput = GUICreate("Input", 302, 205, 454, 285)
      $Resolution = GUICtrlCreateCombo("Resolution", 80, 24, 145, 25)
      GUICtrlSetData(-1, "1920|VM1920|1366")
      $ComboProgramType = GUICtrlCreateCombo("Program Type Selection", 80, 64, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
      GUICtrlSetData(-1, "Multimedia|Official|Design|Web")
      $ButtonEnter = GUICtrlCreateButton("Enter", 104, 104, 75, 25)
      GUISetState(@SW_SHOW)
      #EndRegion ### END Koda GUI section ###
      While 1
          $nMsg = GUIGetMsg()
          Switch $nMsg
              Case $GUI_EVENT_CLOSE
                  Exit
          EndSwitch
      WEnd
       
      please help me to read combo box input and use the value for another function/ work
       
×
×
  • Create New...