Export (0) Print
Expand All

TrackBarRenderer Class

Provides methods used to render a track bar control with visual styles. This class cannot be inherited.

System.Object
  System.Windows.Forms.TrackBarRenderer

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public NotInheritable Class TrackBarRenderer

The TrackBarRenderer type exposes the following members.

  NameDescription
Public propertyStatic memberIsSupportedGets a value indicating whether the TrackBarRenderer class can be used to draw a track bar with visual styles.
Top

  NameDescription
Public methodStatic memberDrawBottomPointingThumbDraws a downward-pointing track bar slider (also known as the thumb) with visual styles.
Public methodStatic memberDrawHorizontalThumbDraws a horizontal track bar slider (also known as the thumb) with visual styles.
Public methodStatic memberDrawHorizontalTicksDraws the specified number of horizontal track bar ticks with visual styles.
Public methodStatic memberDrawHorizontalTrackDraws the track for a horizontal track bar with visual styles.
Public methodStatic memberDrawLeftPointingThumbDraws a left-pointing track bar slider (also known as the thumb) with visual styles.
Public methodStatic memberDrawRightPointingThumbDraws a right-pointing track bar slider (also known as the thumb) with visual styles.
Public methodStatic memberDrawTopPointingThumbDraws an upward-pointing track bar slider (also known as the thumb) with visual styles.
Public methodStatic memberDrawVerticalThumbDraws a vertical track bar slider (also known as the thumb) with visual styles.
Public methodStatic memberDrawVerticalTicksDraws the specified number of vertical track bar ticks with visual styles.
Public methodStatic memberDrawVerticalTrackDraws the track for a vertical track bar with visual styles.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodStatic memberGetBottomPointingThumbSizeReturns the size, in pixels, of the track bar slider (also known as the thumb) that points down.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodStatic memberGetLeftPointingThumbSizeReturns the size, in pixels, of the track bar slider (also known as the thumb) that points to the left.
Public methodStatic memberGetRightPointingThumbSizeReturns the size, in pixels, of the track bar slider (also known as the thumb) that points to the right.
Public methodStatic memberGetTopPointingThumbSizeReturns the size, in pixels, of the track bar slider (also known as the thumb) that points up.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The TrackBarRenderer class provides a set of static methods that can be used to render each part of a track bar control with the current visual style of the operating system. Rendering a control refers to drawing the user interface of a control. This is useful if you are drawing a custom control that should have the appearance of the current visual style.

If visual styles are enabled in the operating system and visual styles are applied to the client area of application windows, the methods in this class will draw the track bar with the current visual style. Otherwise, the methods in this class will throw an InvalidOperationException. To determine whether the members of this class can be used, you can check the value of the IsSupported property.

This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements exposed by the System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar class. For more information, see Rendering Controls with Visual Styles.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.

The following code example demonstrates how to create a custom control that uses the TrackBarRenderer methods to draw a horizontal track bar that responds to mouse clicks.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles


Namespace TrackBarRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Dim TrackBar1 As New CustomTrackBar(19, New Size(300, 50))
            Me.Width = 500
            Me.Controls.Add(TrackBar1)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' Note that the call to EnableVisualStyles below does 
            ' not affect whether TrackBarRenderer.IsSupported is true;  
            ' as long as visual styles are enabled by the operating system,  
            ' IsSupported is true.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub 
    End Class 

    Class CustomTrackBar
        Inherits Control
        Private numberTicks As Integer = 10
        Private trackRectangle As New Rectangle()
        Private ticksRectangle As New Rectangle()
        Private thumbRectangle As New Rectangle()
        Private currentTickPosition As Integer = 0
        Private tickSpace As Single = 0
        Private thumbClicked As Boolean = False 
        Private thumbState As TrackBarThumbState = TrackBarThumbState.Normal

        Public Sub New(ByVal ticks As Integer, ByVal trackBarSize As Size)

            With Me
                .Location = New Point(10, 10)
                .Size = trackBarSize
                .numberTicks = ticks
                .BackColor = Color.DarkCyan
                .DoubleBuffered = True 
            End With 

            ' Calculate the initial sizes of the bar,  
            ' thumb and ticks.
            SetupTrackBar()
        End Sub 

        ' Calculate the sizes of the bar, thumb, and ticks rectangle. 
        Private Sub SetupTrackBar()
            If Not TrackBarRenderer.IsSupported Then 
                Return 
            End If 
            Using g As Graphics = Me.CreateGraphics()
                ' Calculate the size of the track bar.
                trackRectangle.X = ClientRectangle.X + 2
                trackRectangle.Y = ClientRectangle.Y + 28
                trackRectangle.Width = ClientRectangle.Width - 4
                trackRectangle.Height = 4

                ' Calculate the size of the rectangle in which to  
                ' draw the ticks.
                ticksRectangle.X = trackRectangle.X + 4
                ticksRectangle.Y = trackRectangle.Y - 8
                ticksRectangle.Width = trackRectangle.Width - 8
                ticksRectangle.Height = 4

                tickSpace = (CSng(ticksRectangle.Width) - 1) / _
                    (CSng(numberTicks) - 1)

                ' Calculate the size of the thumb.
                thumbRectangle.Size = _
                    TrackBarRenderer.GetTopPointingThumbSize( _
                    g, TrackBarThumbState.Normal)

                thumbRectangle.X = CurrentTickXCoordinate()
                thumbRectangle.Y = trackRectangle.Y - 8
            End Using 
        End Sub 

        Private Function CurrentTickXCoordinate() As Integer 
            If tickSpace = 0 Then 
                Return 0
            Else 
                Return CInt(Math.Round(tickSpace)) * currentTickPosition
            End If 
        End Function 

        ' Draw the track bar. 
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            If Not TrackBarRenderer.IsSupported Then 
                Me.Parent.Text = "CustomTrackBar Disabled" 
                Return 
            End If 

            Me.Parent.Text = "CustomTrackBar Enabled"
            TrackBarRenderer.DrawHorizontalTrack(e.Graphics, _
                trackRectangle)
            TrackBarRenderer.DrawTopPointingThumb(e.Graphics, _
                thumbRectangle, thumbState)
            TrackBarRenderer.DrawHorizontalTicks(e.Graphics, _
                ticksRectangle, numberTicks, EdgeStyle.Raised)
        End Sub 

        ' Determine whether the user has clicked the track bar thumb. 
        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            If Not TrackBarRenderer.IsSupported Then 
                Return 
            End If 
            If Me.thumbRectangle.Contains(e.Location) Then
                thumbClicked = True
                thumbState = TrackBarThumbState.Pressed
            End If 

            Me.Invalidate()
        End Sub 

        ' Redraw the track bar thumb if the user has moved it. 
        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            If Not TrackBarRenderer.IsSupported Then 
                Return 
            End If 
            If thumbClicked = True Then 
                If e.Location.X > trackRectangle.X And _
                    e.Location.X < trackRectangle.X + _
                    trackRectangle.Width - thumbRectangle.Width Then

                    thumbClicked = False
                    thumbState = TrackBarThumbState.Hot
                    Me.Invalidate()
                End If
                thumbClicked = False 
            End If 
        End Sub 

        ' Track cursor movements. 
        Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
            If Not TrackBarRenderer.IsSupported Then 
                Return 
            End If 
            ' The user is moving the thumb. 
            If thumbClicked = True Then 

                ' Track movements to the next tick to the right, if the 
                ' cursor has moved halfway to the next tick. 
                If currentTickPosition < numberTicks - 1 And _
                    e.Location.X > CurrentTickXCoordinate() + _
                    CInt(tickSpace) Then
                    currentTickPosition += 1

                ' Track movements to the next tick to the left, if  
                ' the cursor has moved halfway to the next tick. 
                Else 
                    If currentTickPosition > 0 And _
                        e.Location.X < CurrentTickXCoordinate() - _
                        CInt(tickSpace / 2) Then
                        currentTickPosition -= 1
                    End If 
                End If
                thumbRectangle.X = CurrentTickXCoordinate()

            ' The cursor is passing over the track. 
            Else 
                If thumbRectangle.Contains(e.Location) Then
                    thumbState = TrackBarThumbState.Hot
                Else
                    thumbState = TrackBarThumbState.Normal
                End If 
            End If

            Invalidate()
        End Sub 

    End Class 
End Namespace

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft