Jump to content

Recommended Posts

Posted (edited)

_DragNumAdjust

Adjust the value of a numeric control by dragging with the mouse.
It also checks whether the Control key (faster) or Shift key (slower) is pressed while dragging, which allows the user to adjust the sensitivity of the adjustment speed.

_DragNumAdjust ( $sControls, $iAxis = 1, $iSensitivity = 4 )

$sControls  A string containing the IDs of the controls to be adjusted, separated by a vertical bar |
                      Each control ID (Optionaly) is followed by its min and max values, separated by a semicolon ;
                      e.g. _DragNumAdjust("$idNum1;-100;100|$idNum2;-50;50|$idNum3")
                      Adjust control $idNum1 (min=-100, max=100) and $idNum2 (min=-50, max=50) and $idNum3 (without min/max limits)

$iAxis [optional] The axis to drag. 0 for horizontal, 1 for vertical. (Default is 1)

$iSensitivity [optional] The sensitivity factor for mouse sensitivity. Higher means slower change. (Default is 4)

 

; https://www.autoitscript.com/forum/topic/213011-_dragnumadjust/
;----------------------------------------------------------------------------------------
; Title...........: _DragNumAdjust.au3
; Description.....: Adjust the value of a numeric control by dragging with the mouse.
; AutoIt Version..: 3.3.16.1   Author: ioa747  Script Version: 0.1
; 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 <WinAPIDlg.au3>
#include <Array.au3>
#include <Misc.au3>

Global $hGUI, $idNum1, $idNum2, $idNum3, $idNum4

Example()

Func Example()
    $hGUI = GUICreate("Example GUI", 320, 220)

    $idNum1 = GUICtrlCreateInput("100", 10, 20, 70, 20)
    $idNum2 = GUICtrlCreateInput("20", 130, 20, 70, 20)
    $idNum3 = GUICtrlCreateInput("3", 10, 50, 70, 20)
    $idNum4 = GUICtrlCreateInput("30", 130, 50, 70, 20)

    Local $idBtn = GUICtrlCreateButton("Ok", 40, 180, 60, 20)
    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idBtn
                ExitLoop
        EndSwitch
        ; Test with negative min/max values
        _DragNumAdjust("$idNum1;-100;100|$idNum2;-50;50|$idNum3;0;10|$idNum4", 0) ; Horizontal dragging
    WEnd
EndFunc   ;==>Example


; #FUNCTION# ====================================================================================================================
; Name...........: _DragNumAdjust
; Description....: Adjust the value of a numeric control by dragging with the mouse.
; Syntax.........: _DragNumAdjust($sControls [, $iAxis = 1 [, $iSensitivity = 4]])
; Parameters.....: $sControls    - A string containing the IDs of the controls to be adjusted, separated by a vertical bar |
;                                  Each control ID (Optionaly) is followed by its min and max values, separated by a semicolon ;
;                  $iAxis        - [optional] The axis to drag. 0 for horizontal, 1 for vertical. (Default is 1)
;                  $iSensitivity - [optional] The sensitivity factor for mouse movement. Higher means slower change.(Default is 4)
; Return values .: Success: Returns None. (Just adjust the control value)
; Author ........: ioa747
; Modified ......:
; Remarks .......: This function allows users to adjust numeric control values by dragging the mouse.
; Related .......: ControlGetHandle, GUIGetCursorInfo, MouseGetPos, _IsPressed
; Link ..........: https://www.autoitscript.com/forum/topic/213011-_dragnumadjust/
; Example .......: _DragNumAdjust("$idNum1;-100;100|$idNum2|-50;50", 1) ; Adjust control $idNum1 (min=-100, max=100) and $idNum2 (min=-50, max=50)
; ===============================================================================================================================
Func _DragNumAdjust($sControls, $iAxis = 1, $iSensitivity = 4)
    ; Define constants locally.
    Local Const $_GUI_CURSOR_ARROW = 2    ; Default ↖ Arrow cursor
    Local Const $_GUI_CURSOR_SIZENS = 11  ; Vertical ↕ SizeNS cursor
    Local Const $_GUI_CURSOR_SIZEWE = 13  ; Horizontal ↔ SizeWE cursor
    Local Const $_MAXINT = 2147483647     ; Maximum value for a 32-bit signed integer
    Local Const $_MININT = -2147483648    ; Minimum value for a 32-bit signed integer

    Local Static $aCtrlParameters

    ; Initialize $aCtrlParameters once
    If Not IsArray($aCtrlParameters) Then
        Local $aParts = StringSplit($sControls, "|")
        Local $aTempArray[$aParts[0] + 1][3]
        For $i = 1 To $aParts[0]
            Local $aTmp = StringSplit($aParts[$i], ";")
            $aTempArray[$i][0] = Execute($aTmp[1])
            $aTempArray[$i][1] = ($aTmp[0] > 1 ? Number($aTmp[2]) : $_MININT) ; set Min
            $aTempArray[$i][2] = ($aTmp[0] > 2 ? Number($aTmp[3]) : $_MAXINT) ; set Max
        Next
        $aCtrlParameters = $aTempArray
    EndIf

    If Not WinActive($hGUI) Then Return ; Return if $hGUI has no focus
    Local $aMouseInfo = GUIGetCursorInfo($hGUI)

    ; $iFocusCtrlID = which control has the focus
    Local $hFocusCtrlHandle = ControlGetHandle($hGUI, "", ControlGetFocus($hGUI))
    Local $iFocusCtrlID = _WinAPI_GetDlgCtrlID($hFocusCtrlHandle)
    Local $iCtrlIdx = -1
    For $i = 1 To UBound($aCtrlParameters) - 1
        If $aCtrlParameters[$i][0] = $iFocusCtrlID Then
            $iCtrlIdx = $i
            ExitLoop
        EndIf
    Next
    If $iCtrlIdx = -1 Then Return ; Return if no matching control index is found

    ; $iCtrlUnderMouse = which control is under the mouse
    Local $iCtrlUnderMouse = IsArray($aMouseInfo) ? $aMouseInfo[4] : 0

    ; Return and set cursor to Default ↖ Arrow  if $iCtrlUnderMouse has no focus
    If $iCtrlUnderMouse <> $iFocusCtrlID Then Return GUICtrlSetCursor($iFocusCtrlID, $_GUI_CURSOR_ARROW)

    ; Set cursor type based on axis
    Local $iCursorType = $_GUI_CURSOR_ARROW
    If $iAxis = 1 Then
        $iCursorType = $_GUI_CURSOR_SIZENS
    ElseIf $iAxis = 0 Then
        $iCursorType = $_GUI_CURSOR_SIZEWE
    Else
        Return ; Exit if invalid axis
    EndIf
    GUICtrlSetCursor($iFocusCtrlID, $iCursorType)

    If $aMouseInfo[2] = 1 Then ; Primary button down
        Local $startValue = GUICtrlRead($iFocusCtrlID)    ; Store start value
        Local $lastDisplayedValue = $startValue           ; start value
        Local $startPos = MouseGetPos($iAxis)             ; Initial mouse position (Y or X)

        While $aMouseInfo[2] = 1 ; Primary button down
            $aMouseInfo = GUIGetCursorInfo($hGUI)         ; Update control
            Local $currentPos = MouseGetPos($iAxis)       ; Current mouse position (Y or X)

            Local $diff = Int(($startPos - $currentPos) / $iSensitivity) ; Adjust sensitivity

            If _IsPressed("11") Then      ; Control key makes adjustment faster
                $diff *= 4
            ElseIf _IsPressed("10") Then  ; Shift key makes adjustment slower
                $diff = Int($diff / 4)
            EndIf

            If $iAxis = 0 Then $diff *= -1 ; Horizontal increases to the right

            Local $newValue = $startValue + $diff

            Local $sMin = $aCtrlParameters[$iCtrlIdx][1]
            Local $sMax = $aCtrlParameters[$iCtrlIdx][2]

            $newValue = ($newValue < $sMin ? $sMin : $newValue)
            $newValue = ($newValue > $sMax ? $sMax : $newValue)

            If $newValue <> $lastDisplayedValue Then
                GUICtrlSetData($iFocusCtrlID, $newValue)
                $lastDisplayedValue = $newValue  ; Update last displayed value
            EndIf

            Sleep(10)
        WEnd
        GUICtrlSetCursor($iFocusCtrlID, $_GUI_CURSOR_ARROW)
    EndIf
EndFunc   ;==>_DragNumAdjust


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

Edited by ioa747
mising ;

I know that I know nothing

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