Jump to content

Why do controls always revert to initial position while resizing GUI?


Go to solution Solved by pixelsearch,

Recommended Posts

Posted (edited)

This has been driving me a bit bananas the past few days. 🍌

  • So you create a control at a specific position initially
  • You move it after the fact with _WinAPI_SetWindowPos or WinMove to it's new happy home

Now, you decide to resize the GUI and you notice that the control is no longer at it's new happy home during resizing of the GUI. It temporarily reverts to it's original position from when it was first created during the entire time that the GUI is resizing.

After resizing, you can move the control back to it's new happy home once again. But during resizing it always reverts.

Does anyone know why this happens like this and if there is anything that I can do to keep it in it's new happy home? :)

Thank you.

Here is the relevant code example:

#include <ButtonConstants.au3>
#include <WindowsStylesConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>

; DPI
DllCall("User32.dll", "bool", "SetProcessDpiAwarenessContext" , "HWND", "DPI_AWARENESS_CONTEXT" -2)

Global $g_hButton, $iHeightSpace, $hGUI
Global $bSizeChanged = False

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 400, -1, -1, $WS_OVERLAPPEDWINDOW)

    $aClientSize = WinGetClientSize($hGUI)
    ; original button position:  0, 0
    $idButton = GUICtrlCreateButton("Button", 0, 0, 300, 300)
    GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM)
    GUICtrlSetBkColor (-1, 0x808080)

    $g_hButton = GUICtrlGetHandle($idButton)

    $iHeightSpace = 26

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    GUIRegisterMsg($WM_SIZE, "WM_SIZE")

    ; Move button to new position with _WinAPI_SetWindowPos
    Sleep(1000)
    _WinAPI_SetWindowPos($g_hButton, 0, 0, 0 + $iHeightSpace + $iHeightSpace, 300, 300 - $iHeightSpace, BitOR($SWP_NOZORDER, $SWP_NOACTIVATE))

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch

        If $bSizeChanged Then
            ; reset position after WM_SIZE
            ; original button position:  0, 0
            _WinAPI_SetWindowPos($g_hButton, 0, 0, 0 + $iHeightSpace + $iHeightSpace, 300, 300 - $iHeightSpace, BitOR($SWP_NOZORDER, $SWP_NOACTIVATE))
            $bSizeChanged = False
        EndIf
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func WM_SIZE()
    ConsoleWrite("size changing" & @CRLF)
    $bSizeChanged = True
EndFunc

 

I've tried removing the GUICtrlSetResizing and a plethora of different things. But I am really perplexed on this. I appreciate any help and understanding with this. :)

Edited by WildByDesign
Posted
12 minutes ago, pixelsearch said:

Does this work for you ?

You are an absolute lifesaver! 🙏

Thank you so much. That did it. 

_WinAPI_SetWindowPos and WinMove moved the control as well but not in such a permanent way. GUICtrlSetPos did exactly what I was hoping for. :)

Posted

Yeah that's an Autoit thing, presumably to do with its "docking" logic (GUICtrlSetResizing()).

So you can also bypass this, and use the default windows logic by returning defWindowProcW() from your WM_SIZE handler.  

#include <guiConstants.au3>
#include <winapi.au3>

Global $hGui = GUICreate("", 300, 200, 100, 100, BitOR($WS_SIZEBOX, $WS_MINIMIZEBOX))

Global $idBtn = GUICtrlCreateButton("Button 1", 4, 4, 80, 80)
Global $hButton = GUICtrlGetHandle($idBtn)

GUIRegisterMsg($WM_SIZE, "WM_SIZE")
GUISetState()

Local $iMsg
While 1
    $iMsg = GUIGetMsg()
    switch $iMsg
        CAse $idBtn
            _WinAPI_SetWindowPos($hButton, 0, 84, 84, 80, 80, 0)

        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam)
    Return _WinAPI_DefWindowProcW($hWnd, $iMsg, $wParam, $lParam)
EndFunc

 

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
×
×
  • Create New...