VERSION 5.00
Begin VB.UserControl ctlKnob 
   AutoRedraw      =   -1  'True
   ClientHeight    =   3600
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   4800
   FillStyle       =   0  'Solid
   ScaleHeight     =   240
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   320
   Windowless      =   -1  'True
End
Attribute VB_Name = "ctlKnob"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
' Flicker free modification by Georgekar

Private Const PI        As Double = 3.14159265358979

Private mMin            As Double
Private mMax            As Double
Private mNumOfTurns     As Long
Private mValue          As Double
Private mSmallChange    As Double
Private mLargeChange    As Double

Private oldX            As Single
Private oldY            As Single
Private oldButton       As Long
Private mActive         As Boolean

Public Event Change()

Public Property Get value() As Double
    value = mValue
End Property
Public Property Let value( _
                    ByVal value As Double)
    If value < mMin Then
        value = mMin
    ElseIf value > mMax Then
        value = mMax
    End If
    
    mValue = value: UserControl.Refresh
    RaiseEvent Change
    
    PropertyChanged "Value"
End Property

Public Property Get Min() As Double
    Min = mMin
End Property
Public Property Let Min( _
                    ByVal value As Double)
    If value > mMax Then err.Raise 5
    mMin = value
    If mValue < value Then Me.value = value
    UserControl_Paint
    PropertyChanged "Min"
End Property

Public Property Get Max() As Double
    Max = mMax
End Property
Public Property Let Max( _
                    ByVal value As Double)
    If value < mMin Then err.Raise 5
    mMax = value
    If mValue > value Then mValue = value
    UserControl_Paint
    PropertyChanged "Max"
End Property

Public Property Get NumOfTurns() As Long
    NumOfTurns = mNumOfTurns
End Property
Public Property Let NumOfTurns( _
                    ByVal value As Long)
    If value < 1 Then err.Raise 5
    mNumOfTurns = value: UserControl_Paint
    PropertyChanged "NumOfTurns"
End Property

Public Property Get SmallChange() As Double
    SmallChange = mSmallChange
End Property
Public Property Let SmallChange( _
                    ByVal value As Double)
    mSmallChange = value
    PropertyChanged "SmallChange"
End Property

Public Property Get LargeChange() As Double
    LargeChange = mLargeChange
End Property
Public Property Let LargeChange( _
                    ByVal value As Double)
    mLargeChange = value
    PropertyChanged "LargeChange"
End Property

Public Property Get Enabled() As Double
    Enabled = UserControl.Enabled
End Property
Public Property Let Enabled( _
                    ByVal value As Double)

    UserControl.Enabled = value

    PropertyChanged "Enabled"
    
End Property

Private Sub UserControl_InitProperties()
    mMin = 0
    mMax = 100
    mValue = 50
    mNumOfTurns = 2
    mSmallChange = 0.1
    mLargeChange = 1
End Sub

Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    oldX = X: oldY = Y: oldButton = Button: mActive = True
End Sub

Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim delta   As Single
    Dim newVal  As Double
    
    If Not mActive Then Exit Sub
    
    delta = Y - oldY: oldY = Y
    
    If Button = vbLeftButton Then
    
        newVal = mValue - (delta * mSmallChange)

    ElseIf Button = vbRightButton Then
    
        newVal = mValue - (delta * mLargeChange)
        
    End If
        
    If newVal > mMax Then
        newVal = mMax
    ElseIf newVal < mMin Then
        newVal = mMin
    End If
    
    If mValue <> newVal Then
    
        mValue = newVal
        UserControl_Paint
        RaiseEvent Change
        
    End If
    
End Sub

Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    mActive = False
End Sub

Private Sub UserControl_Paint()
    Dim percent As Double
    Dim angle   As Double
    Dim aStart  As Double
    Dim aEnd    As Double
    Dim posX    As Single
    Dim posY    As Single
    Dim radius  As Single
    Dim posKnb  As Single
    percent = (mValue - mMin) / (mMax - mMin)
    angle = ((2 * PI) * percent) / 1.25
    posX = UserControl.ScaleWidth \ 2: posY = UserControl.ScaleHeight \ 2
    radius = IIf(posX < posY, posX, posY) - 1
    UserControl.cls
    If angle > 0 Then
        
        If angle < PI Then
            aEnd = -(angle + PI) / 2: aStart = aEnd + angle
        Else
            aEnd = -(angle + PI) / 2: aStart = -(2 * PI - (aEnd + angle))
        End If
    
        UserControl.FillColor = vbButtonShadow
        UserControl.Circle (posX, posY - 1), radius, vbButtonShadow, aStart, aEnd
        
    End If
    
    UserControl.FillColor = vb3DHighlight
    UserControl.Circle (posX, posY), radius - 4, vb3DDKShadow
    
    angle = ((2 * PI) * percent) * mNumOfTurns
    posKnb = radius - 8
    
    posX = Sin(angle) * posKnb + UserControl.ScaleWidth \ 2
    posY = -Cos(angle) * posKnb + UserControl.ScaleHeight \ 2
    
    UserControl.FillColor = vbHighlight
    UserControl.Circle (posX, posY), 3, vbHighlight
   
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    mMin = PropBag.ReadProperty("Min", 0)
    mMax = PropBag.ReadProperty("Max", 100)
    mValue = PropBag.ReadProperty("Value", 50)
    mNumOfTurns = PropBag.ReadProperty("NumOfTurns", 2)
    mSmallChange = PropBag.ReadProperty("SmallChange", 0.1)
    mLargeChange = PropBag.ReadProperty("LargeChange", 1)
    UserControl.Enabled = PropBag.ReadProperty("Enabled", True)
    UserControl.Refresh
End Sub


Private Sub UserControl_Show()
 UserControl_Paint
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "Min", mMin, 0
    PropBag.WriteProperty "Max", mMax, 100
    PropBag.WriteProperty "Value", mValue, 50
    PropBag.WriteProperty "NumOfTurns", mNumOfTurns, 2
    PropBag.WriteProperty "SmallChange", mSmallChange, 0.1
    PropBag.WriteProperty "LargeChange", mLargeChange, 1
    PropBag.WriteProperty "Enabled", UserControl.Enabled, True
End Sub

