Hi,
I imagine that if the user interface were to contain more controls, your code could quickly become difficult to manage, even though it is well-written. I suggest leaving everything to the message handler to optimize the code a bit. Additionally, you are making too many unnecessary calls to the control movement function, especially within a loop, which is not recommended. You could specifically target the events you need in order to handle them correctly. Here is a proposal to optimize your code.
;----------------------------------------------------------------------------------------
; Title...........: DragAdjust.au3
; Description.....: Resize and reposition the GUI controls when the user drags the DragBar.
; AutoIt Version..: 3.3.16.1 Author: ioa747
; Note............: Tested in Win10 22H2
;----------------------------------------------------------------------------------------
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <WinAPIGdi.au3>
#include <Math.au3>
; Global constants
Global Const $GUI_WIDTH = 600
Global Const $GUI_HEIGHT = 315
Global Const $MARGIN = 4
Global Const $DRAGBAR_WIDTH = $MARGIN
; GUI creation
Global $hGUI = GUICreate("Resizable GUI", $GUI_WIDTH, $GUI_HEIGHT, 205, 130)
Global $Combo1 = GUICtrlCreateCombo("Combo1", $MARGIN, $MARGIN, 196, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
Global $List1 = GUICtrlCreateList("", $MARGIN, 30, 196, 285)
Global $Edit1 = GUICtrlCreateEdit("Edit1", 204, $MARGIN, 392, 305)
Global $DragBar = GUICtrlCreateLabel("", 200, 0, $DRAGBAR_WIDTH, $GUI_HEIGHT, $SS_CENTERIMAGE, $WS_EX_LAYERED)
; Set drag bar cursor
GUICtrlSetCursor($DragBar, 13)
GUISetState(@SW_SHOW)
; Variables to track mouse movement
Global $iPrevMousePos = -1
; Main loop
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $GUI_EVENT_PRIMARYDOWN
AdjustGUIControls()
EndSwitch
WEnd
GUIDelete()
; Function to adjust GUI controls when the drag bar is moved
Func AdjustGUIControls()
Local Static $iOffset = $MARGIN / 2
If Not WinActive($hGUI) Then Return SetError(1, 0, False)
Local $aCursorInfo = GUIGetCursorInfo($hGUI)
Local $iActiveCtrl = IsArray($aCursorInfo) ? $aCursorInfo[4] : 0
Local $iPrimaryDown = 0
If $iActiveCtrl = $DragBar Then
Opt("MouseCoordMode", 2) ; 1=absolute, 0=relative, 2=client
Do
$aCursorInfo = GUIGetCursorInfo($hGUI)
$iPrimaryDown = $aCursorInfo[2]
Local $iMousePos = MouseGetPos(0)
$iMousePos = _Clamp($iMousePos, $MARGIN, $GUI_WIDTH - $MARGIN)
; Only update positions if the mouse has moved
If $iMousePos <> $iPrevMousePos Then
GUICtrlSetPos($DragBar, $iMousePos - $iOffset)
GUICtrlSetPos($Combo1, $MARGIN, Default, $iMousePos - $MARGIN - $iOffset)
GUICtrlSetPos($List1, $MARGIN, Default, $iMousePos - $MARGIN - $iOffset)
GUICtrlSetPos($Edit1, $iMousePos + $iOffset, Default, $GUI_WIDTH - $iMousePos - $MARGIN - $iOffset)
$iPrevMousePos = $iMousePos
_WinAPI_RedrawWindow($hGUI)
EndIf
Sleep(10)
Until Not $iPrimaryDown
Opt("MouseCoordMode", 1) ; 1=absolute, 0=relative, 2=client
EndIf
EndFunc ;==>AdjustGUIControls
; Helper function to clamp a value between a minimum and maximum
Func _Clamp($value, $min, $max)
Return _Max($min, _Min($value, $max))
EndFunc