Jump to content

using mouse wheel to change value in input box


lyly
 Share

Recommended Posts

Try this :

#include <WinAPIShellEx.au3>
#include <WinAPIConv.au3>
#include <GUIConstants.au3>

Global $hGUI = GUICreate("UpDown and scroll", -1, -1, -1, -1, $WS_SIZEBOX)

Global $idInput = GUICtrlCreateInput("2", 10, 10, 50, 20)
;~ $hInput = GUICtrlGetHandle($idInput)

Global $idUpdown = GUICtrlCreateUpdown($idInput)
;~ $hUpdown =GUICtrlGetHandle($idUpdown)

GUISetState(@SW_SHOW)

Global $hDll = DllCallbackRegister(SubclassProc, 'lresult', 'hwnd;uint;wparam;lparam;uint_ptr;dword_ptr')
Global $pDll = DllCallbackGetPtr($hDll)

_WinAPI_SetWindowSubclass($hGUI, $pDll, $idInput, 0)

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
WEnd
GUIDelete()
_WinAPI_RemoveWindowSubclass($hGUI, $pDll, 1000)
DllCallbackFree($hDll)

Func SubclassProc($hWnd, $iMsg, $wParam, $lParam, $iID, $pData)
  If $iMsg = $WM_MOUSEWHEEL Then
    Local $iDelta = _WinAPI_HiWord($wParam)
    Local $iCurrent = GUICtrlRead($iID)
    GUICtrlSetData($iID, $iDelta > 0 ? $iCurrent + 1 : $iCurrent - 1)
  EndIf
  Return _WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>_SubclassProc

 

Link to comment
Share on other sites

A couple of years ago, I used the following code in one of my script, which registers the WM_MOUSEWHEEL message :

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

Global $g_idInput

GUICreate("UpDown and scroll", -1, -1, -1, -1, $WS_SIZEBOX)
$g_idInput = GUICtrlCreateInput("2", 10, 10, 50, 20)
Local $idUpdown = GUICtrlCreateUpdown($g_idInput)

GUIRegisterMsg($WM_MOUSEWHEEL, "WM_MOUSEWHEEL")
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $g_idInput
            ; ConsoleWrite("here" & @crlf)
    EndSwitch
WEnd

;================================================
Func WM_MOUSEWHEEL($hWnd, $iMsg, $wParam, $lParam)
    Local $iDistance = BitShift($wParam, 16) ; +120 or -120 (based on WHEEL_DELTA, which is 120 : msdn)
    Local $iCurrent = GUICtrlRead($g_idInput)
    GUICtrlSetData($g_idInput, $iDistance > 0 ? $iCurrent + 1 : $iCurrent - 1)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MOUSEWHEEL

@Nine subclassing a window or registering a message, do you think one method could be preferred to the other ? Or are there equivalents ?
Thanks

Link to comment
Share on other sites

@Nine thanks for your answer

@lyly as there are probably other controls in your GUI, then you'll have to take care of not modifying inadvertently the input box value when "mouse wheeling" anywhere in the GUI

To prevent that, this is (basically) what I did in my WM_MOUSEWHEEL function, where 2 GUI's were involved :

Func WM_MOUSEWHEEL($hWnd, $iMsg, $wParam, $lParam)
    Local $iDistance = BitShift($wParam, 16) ; +120 or -120 (based on WHEEL_DELTA, which is 120 : msdn)

    ; MouseWheel when GUI Preview exists (interactive resize of any previewed image)
    ; ..................................
    If $hGUI_Preview Then
        ... ; process the resize of the previewed image
        Return $GUI_RUNDEFMSG
    EndIf

    ; MouseWheel when GUI Preview doesn't exist (interactive modification of 7 possible Input fields in main GUI) => update of one field
    ; .........................................
    Local $aInfo = GUIGetCursorInfo($hGUI_Main)
    If IsArray($aInfo) Then
        Switch $aInfo[4] ; ID of the control that the mouse cursor is hovering over (or 0 if none)
            Case $idInput11
                ...
            Case $idInput12
                ...
            Case ...
                ...
        EndSwitch
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MOUSEWHEEL

So as you see, in case you got several input fields "mouse wheelables", you'll have to hover over the concerned input field, then start the mouse wheeling. The input field taken care of will be the one retrieved by this line in the fuction :

Local $aInfo = GUIGetCursorInfo($hGUI_Main)

Good luck

Edited by pixelsearch
Link to comment
Share on other sites

Hi pixelsearch

I have done with 2 edit controls and it works as yousaid each edit has different number changes.

Thanks again

I did not use: $hGUI_Preview as it not included in my script and I think I do not need it, I only need about 10 edit control each with different number changes.

 

 If $hGUI_Preview Then
        ... ; process the resize of the previewed image
        Return $GUI_RUNDEFMSG
    EndIf
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...