Leer en inglés

Compartir a través de


TrackBarRenderer Clase

Definición

Proporciona métodos que se utilizan para representar un control de barra de seguimiento con estilos visuales. Esta clase no puede heredarse.

C#
public sealed class TrackBarRenderer
C#
public static class TrackBarRenderer
Herencia
TrackBarRenderer

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear un control personalizado que use los TrackBarRenderer métodos para dibujar una barra de seguimiento horizontal que responda a los clics del mouse.

C#
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 == true)
            {
                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 == true)
            {
                // 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();
        }
    }
}

Comentarios

La TrackBarRenderer clase proporciona un conjunto de static métodos que se pueden usar para representar cada parte de un control de barra de seguimiento con el estilo visual actual del sistema operativo. La representación de un control hace referencia al hecho de dibujar la interfaz de usuario de un control. Esto resulta útil si está dibujando un control personalizado que debe tener la apariencia del estilo visual actual.

Si los estilos visuales están habilitados en el sistema operativo y los estilos visuales se aplican al área cliente de las ventanas de la aplicación, los métodos de esta clase dibujarán la barra de seguimiento con el estilo visual actual. De lo contrario, los métodos de esta clase producirán una InvalidOperationExceptionexcepción . Para determinar si se pueden usar los miembros de esta clase, puede comprobar el valor de la IsSupported propiedad .

Esta clase ajusta la funcionalidad de un System.Windows.Forms.VisualStyles.VisualStyleRenderer objeto que se establece en uno de los elementos expuestos por la System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar clase . Para obtener más información, vea Rendering Controls with Visual Styles.

Propiedades

IsSupported

Obtiene un valor que indica si la clase TrackBarRenderer puede utilizarse para dibujar una barra de seguimiento con estilos visuales.

Métodos

DrawBottomPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Dibuja un control deslizante de barra de seguimiento descendente (denominado también control de posición) con estilos visuales.

DrawHorizontalThumb(Graphics, Rectangle, TrackBarThumbState)

Dibuja un control deslizante de barra de seguimiento horizontal (denominado también control de posición) con estilos visuales.

DrawHorizontalTicks(Graphics, Rectangle, Int32, EdgeStyle)

Dibuja el número especificado de pasos de la barra de seguimiento horizontal con estilos visuales.

DrawHorizontalTrack(Graphics, Rectangle)

Dibuja la trayectoria de una barra de seguimiento horizontal con estilos visuales.

DrawLeftPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Dibuja un control deslizante de barra de seguimiento con desplazamiento hacia la izquierda (denominado también control de posición) con estilos visuales.

DrawRightPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Dibuja un control deslizante de barra de seguimiento con desplazamiento hacia la derecha (denominado también control de posición) con estilos visuales.

DrawTopPointingThumb(Graphics, Rectangle, TrackBarThumbState)

Dibuja un control deslizante de barra de seguimiento ascendente (denominado también control de posición) con estilos visuales.

DrawVerticalThumb(Graphics, Rectangle, TrackBarThumbState)

Dibuja un control deslizante de barra de seguimiento vertical (denominado también control de posición) con estilos visuales.

DrawVerticalTicks(Graphics, Rectangle, Int32, EdgeStyle)

Dibuja el número especificado de pasos de la barra de seguimiento vertical con estilos visuales.

DrawVerticalTrack(Graphics, Rectangle)

Dibuja la trayectoria de una barra de seguimiento vertical con estilos visuales.

GetBottomPointingThumbSize(Graphics, TrackBarThumbState)

Devuelve el tamaño, en píxeles, del control deslizante de barra de seguimiento (denominado también control de posición) que señala hacia abajo.

GetLeftPointingThumbSize(Graphics, TrackBarThumbState)

Devuelve el tamaño, en píxeles, del control deslizante de barra de seguimiento (denominado también control de posición) que señala hacia la izquierda.

GetRightPointingThumbSize(Graphics, TrackBarThumbState)

Devuelve el tamaño, en píxeles, del control deslizante de barra de seguimiento (denominado también control de posición) que señala hacia la derecha.

GetTopPointingThumbSize(Graphics, TrackBarThumbState)

Devuelve el tamaño, en píxeles, del control deslizante de barra de seguimiento (denominado también control de posición) que señala hacia arriba.

Se aplica a

Producto Versiones
.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
Windows Desktop 3.0, 3.1, 5, 6, 7

Consulte también