Code: Select all
SetBatchLines, -1
slider1 := new MySlider("h", 400, 15, 70, "Lime", "Fuchsia", 10, 1)
Gui, % slider1.hWnd ":Show", x300 y350
slider2 := new MySlider("v", 400, 17, 70, "Silver", "Red", 50) ; without tooltip smoother motion
Gui, % slider2.hWnd ":Show", x800 y150
Return
Esc::ExitApp
class MySlider
{
__New(orientation, len, height, thumbWidth, backColor, thumbColor, startPosPercent := 0, tooltipID := false) {
static styles := "+" . (WS_CLIPCHILDREN := 0x2000000)
. " +E" . (WS_EX_COMPOSITED := 0x2000000)
this.vert := !!InStr(orientation, "v")
this.height := height
this._thumb := thumbWidth
dpi := A_ScreenDPI/96
this.thumb := thumbWidth*dpi
this.size := (len - thumbWidth)*dpi
this.tooltipID := tooltipID
Gui, New, +hwndhBack +ToolWindow -Caption +AlwaysOnTop %styles%
this.hWnd := hBack
Gui, Color, % backColor
Gui, New, +hwndhThumb +ToolWindow -Caption +Parent%hBack%
this.hThumb := hThumb
Gui, Color, % thumbColor
this.pos := Round( this.size/100*startPosPercent )
Gui, Show, % this.BuildCoords()
coords := this.vert ? "w" . height . " h" . len : "w" . len . " h" . height
Gui, %hBack%: Show, Hide %coords%
self := this.Clone(), self.self := true
this.onClick := this.WM_LBUTTONDOWN.Bind(self)
this.onWheel := this.WM_MOUSEWHEEL.Bind(self)
OnMessage(0x201, this.onClick)
OnMessage(0x20A, this.onWheel)
}
__Delete() {
if this.self
Return
Gui, % this.hThumb ": Destroy"
Gui, % this.hWnd ": Destroy"
OnMessage(0x201, this.onClick, 0)
OnMessage(0x20A, this.onWheel, 0)
}
WM_LBUTTONDOWN() {
static newPos
if !GetKeyState("LButton", "P")
Return
prevCMM := A_CoordModeMouse
CoordMode, Mouse, Screen
MouseGetPos, X_Mouse, Y_Mouse, hwnd, hCtrl, 2
WinGetPos, X_Gui, Y_Gui,,, % "ahk_id" this.hWnd
prevX := X_Mouse - X_Gui
prevY := Y_Mouse - Y_Gui
if (hCtrl = "" && hwnd = this.hWnd) {
if this.vert
this.pos := this.size - prevY + this.thumb//2
else
this.pos := prevX - this.thumb//2
this.PosInRange()
Gui, % this.hThumb ": Show", % this.BuildCoords()
hCtrl := this.hThumb
}
if (hCtrl = this.hThumb) {
prevPos := this.pos
while GetKeyState("LButton", "P") {
MouseGetPos, X_Mouse, Y_Mouse
X := X_Mouse - X_Gui
Y := Y_Mouse - Y_Gui
if this.vert
this.pos := prevPos - Y + prevY
else
this.pos := prevPos + X - prevX
this.PosInRange()
try Gui, % this.hThumb ": Show", % this.BuildCoords()
if this.tooltipID
ToolTip % Round(this.pos*100/this.size),,, this.tooltipID
Sleep, 10
}
if this.tooltipID
ToolTip,,,, this.tooltipID
}
CoordMode, Mouse, % prevCMM
}
WM_MOUSEWHEEL(wp, lp, msg, hwnd) {
if !(WinActive("ahk_id" this.hWnd) && hwnd = this.hWnd)
Return
dir := (wp >> 16) & 0xFFFF = 120 ? 1 : -1
this.pos += dir*this.thumb//2
this.PosInRange()
Gui, % this.hThumb ": Show", % this.BuildCoords()
}
BuildCoords() {
Return "NA " . ( this.vert ? "x0 y" this.size - this.pos " w" this.height " h" this._thumb
: "x" this.pos " y0 w" this._thumb " h" this.height )
}
PosInRange() {
Return this.pos := this.pos < 0 ? 0 : this.pos > this.size ? this.size : this.pos
}
}