Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

javaSpy

Browsing ctrlTrackBar.vb (8.98 KB)

Option Explicit On

Imports System.ComponentModel

Public Class ctrlTrackBar

    Inherits System.Windows.Forms.TrackBar

    Private ThumbHot As Boolean = False
    Private ThumbDown As Boolean = False
    Private TickRectTop As Rectangle
    Private TickRectBottom As Rectangle
    Private TrackRect As Rectangle
    Private ThumbRect As Rectangle

    Public Sub New()

        MyBase.New()

        Me.TickStyle = Windows.Forms.TickStyle.BottomRight
        Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)
        Me.SetStyle(ControlStyles.EnableNotifyMessage, True)
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.UpdateStyles()

    End Sub

    <Browsable(True), Category("Appearance"), DefaultValue(TickStyle.BottomRight)> _
    Shadows Property TickStyle() As TickStyle

        Get
            Return MyBase.TickStyle
        End Get

        Set(ByVal value As TickStyle)
            MyBase.TickStyle = value
            Me.OnResize(Nothing)
        End Set

    End Property

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)

        MyBase.OnResize(e)

        If VisualStyles.VisualStyleRenderer.IsSupported Then

            TrackRect.X = 8
            TrackRect.Width = Me.ClientRectangle.Width - 16
            TrackRect.Height = 4

            If Me.TickStyle = Windows.Forms.TickStyle.Both Or Me.TickStyle = Windows.Forms.TickStyle.TopLeft Then
                TrackRect.Y = 19
            Else
                TrackRect.Y = 10
            End If

            ThumbRect.Width = 10
            ThumbRect.Height = 22
            ThumbRect.Y = TrackRect.Y - 8
            ThumbRect.X = TrackRect.X

            If Not Me.TickStyle = Windows.Forms.TickStyle.None Then

                TickRectTop.Height = 4
                TickRectTop.X = TrackRect.X
                TickRectTop.Width = TrackRect.Width
                TickRectTop.Y = 6

                TickRectBottom.Height = 4
                TickRectBottom.X = TrackRect.X
                TickRectBottom.Width = TrackRect.Width
                TickRectBottom.Y = ThumbRect.Bottom + 1

            End If

        End If

    End Sub

    Protected Overrides Sub OnValueChanged(ByVal e As System.EventArgs)

        MyBase.OnValueChanged(e)

        If VisualStyles.VisualStyleRenderer.IsSupported Then

            'CALCULATE WHERE THE THUMB SHOULD BE
            Dim ValuePerPixel As Double = (Me.Maximum - Me.Minimum) / (Me.ClientRectangle.Width - 26)
            Me.ThumbRect.X = CInt(13 + (Me.Value / ValuePerPixel) - (ThumbRect.Width / 2) - (Me.Minimum / ValuePerPixel))
            Me.Invalidate()

        End If

    End Sub

    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)

        MyBase.OnMouseMove(e)

        If VisualStyles.VisualStyleRenderer.IsSupported Then

            If Not Me.Focused Then

                'HIGHLIGHT THE THUMB IF WE MOVE OVER IT
                If Me.ThumbRect.Contains(e.X, e.Y) Then
                    Me.ThumbHot = True
                Else
                    Me.ThumbHot = False
                End If

                Me.Invalidate()

            End If

        End If

    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)

        MyBase.OnMouseDown(e)

        If VisualStyles.VisualStyleRenderer.IsSupported Then

            'DE-HIGHLIGHT THE THUMB IF WE PESS THE MOUSE BUTTON OVER IT
            If Me.ThumbRect.Contains(e.X, e.Y) Then
                Me.ThumbDown = True
            Else
                Me.ThumbDown = False
            End If

            Me.Invalidate()

        End If

    End Sub

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)

        MyBase.OnMouseUp(e)

        If VisualStyles.VisualStyleRenderer.IsSupported Then
            Me.ThumbDown = False
            Me.Invalidate()
        End If

    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        Try

            If VisualStyles.VisualStyleRenderer.IsSupported Then

                Dim vs As VisualStyles.VisualStyleRenderer

                'DRAW THE TRACK
                vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.Track.Normal)
                vs.DrawBackground(e.Graphics, Me.TrackRect)

                'DRAW THE TICKS
                If Me.TickStyle <> Windows.Forms.TickStyle.None Then

                    Dim pt(1) As Point
                    Dim ValuePerPixel As Double = (Me.Maximum - Me.Minimum) / (Me.ClientRectangle.Width - 26)
                    Dim StartPoint As Integer = CInt(13 - (Me.Minimum / ValuePerPixel))
                    Dim pn As New Pen(SystemColors.WindowText)

                    If Me.TickStyle = Windows.Forms.TickStyle.TopLeft Or Me.TickStyle = Windows.Forms.TickStyle.Both Then
                        pt(1).Y = Me.TickRectTop.Bottom
                        For i As Integer = Me.Minimum To Me.Maximum Step Me.TickFrequency
                            If i > Me.Minimum And i < Me.Maximum Then
                                pt(0).Y = Me.TickRectTop.Bottom - 2
                            Else
                                pt(0).Y = Me.TickRectTop.Y
                            End If
                            pt(0).X = StartPoint + CInt(i / ValuePerPixel)
                            pt(1).X = StartPoint + CInt(i / ValuePerPixel)
                            e.Graphics.DrawLine(pn, pt(0), pt(1))
                        Next
                    End If

                    If Me.TickStyle = Windows.Forms.TickStyle.BottomRight Or Me.TickStyle = Windows.Forms.TickStyle.Both Then
                        pt(0).Y = Me.TickRectBottom.Y
                        For i As Integer = Me.Minimum To Me.Maximum Step Me.TickFrequency
                            If i > Me.Minimum And i < Me.Maximum Then
                                pt(1).Y = Me.TickRectBottom.Y + 2
                            Else
                                pt(1).Y = Me.TickRectBottom.Bottom
                            End If
                            pt(0).X = StartPoint + CInt(i / ValuePerPixel)
                            pt(1).X = StartPoint + CInt(i / ValuePerPixel)
                            e.Graphics.DrawLine(pn, pt(0), pt(1))
                        Next
                    End If

                    pn.Dispose()

                End If

                'DRAW THE THUMB
                Select Case Me.TickStyle

                    Case Windows.Forms.TickStyle.BottomRight

                        If Me.Enabled Then
                            If (Me.Focused Or Me.ThumbHot) And Not Me.ThumbDown Then
                                vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.ThumbBottom.Hot)
                            Else
                                vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.ThumbBottom.Normal)
                            End If
                        Else
                            vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.ThumbBottom.Disabled)
                        End If

                    Case Windows.Forms.TickStyle.TopLeft

                        If Me.Enabled Then
                            If (Me.Focused Or Me.ThumbHot) And Not Me.ThumbDown Then
                                vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.ThumbTop.Hot)
                            Else
                                vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.ThumbTop.Normal)
                            End If
                        Else
                            vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.ThumbTop.Disabled)
                        End If

                    Case Else

                        If Me.Enabled Then
                            If (Me.Focused Or Me.ThumbHot) And Not Me.ThumbDown Then
                                vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.Thumb.Hot)
                            Else
                                vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.Thumb.Normal)
                            End If
                        Else
                            vs = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.TrackBar.Thumb.Disabled)
                        End If

                End Select

                vs.DrawBackground(e.Graphics, Me.ThumbRect)

            Else

                MyBase.OnPaint(e)

            End If

        Catch
        End Try

    End Sub

End Class

Download ctrlTrackBar.vb

Back to file list


Back to project page