Jump to content

Drag-Adjust


Recommended Posts

Resize and reposition the GUI controls when the user drags the $DragBar.

the $DragBar is invisible, move the mouse between $List1 and $Edit1 and you will see the change in the cursor, then you can do it
 

; https://www.autoitscript.com/forum/topic/212020-drag-adjust/
;----------------------------------------------------------------------------------------
; 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............: Testet 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>

Global $iGuiW = 600, $iMargin = 4

Global $hGUI = GUICreate("Form1", $iGuiW, 315, 205, 130)
Global $Combo1 = GUICtrlCreateCombo("Combo1", $iMargin, $iMargin, 196, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
Global $List1 = GUICtrlCreateList("", $iMargin, 30, 196, 285)
Global $Edit1 = GUICtrlCreateEdit("Edit1", 204, $iMargin, 392, 305)

Global $DragBar = GUICtrlCreateLabel("", 200, 0, $iMargin, 315, $SS_CENTERIMAGE, $WS_EX_LAYERED)
GUICtrlSetCursor(-1, 13)
GUISetState(@SW_SHOW)

;**********************************
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    _DragAdjust()
    Sleep(10)
WEnd
;**********************************

Func _DragAdjust()
    Local Static $iOffset = $iMargin / 2
    If Not WinActive($hGUI) Then Return SetError(1, 0, False)
    Local $ActiveCtrl
    Local $aCtrl = GUIGetCursorInfo($hGUI)
    $ActiveCtrl = IsArray($aCtrl) ? $aCtrl[4] : 0

    If $ActiveCtrl = $DragBar Then
        Opt("MouseCoordMode", 2) ;1=absolute, 0=relative, 2=client
        If $aCtrl[2] = 1 Then ;Primary down
            While $aCtrl[2]
                $aCtrl = GUIGetCursorInfo($hGUI)
                Local $iMp = MouseGetPos(0) < $iMargin ? $iMargin : MouseGetPos(0) > ($iGuiW - $iMargin) ? $iGuiW - $iMargin : MouseGetPos(0)
                GUICtrlSetPos($DragBar, $iMp - $iOffset)
                GUICtrlSetPos($Combo1, $iMargin, Default, $iMp - $iMargin - $iOffset)
                GUICtrlSetPos($List1, $iMargin, Default, $iMp - $iMargin - $iOffset)
                GUICtrlSetPos($Edit1, $iMp + $iOffset, Default, $iGuiW - $iMp - $iMargin - $iOffset)
                Sleep(10)
            WEnd
            _WinAPI_RedrawWindow($hGUI)
        EndIf
        Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client
    EndIf
EndFunc   ;==>_DragAdjust

 

Please, every comment is appreciated!
leave your comments and experiences here!
Thank you very much  :)

Edited by ioa747
improvement

I know that I know nothing

Link to comment
Share on other sites

  • ioa747 changed the title to Drag-Adjust

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

 

Link to comment
Share on other sites

Posted (edited)

@Numeric1  thanks for the improvement points

In the new version for overlap window, I have adopted them

; https://www.autoitscript.com/forum/topic/212020-drag-adjust/
;----------------------------------------------------------------------------------------
; Title...........: _DragAdjust.au3
; Description.....: Resize and reposition the GUI controls when the user drags the $DragBar.
; AutoIt Version..: 3.3.16.1   Author: ioa747  Script Version: 2.0
; Note............: Testet 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 <ListBoxConstants.au3>
#include <WinAPIGdi.au3>

Global $iGuiW = 600, $iGuiH = 300, $iMargin = 4

Global $hGUI = GUICreate("Form1", $iGuiW, $iGuiH, 205, 130, $WS_OVERLAPPEDWINDOW)
Global $Combo1 = GUICtrlCreateCombo("Combo1", $iMargin, $iMargin, 196, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKHEIGHT) ;DOCK ctrl
Global $List1 = GUICtrlCreateList("", $iMargin, 29, 196, 267, BitOR($LBS_STANDARD, $LBS_NOINTEGRALHEIGHT))
Global $Edit1 = GUICtrlCreateEdit("Edit1", 204, $iMargin, 392, 292)
Global $DragBar = GUICtrlCreateLabel("", 200, 0, $iMargin, 315, $SS_CENTERIMAGE, $WS_EX_LAYERED)
GUICtrlSetCursor(-1, 13)
GUISetState(@SW_SHOW)

;**********************************
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $GUI_EVENT_PRIMARYDOWN
            _DragAdjust()
        Case $GUI_EVENT_RESIZED, $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESTORE
            _OrderPos()
    EndSwitch
    Sleep(10)
WEnd
;**********************************

;--------------------------------------------------------------------------------------------------------------------------------
Func _OrderPos()
    Local $aWin = WinGetClientSize($hGUI)
    Local $aCtrl = ControlGetPos($hGUI, "", $Combo1) ;DOCK ctrl
    $iGuiW = $aWin[0]
    $iGuiH = $aWin[1]
    ;ConsoleWrite($iGuiW & ", " & $iGuiH & @CRLF)
    GUICtrlSetPos($DragBar, $iMargin + $aCtrl[2], 0, $iMargin, $iGuiH)
    GUICtrlSetPos($List1, $iMargin, 2 * $iMargin + $aCtrl[3], $aCtrl[2], $iGuiH - (3 * $iMargin + $aCtrl[3]))
    GUICtrlSetPos($Edit1, 2 * $iMargin + $aCtrl[2], $iMargin, $iGuiW - (3 * $iMargin + $aCtrl[2]), $iGuiH - (2 * $iMargin))
    _WinAPI_RedrawWindow($hGUI)
EndFunc   ;==>_OrderPos
;--------------------------------------------------------------------------------------------------------------------------------
Func _DragAdjust()
    Local Static $iOffset = $iMargin / 2
    Local Static $iPrevMousePos = -1
    If Not WinActive($hGUI) Then Return SetError(1, 0, False)
    Local $ActiveCtrl
    Local $aCtrl = GUIGetCursorInfo($hGUI)
    $ActiveCtrl = IsArray($aCtrl) ? $aCtrl[4] : 0

    If $ActiveCtrl = $DragBar Then
        Opt("MouseCoordMode", 2) ;1=absolute, 0=relative, 2=client
        If $aCtrl[2] = 1 Then ;Primary down
            While $aCtrl[2]
                $aCtrl = GUIGetCursorInfo($hGUI)
                Local $iMousePos = MouseGetPos(0)
                $iMousePos = $iMousePos < $iMargin ? $iMargin : $iMousePos > ($iGuiW - $iMargin) ? $iGuiW - $iMargin : $iMousePos
                If $iMousePos <> $iPrevMousePos Then
                    GUICtrlSetPos($DragBar, $iMousePos - $iOffset)
                    GUICtrlSetPos($Combo1, $iMargin, Default, $iMousePos - $iMargin - $iOffset)
                    GUICtrlSetPos($List1, $iMargin, Default, $iMousePos - $iMargin - $iOffset)
                    GUICtrlSetPos($Edit1, $iMousePos + $iOffset, Default, $iGuiW - $iMousePos - $iMargin - $iOffset)
                    $iPrevMousePos = $iMousePos
                EndIf
                Sleep(10)
            WEnd
            _WinAPI_RedrawWindow($hGUI)
        EndIf
        Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client
    EndIf
EndFunc   ;==>_DragAdjust
;--------------------------------------------------------------------------------------------------------------------------------

 

Edited by ioa747
UpDate

I know that I know nothing

Link to comment
Share on other sites

Good job! Since the movement of the controls is done in both functions, you could create a single function for that. The code could be further simplified by removing the _OrderPos function.

Link to comment
Share on other sites

Posted (edited)

I incorporated them

; https://www.autoitscript.com/forum/topic/212020-drag-adjust/
;----------------------------------------------------------------------------------------
; Title...........: _DragAdjust.au3
; Description.....: Resize and reposition the GUI controls when the user drags the $DragBar.
; AutoIt Version..: 3.3.16.1   Author: ioa747  Script Version: 3.0
; Note............: Testet 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 <ListBoxConstants.au3>
#include <WinAPIGdi.au3>

Global $iGuiW = 600, $iGuiH = 300, $iMargin = 4

Global $hGUI = GUICreate("Form1", $iGuiW, $iGuiH, 205, 130, $WS_OVERLAPPEDWINDOW)
Global $Combo1 = GUICtrlCreateCombo("Combo1", $iMargin, $iMargin, 196, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKHEIGHT) ;DOCK ctrl
Global $List1 = GUICtrlCreateList("", $iMargin, 29, 196, 267, BitOR($LBS_STANDARD, $LBS_NOINTEGRALHEIGHT))
Global $Edit1 = GUICtrlCreateEdit("Edit1", 204, $iMargin, 392, 292)
Global $DragBar = GUICtrlCreateLabel("", 200, 0, $iMargin, 315, $SS_CENTERIMAGE, $WS_EX_LAYERED)
GUICtrlSetCursor(-1, 13)
GUISetState(@SW_SHOW)

;**********************************
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $GUI_EVENT_PRIMARYDOWN
            _DragAdjust()
        Case $GUI_EVENT_RESIZED, $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESTORE
            _DragAdjust(True)
    EndSwitch
    Sleep(10)
WEnd
;**********************************

Func _DragAdjust($bResized = False)
    Local Static $iOffset = $iMargin / 2
    Local Static $iPrevMousePos = -1
    If Not WinActive($hGUI) Then Return SetError(1, 0, False)

    If $bResized = True Then ;OrderPos
        Local $aWin = WinGetClientSize($hGUI)
        Local $aDockCtrl = ControlGetPos($hGUI, "", $Combo1) ;DOCK ctrl
        $iGuiW = $aWin[0]
        $iGuiH = $aWin[1]
        GUICtrlSetPos($DragBar, $iMargin + $aDockCtrl[2], 0, $iMargin, $iGuiH)
        GUICtrlSetPos($List1, $iMargin, 2 * $iMargin + $aDockCtrl[3], $aDockCtrl[2], $iGuiH - (3 * $iMargin + $aDockCtrl[3]))
        GUICtrlSetPos($Edit1, 2 * $iMargin + $aDockCtrl[2], $iMargin, $iGuiW - (3 * $iMargin + $aDockCtrl[2]), $iGuiH - (2 * $iMargin))
    Else
        Local $aCtrl = GUIGetCursorInfo($hGUI)
        Local $ActiveCtrl = IsArray($aCtrl) ? $aCtrl[4] : 0

        If $ActiveCtrl <> $DragBar Then Return SetError(2, 0, False)

        Opt("MouseCoordMode", 2)  ;1=absolute, 0=relative, 2=client
        If $aCtrl[2] = 1 Then     ;Primary down
            While $aCtrl[2]
                $aCtrl = GUIGetCursorInfo($hGUI)
                Local $iMousePos = MouseGetPos(0)
                $iMousePos = $iMousePos < $iMargin ? $iMargin : $iMousePos > ($iGuiW - $iMargin) ? $iGuiW - $iMargin : $iMousePos
                If $iMousePos <> $iPrevMousePos Then
                    GUICtrlSetPos($DragBar, $iMousePos - $iOffset)
                    GUICtrlSetPos($Combo1, $iMargin, Default, $iMousePos - $iMargin - $iOffset)
                    GUICtrlSetPos($List1, $iMargin, Default, $iMousePos - $iMargin - $iOffset)
                    GUICtrlSetPos($Edit1, $iMousePos + $iOffset, Default, $iGuiW - $iMousePos - $iMargin - $iOffset)
                    $iPrevMousePos = $iMousePos
                EndIf
                Sleep(10)
            WEnd
        EndIf
        Opt("MouseCoordMode", 1)  ;1=absolute, 0=relative, 2=client
    EndIf
    _WinAPI_RedrawWindow($hGUI)
EndFunc   ;==>_DragAdjust
;--------------------------------------------------------------------------------------------------------------------------------

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

Posted (edited)

It's good. But since events are managed distinctly through message handling, why not clearly differentiate responsibilities? It's an effective technique for development, maintenance, and debugging. You could simply simplify the code by considering all the suggestions. Here is a very simple and much more optimized example. Try it.

; https://www.autoitscript.com/forum/topic/212020-drag-adjust/
;----------------------------------------------------------------------------------------
; Title...........: _DragAdjust.au3
; Description.....: Resize and reposition the GUI controls when the user drags the $DragBar.
; AutoIt Version..: 3.3.16.1   Author: ioa747  Script Version: 2.0
; Note............: Testet 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 <ListBoxConstants.au3>
#include <WinAPIGdi.au3>

; Constants
Global $iGuiW = 600, $iGuiH = 300, $iMargin = 4

; Create GUI
Global $hGUI = GUICreate("Form1", $iGuiW, $iGuiH, 205, 130, $WS_OVERLAPPEDWINDOW)
Global $Combo1 = GUICtrlCreateCombo("Combo1", $iMargin, $iMargin, 196, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKHEIGHT) ; DOCK ctrl
Global $List1 = GUICtrlCreateList("", $iMargin, 2 * $iMargin + 25, 196, 267, BitOR($LBS_STANDARD, $LBS_NOINTEGRALHEIGHT))
Global $Edit1 = GUICtrlCreateEdit("Edit1", 204, $iMargin, 392, 292)
Global $DragBar = GUICtrlCreateLabel("", 200, 0, $iMargin, 315, $SS_CENTERIMAGE, $WS_EX_LAYERED)
GUICtrlSetCursor(-1, 13)
GUISetState(@SW_SHOW)

; Main loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $GUI_EVENT_PRIMARYDOWN
            _DragAdjust()
        Case $GUI_EVENT_RESIZED, $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESTORE
            _AdjustControls()
    EndSwitch
WEnd

; Functions

; Adjusts the positions and sizes of controls when the window is resized or DragBar is moved
Func _AdjustControls($iMousePos = -1)
    Local $aWin = WinGetClientSize($hGUI)
    $iGuiW = $aWin[0]
    $iGuiH = $aWin[1]

    Local $aCtrl = ControlGetPos($hGUI, "", $Combo1)
    Local $iPos = $iMousePos == -1 ? $aCtrl[2] + $iMargin : _Constrain($iMousePos, $iMargin, $iGuiW - $iMargin)

    GUICtrlSetPos($DragBar, $iPos - $iMargin, 0, $iMargin, $iGuiH)
    GUICtrlSetPos($Combo1, $iMargin, Default, $iPos - 2 * $iMargin)
    GUICtrlSetPos($List1, $iMargin, 2 * $iMargin + $aCtrl[3], $iPos - 2 * $iMargin, $iGuiH - (3 * $iMargin + $aCtrl[3]))
    GUICtrlSetPos($Edit1, $iPos, $iMargin, $iGuiW - $iPos - $iMargin, $iGuiH - 2 * $iMargin)

EndFunc   ;==>_AdjustControls

; Handles the dragging of the DragBar and adjusts the controls accordingly
Func _DragAdjust()
    Local Static $iPrevMousePos = -1
    If Not WinActive($hGUI) Then Return SetError(1, 0, False)

    Local $aCtrl = GUIGetCursorInfo($hGUI)
    If IsArray($aCtrl) And $aCtrl[4] = $DragBar Then
        Opt("MouseCoordMode", 2) ; 1=absolute, 0=relative, 2=client
        If $aCtrl[2] = 1 Then ; Primary down
            While $aCtrl[2]
                $aCtrl = GUIGetCursorInfo($hGUI)
                Local $iMousePos = MouseGetPos(0)
                $iMousePos = _Constrain($iMousePos, $iMargin, $iGuiW - $iMargin)
                If $iMousePos <> $iPrevMousePos Then
                    _AdjustControls($iMousePos)
                    $iPrevMousePos = $iMousePos
                EndIf
                Sleep(10)
            WEnd
        EndIf
        Opt("MouseCoordMode", 1)
    EndIf
EndFunc   ;==>_DragAdjust

; Constrains a value between a minimum and maximum
Func _Constrain($value, $min, $max)
    Return $value < $min ? $min : ($value > $max ? $max : $value)
EndFunc   ;==>_Constrain

 

This code structure offers several significant advantages:

1. Clear Separation of Responsibilities:
   Each function has a distinct and clear responsibility:
   - _AdjustControls manages the resizing of controls.
   - _DragAdjust handles the movement of controls when the drag bar is used.
   - _Constrain ensures values stay within defined limits.

2. Readability and Maintainability:
   The separation of responsibilities makes the code more readable. It is easier to understand what each part of the code does, which facilitates maintenance and future modifications.

3. Reusability:
   By isolating functionalities into distinct functions, it is easier to reuse these functions in other parts of the project or even in other projects. For instance, _Constrain could be reused wherever value constraints are needed.

4. Simplified Debugging:
   Having separate functions makes it easier to locate and fix errors. If an issue arises in the control movement, you know that the _DragAdjust function is likely the cause.

5. Scalability:
   The modular structure allows for easier addition of new features without disrupting existing functionalities. For example, adding a new resizing or moving method could be done by simply adding a new function.

6. Centralized Event Handling:
   The centralized event handler in the main loop makes the execution flow clearer and simplifies the management of various GUI events ($GUI_EVENT_CLOSE, $GUI_EVENT_PRIMARYDOWN, $GUI_EVENT_RESIZED, etc.).

In conclusion, this code structure allows for better organization, improves readability, and facilitates the maintenance and evolution of the project.

+-------------------------------------------------------------+
|                       Main Loop                              |
| +-------------------+-------------------------------------+ |
| | GUIGetMsg         |                                     | |
| |                   |                                     | |
| |                   |                                     | |
| |                   |                                     | |
| +-------------------+--+                                  | |
+------------------------|----------------------------------|-------+
                         |                                  |       |
                         v                                  v       |
                 +--------------+                    +-------------+ |
                 | _DragAdjust  |<------------------>| _Constrain  | |
                 |              |                    |             | |
                 +--------------+                    +-------------+ |
                           ^                                  |       |
                           |                                  |       |
                           v                                  |       |
                 +-----------------+                          |       |
                 | _AdjustControls |<------------------------+        |
                 +-----------------+                                  |
                                                                      |
                                                                      |
                                                                      |
                                                                      |
                                                                      |
                                                                      v
                                                                    +----------------+
                                                                    | GUI Functions  |
                                                                    +----------------+

 

 

Here are some remarks on your code:

👉 In your script, since you are already using GUICtrlSetPos to adjust the positions and sizes of controls, AutoIt should automatically handle the redrawing of these controls. Therefore, you can remove the calls to _WinAPI_RedrawWindow.

 

👉 You are using the Sleep function in the main loop while also using GUIGetMsg() to handle events. This significantly slows down your program because GUIGetMsg() already handles code timing to prevent CPU overload.

 

👉 The function _DragAdjust exhibits blatant redundancy, making it prone to errors and turning into a bug nest.

Edited by Numeric1
Link to comment
Share on other sites

Posted (edited)

 

Local $iPos = $iMousePos == -1 ? $aCtrl[2] + $iMargin : _Constrain($iMousePos, $iMargin, $iGuiW - $iMargin)

on this line why are you using ==  instead of = ?

 

Edit:

On 6/30/2024 at 4:12 PM, Numeric1 said:

you could create a single function for that

finally the idea of merging the functions was not good ?

 

14 hours ago, Numeric1 said:

Therefore, you can remove the calls to _WinAPI_RedrawWindow.

I thought so but wasn't sure

 

14 hours ago, Numeric1 said:

 You are using the Sleep function in the main loop while also using GUIGetMsg()

Yes, it's a habit I have to break

 

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

Posted (edited)
7 hours ago, ioa747 said:

 

Local $iPos = $iMousePos == -1 ? $aCtrl[2] + $iMargin : _Constrain($iMousePos, $iMargin, $iGuiW - $iMargin)

on this line why are you using ==  instead of = ?

 

I use it for code readability, as the condition to be tested is easily identifiable.

Also, I avoid misunderstandings with AutoIt. (I wouldn't want an accidental assignment of a value instead of a value test.). 🤫

 

7 hours ago, ioa747 said:

finally the idea of merging the functions was not good ?

 

No, the idea was to create a new function specifically for the proper movement of the controls.

 

Edited by Numeric1
Link to comment
Share on other sites

Posted (edited)
On Fri Jun 28 2024 (GMT+0000) at 3:28 PM, ioa747 said:

Resize and reposition the GUI controls when the user drags the $DragBar.

Great idea, especially on a resizable GUI (as you did in one of your example above)
So I adapted it to one of my script, with a horizontal purple dragbar (which reacts same as the greenish horizontal dragbar placed at its right), the purpose is to increase the vertical size of an Edit field while decreasing the size of another Edit field.

Here is a light functional script without registering any message (WM_ENTERSIZEMOVE, WM_EXITSIZEMOVE, WM_GETMINMAXINFO...) and without using the events returned by GUIGetMsg() i.e. $GUI_EVENT_RESIZED etc... I got a couple of little issues but basically it seems to work :

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

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration

Global $hGUI, $idTextFrom, $idTextTo, $idDragBar, $idCharStatus, $iLimit = 1 ; change it to 50 (for example) to see the difference

$hGUI = GUICreate("Trying DragBar", 900, 700, -1, -1, $WS_OVERLAPPEDWINDOW)
GUICtrlCreateButton("Paste Clipboard", 10, 10, 100, 30)
GUICtrlCreateButton("Open File", 10, 50, 100, 30)
GUICtrlCreateButton("Translate", 790, 10, 100, 30)
GUICtrlCreateButton("Copy Translated", 790, 50, 100, 30)
GUICtrlCreateLabel("Other controls in this area", 200, 30, 500, 25, BitOR($SS_CENTER,$SS_CENTERIMAGE))
$idTextFrom = GUICtrlCreateEdit("Original Text", 10, 100, 880, 275)
$idTextTo = GUICtrlCreateEdit("Translated Text", 10, 410, 880, 275)

$idDragBar = GUICtrlCreateLabel("", 10, 375, 780, 35)
GUICtrlSetBkColor(-1, 0x800080) ; purple
GUICtrlSetCursor(-1, 11) ; ↕ SizeNS cursor

$idCharStatus = GUICtrlCreateLabel("0 / 65535", 790, 375, 80, 35, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetBkColor(-1, 0x80FF80) ; greenish
GUICtrlSetCursor(-1, 11) ; ↕ SizeNS cursor

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop

        Case $idDragBar, $idCharStatus
        _DragAdjust()
    EndSwitch
WEnd

;==============================================
Func _DragAdjust()

    Local $iMousePos, $aInfo = GUIGetCursorInfo($hGUI)
    Local $iPrevMousePos = $aInfo[1] ; Vertical pos of mouse cursor (relative to GUI window, aka client coords)
    While $aInfo[2] ; Primary down
        $aInfo = GUIGetCursorInfo($hGUI)
        $iMousePos = $aInfo[1]
        If $iMousePos <> $iPrevMousePos Then
            _AdjustControls($iMousePos - $iPrevMousePos)
            $iPrevMousePos = $iMousePos
        EndIf
        Sleep(10)
    WEnd
EndFunc   ;==>_DragAdjust

;==============================================
Func _AdjustControls($iDelta)

    Local $a1Ctrl = ControlGetPos($hGUI, "", $idTextFrom)
    Local $a2Ctrl = ControlGetPos($hGUI, "", $idTextTo)
    If $a1Ctrl[3] + $iDelta < $iLimit Or $a2Ctrl[3] - $iDelta < $iLimit Then ; controls new height will become too small => do not adjust
        Return
    EndIf
    Local $aDrag = ControlGetPos($hGUI, "", $idDragBar)
    Local $aStat = ControlGetPos($hGUI, "", $idCharStatus)
    GUICtrlSetPos($idTextFrom, Default, Default, Default, $a1Ctrl[3] + $iDelta)
    GUICtrlSetPos($idTextTo, Default, $a2Ctrl[1] + $iDelta, Default, $a2Ctrl[3] - $iDelta)
    GUICtrlSetPos($idDragBar, Default, $aDrag[1] + $iDelta, Default, Default)
    GUICtrlSetPos($idCharStatus, Default, $aStat[1] + $iDelta, Default, Default)
EndFunc   ;==>_AdjustControls

If you want to keep a part of an Edit field visible, just adjust the global variable $iLimit, for example changing its value from 1 to 50
There is a 2nd version of the script (a bit more complicated) but not sure I'll use it, as everything should stay simple :)

 

Edited by pixelsearch
typo
Link to comment
Share on other sites

I would suggest small changes to _DragAjust in case cursor leaves the dragbar control by dragging out of the window.  I mean if you drag up to the limit and continue dragging up till the cursor goes outside the bar, it will continue dragging.  And this is not what should be.

Func _DragAdjust()
  Local $iMousePos, $aInfo = GUIGetCursorInfo($hGUI)
  Local $iPrevMousePos = $aInfo[1]   ; Vertical pos of mouse cursor (relative to GUI window, aka client coords)
  While $aInfo[2] And ($aInfo[4] = $idDragBar Or $aInfo[4] = $idCharStatus)  ; <<<<< change
    $aInfo = GUIGetCursorInfo($hGUI)
    $iMousePos = $aInfo[1]
    If $iMousePos <> $iPrevMousePos Then
      _AdjustControls($iMousePos - $iPrevMousePos)
      $iPrevMousePos = $iMousePos
    EndIf
  WEnd
EndFunc   ;==>_DragAdjust

ps. removing also the Sleep(10) as it may cause disruption of the dragging process in case of fast drag

Link to comment
Share on other sites

You can have a look at Melba's version: 

 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Thanks funkey, will do.

On Fri Jun 28 2024 (GMT+0000) at 3:28 PM, ioa747 said:

the $DragBar is invisible [...]

I guess my purple dragbar was more than visible in the example above :D
But it was only for the example purpose. In the real script, I just did this :

1) Dragbar color is now "3DLight color" (as named by Koda) with the following good result, first with my grey PC theme :

1-Dragbarwithgreyeditfields.png.107ce9c53c72af7400f6bf30e7f0fe2d.png

Then with a more usual theme where white color predominates, we can see that the dragbar color goes fine with white edit controls :

2-Dragbarwithwhiteeditfields.png.3cfc3f90b3e88550d07a2d1f997e4cd2.png

2) The greenish $idCharStatus control is no more draggable : there's no use of 2 draggable controls and who knows, maybe one day I'll find a more useful purpose when it is clicked.

3) While dragging, to avoid serious flicker problems (in both edit fields filled with text) with OS's that are not Win10+ then the extended style $WS_EX_COMPOSITED (applied during GUI creation) is a big help, as discussed in this thread

Thanks for reading and a have a great evening :bye:

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...