Rechercher dans ce blog

mercredi 29 décembre 2021

Trackbar custom

 Bonjour, je vous montre comment réaliser un trackbar custom.

avec 3 mode : rectangle,cercle,thumb avec une couleur ou deux couleurs.

 

voici ma classes :

Public Class TrackbarCustom : Inherits Control

    Private _ThumbTwoColor As Boolean
    Public Property ThumbTwoColor As Boolean
        Get
            Return _ThumbTwoColor
        End Get
        Set(value As Boolean)
            _ThumbTwoColor = value
            Me.Invalidate()
        End Set
    End Property

    Private _minimum As Integer
    Public Property Minimum() As Integer
        Get
            Return _minimum
        End Get
        Set(ByVal value As Integer)
            _minimum = value

            If value > _maximum Then _maximum = value
            If value > _value Then _value = value

            Invalidate()
        End Set
    End Property

    Private _maximum As Integer
    Public Property Maximum() As Integer
        Get
            Return _maximum
        End Get
        Set(ByVal value As Integer)
            _maximum = value

            If value < _minimum Then _minimum = value
            If value < _value Then _value = value

            Invalidate()
        End Set
    End Property

    Event ValueChanged()
    Private _value As Integer
    Public Property Value() As Integer
        Get
            Return _value
        End Get
        Set(ByVal value As Integer)
            If value < _minimum Then
                _value = _minimum
            ElseIf value > _maximum Then
                _value = _maximum
            Else
                _value = value
            End If

            Invalidate()
            RaiseEvent ValueChanged()
        End Set
    End Property

    Private _trackbar As TypeTrackBar
    Public Property MyCuseur As TypeTrackBar
        Get
            Return _trackbar
        End Get
        Set(value As TypeTrackBar)
            _trackbar = value
            Me.Invalidate()
        End Set
    End Property

    Private _FillColor As Color = Color.White
    Public Property FillColor As Color
        Get
            Return _FillColor
        End Get
        Set(value As Color)
            If value = Color.Transparent Then
                Return
            Else
                _FillColor = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _LineColor As Color = Color.Gray
    Public Property LineColor As Color
        Get
            Return _LineColor
        End Get
        Set(value As Color)
            If value = Color.Transparent Then
                Return
            Else
                _LineColor = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _CuseurColor2 As Color = Color.White
    Public Property CuseurColor2 As Color
        Get
            Return _CuseurColor2
        End Get
        Set(value As Color)
            _CuseurColor2 = value
            Me.Invalidate()
        End Set
    End Property

    Private _CuseurColor As Color = Color.DarkBlue
    Public Property CuseurColor As Color
        Get
            Return _CuseurColor
        End Get
        Set(value As Color)
            If value = Color.Transparent Then
                Return
            Else
                _CuseurColor = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private Rect As New Rectangle
    Private Moving As Boolean = False

    Public Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
        Me.Size = New Size(210, 50)
        Me._trackbar = TypeTrackBar.Rectangle
        Me._maximum = 100
        Me._minimum = 0
        Me._ThumbTwoColor = True
    End Sub

    Protected Overrides Sub OnResize(ByVal e As EventArgs)
        MyBase.OnResize(e)
        If Me.Height <> 50 Then
            Me.Height = 50
        ElseIf Me.Width < 200 Then
            Me.Width = 200
        End If
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        Rect = New Rectangle(0, 0, Me.Width, Me.Height)
        e.Graphics.FillRectangle(New SolidBrush(_FillColor), New Rectangle(0, 0, Rect.Width - 1, Rect.Height - 1))
        e.Graphics.DrawLine(New Pen(New SolidBrush(_LineColor), 5), New Point(5, 10), New Point(Rect.Width - 5, 10))
        Select Case _trackbar
            Case TypeTrackBar.Rectangle
                e.Graphics.FillRectangle(New SolidBrush(_CuseurColor), New Rectangle(Rect.X + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0, 15, 25))
                If _ThumbTwoColor Then
                    e.Graphics.FillRectangle(New SolidBrush(_CuseurColor2), New Rectangle(Rect.X + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 2, 15, 20))
                End If
            Case TypeTrackBar.Circle
                e.Graphics.FillEllipse(New SolidBrush(_CuseurColor), (Rect.X - 1) + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0, 20, 20)
                If _ThumbTwoColor Then
                    e.Graphics.FillEllipse(New SolidBrush(_CuseurColor2), (Rect.X - 1) + 2 + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 2, 16, 16)
                End If
            Case TypeTrackBar.CustomThumb
                Dim size As New Size((Rect.X + 15) + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), Rect.Height - 20)
                e.Graphics.FillPolygon(New SolidBrush(_CuseurColor), New Point() {New Point(size.Width - 15, 1), New Point(size.Width, 1), New Point(size.Width, size.Height - 12), New Point((size.Width - 7), size.Height - 6), New Point(size.Width - 15, size.Height - 12)})
                If _ThumbTwoColor Then
                    e.Graphics.FillPolygon(New SolidBrush(_CuseurColor2), New Point() {New Point(size.Width - 15, 4), New Point(size.Width, 4), New Point(size.Width, size.Height - 16), New Point((size.Width - 7), size.Height - 7), New Point(size.Width - 15, size.Height - 16)})
                End If
        End Select
        e.Graphics.DrawLine(New Pen(New SolidBrush(_CuseurColor), 5), New Point(0, 10), New Point(CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 10))
        DrawLine(e.Graphics, Rect)
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        If Rect.Contains(e.Location) Then
            Dim mp = New Rectangle(New Point(e.Location.X, e.Location.Y), New Size(1, 1))
            If New Rectangle(New Point(Rect.X + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0), New Size(Rect.Width, Rect.Height)).IntersectsWith(mp) Then
                Moving = True
            End If
        End If
    End Sub
    Protected Overrides Sub OnMouseup(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)
        Moving = False
    End Sub
    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        If Moving Then
            Dim mp As Point = New Point(e.X, e.Y)
            Value = CInt(Maximum * ((mp.X - Rect.X) / Rect.Width))
        End If
    End Sub

    Public Enum TypeTrackBar
        Rectangle = 0
        Circle = 1
        CustomThumb = 2
    End Enum

    Private Sub DrawLine(g As Graphics, Rect As Rectangle)
        Dim p1 As Point = New Point(CInt(Rect.X + 8), Rect.Height / 2)
        Dim p2 As Point = New Point(CInt(Rect.Width - 12), Rect.Height / 2)
        Dim dx As Single = (p2.X - p1.X) / (Maximum - Minimum)
        g.DrawLine(New Pen(Brushes.Black, 1), p1, p2)
        For i As Integer = Minimum To Maximum
            If i Mod 10 = 0 Then
                g.DrawLine((New Pen(Brushes.Black, 1)), CInt((Rect.X + 8) + i * dx), CInt(Rect.Height / 2), CInt((Rect.X + 8) + i * dx), CInt(Rect.Height / 1.5))
                g.DrawString(i.ToString, Me.Font, Brushes.Black, New Point(CInt((Rect.X + 8) + i * dx), Rect.Height / 1.5), New StringFormat With {.Alignment = StringAlignment.Center})
            End If
        Next
    End Sub
End Class


Aucun commentaire:

Enregistrer un commentaire