Wheel and Drag: [de]Increment Edit controls with the mouse!

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Wheel and Drag: [de]Increment Edit controls with the mouse!

10 Oct 2013, 15:29

Wheel & Drag

Image

Description:
* Allows you to change numeric values in edit controls by
dragging and/or scrolling the mouse wheel.
* Two modes for Dragging exist, up/down and left/right.
* Please see the comments of the functions aswell as the
demo code for more information.

Code: Select all

/*
Name: Wheel &  Drag
Version 1.0 (Thu October 10, 2013)
Created: Thu October 10, 2013
Author: tidbit
Credit: AfterLemon - detecting a bug some people may or may not get.

Description:
   * Allows you to change numeric values in edit controls by 
   dragging the mouse and/or scrolling the mouse wheel.
   * Two modes for Dragging exist, up/down and left/right.
   * Please see the comments of the functions aswell as the
   demo code for more information.
   
*/

; /*
; -----------------
; --- DEMO CODE ---
; -----------------
; the only thing you need to do is add either (or both) of these 2 lines
; at the top of your script:
; OnMessage(WM_MOUSEWHEEL:=0x20A, "wheel")
; OnMessage(WM_MOUSEMOVE:=0x200, "drag")

#SingleInstance, force

Gui, font, s10, tahoma

Gui, add, text, xy y6 w500 cblue,
(ltrim join`s
This little demo contains 2 functions: Drag() and Wheel().
`n* Activate an edit control and scroll the mouse-wheel to change the value.
`n* Alternatively, Hold down the Left Mouse Button and drag the mouse up or down to
adjust the values.
`n* You may use ctrl to adjust the first decimal place (3.1) or shift to adjust 
the second (3.14)
`n* Note: ComboBoxes seem to behave naughty.
)

Gui, font, s14, tahoma
Gui, add, edit, x6 y+2 w200 r1 vbanana, 34
Gui, add, edit, xp y+2 w180 r1 vdeer, 3.96
Gui, add, edit, xp y+2 w220 r1 vdesk, -0.21
Gui, add, edit, xp y+2 w300 r1 vaaa, whales.4
Gui, add, text, xp y+2 w300 r1 vbbb, 6795
Gui, add, comboBox, xp y+2 w200 r8 vccc, 111||222|333

Gui, show
OnMessage(WM_MOUSEWHEEL:=0x20A, "wheel")
OnMessage(WM_MOUSEMOVE:=0x200, "drag")
return
;*/


/*
drag
   Allows you to click&hold then drag the mouse while an edit control is active 
   to adjust numeric values.

   wParam - DO NOT TOUCH
   lParam - Not used, but DO NOT TOUCH

   You may use ctrl to adjust the first decimal place (3.1) 
   or shift to adjust the second (3.14) decimal.

example: there is none. Just place ... OnMessage(WM_MOUSEMOVE:=0x200, "drag") ... in your code.
*/
drag(wParam, lParam)
{
   static yp,lastControl,change
   sensitivity:=7 ; In pixels, about how far should the mouse move before adjusting the value?
   amt:=1         ; How much to increase the value
   mode:=1        ; 1 = up/down, 2=left/right
   
   ; some safety checks
   if (!GetKeyState("Lbutton", "P"))      
      return
   GuiControlGet, controlType, Focus
   if (!instr(controlType, "Edit"))
      return
   GuiControlGet, value,, %A_GuiControl%
   if value is not number
      return

   if (mode=1)
      MouseGetPos,, y
   else if (mode=2)
   {
      MouseGetPos, y
      y*=-1 ; need to swap it so dragging to the right adds, not subtracts.
   }
   else
      return
      
   if (lastControl!=A_GuiControl) ; set the position to the current mouse position
      yp:=y
   change:=abs(y-yp)              ; check to see if the value is ready to be changed, has it met the sensitivity?
   
   mult:=((wParam=5) ? 0.01 : (wParam=9) ? 0.1 : 1)
   value+=((y<yp && change>=sensitivity) ? amt*mult : (y>yp && change>=sensitivity) ? -amt*mult : 0)

   GuiControl,, %A_GuiControl%, % RegExReplace(value, "(\.[1-9]+)0+$", "$1")
   
   if (change>=sensitivity)
      yp:=y
   lastControl:=A_GuiControl
}

/*
wheel
   Use MouseWheel Up and MouseWheel Down to adjust to adjust neumeric values.

   wParam - DO NOT TOUCH
   lParam - Not used, but DO NOT TOUCH

   You may use ctrl to adjust the first decimal place (3.1)
   or shift to adjust the second (3.14) decimal.

example: there is none. Just place ... OnMessage(WM_MOUSEWHEEL:=0x20A, "wheel") ... in your code.
*/
wheel(wParam, lParam)
{
   amt:=1 ; How much to increase the value
   GuiControlGet, controlType, Focus
   if (!instr(controlType, "Edit"))
      return
      
   GuiControlGet, value,, %A_GuiControl%
   if value is not number
      return
   
   mult:=((wParam & 0xffff=4) ? 0.01 : (wParam & 0xffff=8) ? 0.1 : 1)
   value+=((StrLen(wParam)>7) ? -amt*mult : amt*mult)
   GuiControl,, %A_GuiControl%, % RegExReplace(value, "(\.[1-9]+)0+$", "$1")
}


guiclose:
Esc::
    critical
    exitapp
return
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Wheel and Drag: [de]Increment Edit controls with the mou

30 Oct 2013, 09:28

Thanks!

Wish it could be an actual function and not onmessage. That way I could easily have a whitelist and/or blacklist of controls to work on.
Currently users need to add that manually to the function themself :( or I could use a global.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Wheel and Drag: [de]Increment Edit controls with the mou

30 Oct 2013, 11:01

Good Idea, I'll try that later.. :D

edit:

Code: Select all

wd_control() {
	OnMessage(WM_MOUSEWHEEL:=0x20A, "wheel")
	OnMessage(WM_MOUSEMOVE:=0x200, "drag")
}
????
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 139 guests