Lire en anglais Modifier

Partager via


TrackBarRenderer Class

Definition

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

public sealed class TrackBarRenderer
public static class TrackBarRenderer
Inheritance
TrackBarRenderer

Examples

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.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace TrackBarRendererSample
{
    class Form1 : Form
    {
        public Form1()
        {
            CustomTrackBar TrackBar1 = new CustomTrackBar(19,
                new Size(300, 50));
            this.Width = 500;
            this.Controls.Add(TrackBar1);
        }

        [STAThread]
        static void 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());
        }
    }

    class CustomTrackBar : Control
    {
        private int numberTicks = 10;
        private Rectangle trackRectangle = new Rectangle();
        private Rectangle ticksRectangle = new Rectangle();
        private Rectangle thumbRectangle = new Rectangle();
        private int currentTickPosition = 0;
        private float tickSpace = 0;
        private bool thumbClicked = false;
        private TrackBarThumbState thumbState =
            TrackBarThumbState.Normal;

        public CustomTrackBar(int ticks, Size trackBarSize)
        {
            this.Location = new Point(10, 10);
            this.Size = trackBarSize;
            this.numberTicks = ticks;
            this.BackColor = Color.DarkCyan;
            this.DoubleBuffered = true;

            // Calculate the initial sizes of the bar, 
            // thumb and ticks.
            SetupTrackBar();
        }

        // Calculate the sizes of the bar, thumb, and ticks rectangle.
        private void SetupTrackBar()
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            using (Graphics g = this.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 = ((float)ticksRectangle.Width - 1) /
                    ((float)numberTicks - 1);

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

                thumbRectangle.X = CurrentTickXCoordinate();
                thumbRectangle.Y = trackRectangle.Y - 8;
            }
        }

        private int CurrentTickXCoordinate()
        {
            if (tickSpace == 0)
            {
                return 0;
            }
            else
            {
                return ((int)Math.Round(tickSpace) *
                    currentTickPosition);
            }
        }

        // Draw the track bar.
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!TrackBarRenderer.IsSupported)
            {
                this.Parent.Text = "CustomTrackBar Disabled";
                return;
            }

            this.Parent.Text = "CustomTrackBar Enabled";
            TrackBarRenderer.DrawHorizontalTrack(e.Graphics,
                trackRectangle);
            TrackBarRenderer.DrawTopPointingThumb(e.Graphics,
                thumbRectangle, thumbState);
            TrackBarRenderer.DrawHorizontalTicks(e.Graphics,
                ticksRectangle, numberTicks, EdgeStyle.Raised);
        }

        // Determine whether the user has clicked the track bar thumb.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            if (this.thumbRectangle.Contains(e.Location))
            {
                thumbClicked = true;
                thumbState = TrackBarThumbState.Pressed;
            }

            this.Invalidate();
        }

        // Redraw the track bar thumb if the user has moved it.
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            if (thumbClicked)
            {
                if (e.Location.X > trackRectangle.X &&
                    e.Location.X < (trackRectangle.X +
                    trackRectangle.Width - thumbRectangle.Width))
                {
                    thumbClicked = false;
                    thumbState = TrackBarThumbState.Hot;
                    this.Invalidate();
                }

                thumbClicked = false;
            }
        }

        // Track cursor movements.
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            // The user is moving the thumb.
            if (thumbClicked)
            {
                // Track movements to the next tick to the right, if 
                // the cursor has moved halfway to the next tick.
                if (currentTickPosition < numberTicks - 1 &&
                    e.Location.X > CurrentTickXCoordinate() +
                    (int)(tickSpace))
                {
                    currentTickPosition++;
                }

                // Track movements to the next tick to the left, if 
                // cursor has moved halfway to the next tick.
                else if (currentTickPosition > 0 &&
                    e.Location.X < CurrentTickXCoordinate() -
                    (int)(tickSpace / 2))
                {
                    currentTickPosition--;
                }

                thumbRectangle.X = CurrentTickXCoordinate();
            }

            // The cursor is passing over the track.
            else
            {
                thumbState = thumbRectangle.Contains(e.Location) ?
                    TrackBarThumbState.Hot : TrackBarThumbState.Normal;
            }

            Invalidate();
        }
    }
}

Remarks

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.

Properties

IsSupported

Gets a value indicating whether the TrackBarRenderer class can be used to draw a track bar with visual styles.

Methods

DrawBottomPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Draws a downward-pointing track bar slider (also known as the thumb) with visual styles.

DrawHorizontalThumb(Graphics, Rectangle, TrackBarThumbState)

Draws a horizontal track bar slider (also known as the thumb) with visual styles.

DrawHorizontalTicks(Graphics, Rectangle, Int32, EdgeStyle)

Draws the specified number of horizontal track bar ticks with visual styles.

DrawHorizontalTrack(Graphics, Rectangle)

Draws the track for a horizontal track bar with visual styles.

DrawLeftPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Draws a left-pointing track bar slider (also known as the thumb) with visual styles.

DrawRightPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Draws a right-pointing track bar slider (also known as the thumb) with visual styles.

DrawTopPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Draws an upward-pointing track bar slider (also known as the thumb) with visual styles.

DrawVerticalThumb(Graphics, Rectangle, TrackBarThumbState)

Draws a vertical track bar slider (also known as the thumb) with visual styles.

DrawVerticalTicks(Graphics, Rectangle, Int32, EdgeStyle)

Draws the specified number of vertical track bar ticks with visual styles.

DrawVerticalTrack(Graphics, Rectangle)

Draws the track for a vertical track bar with visual styles.

GetBottomPointingThumbSize(Graphics, TrackBarThumbState)

Returns the size, in pixels, of the track bar slider (also known as the thumb) that points down.

GetLeftPointingThumbSize(Graphics, TrackBarThumbState)

Returns the size, in pixels, of the track bar slider (also known as the thumb) that points to the left.

GetRightPointingThumbSize(Graphics, TrackBarThumbState)

Returns the size, in pixels, of the track bar slider (also known as the thumb) that points to the right.

GetTopPointingThumbSize(Graphics, TrackBarThumbState)

Returns the size, in pixels, of the track bar slider (also known as the thumb) that points up.

Applies to

Produit Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also