Leer en inglés

Compartir a través de


TabRenderer Clase

Definición

Proporciona métodos para representar un control de ficha con estilos visuales. Esta clase no puede heredarse.

C#
public sealed class TabRenderer
C#
public static class TabRenderer
Herencia
TabRenderer

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear un control personalizado que use los DrawTabPage métodos y DrawTabItem para dibujar un control de pestaña básico con dos pestañas.

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

namespace TabRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            CustomTabControl Tab1 = new CustomTabControl();
            Controls.Add(Tab1);
            this.Size = new Size(500, 500);
        }

        [STAThread]
        static void Main()
        {
            // The call to EnableVisualStyles below does not affect whether 
            // TabRenderer.IsSupported is true; as long as visual styles 
            // are enabled by the operating system, IsSupported is true.
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

    public class CustomTabControl : Control
    {
        private Rectangle tabPageRectangle;
        private Rectangle tabItemRectangle1;
        private Rectangle tabItemRectangle2;
        private int tabHeight = 30;
        private int tabWidth = 100;
        private TabItemState tab1State = TabItemState.Selected;
        private TabItemState tab2State = TabItemState.Normal;
        private string tab1Text = "Tab 1";
        private string tab2Text = "Tab 2";
        private bool tab1Focused = true;
        private bool tab2Focused = false;

        public CustomTabControl()
            : base()
        {
            this.Size = new Size(300, 300);
            this.Location = new Point(10, 10);
            this.Font = SystemFonts.IconTitleFont;
            this.Text = "TabRenderer";
            this.DoubleBuffered = true;

            tabPageRectangle = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y + tabHeight,
                ClientRectangle.Width,
                ClientRectangle.Height - tabHeight);

            // Extend the first tab rectangle down by 2 pixels, 
            // because it is selected by default.
            tabItemRectangle1 = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y, tabWidth, tabHeight + 2);

            tabItemRectangle2 = new Rectangle(ClientRectangle.Location.X +
                tabWidth, ClientRectangle.Location.Y, tabWidth, tabHeight);
        }

        // Draw the tab page and the tab items.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (!TabRenderer.IsSupported)
            {
                this.Parent.Text = "CustomTabControl Disabled";
                return;
            }

            TabRenderer.DrawTabPage(e.Graphics, tabPageRectangle);
            TabRenderer.DrawTabItem(e.Graphics, tabItemRectangle1,
                tab1Text, this.Font, tab1Focused, tab1State);
            TabRenderer.DrawTabItem(e.Graphics, tabItemRectangle2,
                tab2Text, this.Font, tab2Focused, tab2State);

            this.Parent.Text = "CustomTabControl Enabled";
        }

        // Draw the selected tab item.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (!TabRenderer.IsSupported)
                return;

            // The first tab is clicked. Note that the height of the 
            // selected tab rectangle is raised by 2, so that it draws 
            // over the border of the tab page.
            if (tabItemRectangle1.Contains(e.Location))
            {
                tab1State = TabItemState.Selected;
                tab2State = TabItemState.Normal;
                tabItemRectangle1.Height += 2;
                tabItemRectangle2.Height -= 2;
                tab1Focused = true;
                tab2Focused = false;
            }

            // The second tab is clicked.
            if (tabItemRectangle2.Contains(e.Location))
            {
                tab2State = TabItemState.Selected;
                tab1State = TabItemState.Normal;
                tabItemRectangle2.Height += 2;
                tabItemRectangle1.Height -= 2;
                tab2Focused = true;
                tab1Focused = false;
            }

            Invalidate();
        }
    }
}

Comentarios

La TabRenderer clase proporciona un conjunto de static métodos que se pueden usar para representar un control de pestaña 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. Para dibujar un control de pestaña, use el DrawTabPage método para dibujar la página y use el DrawTabItem método para dibujar cada pestaña.

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 el control tab con el estilo visual actual. De lo contrario, los métodos y las propiedades de esta clase producirán un InvalidOperationException. Para determinar si se pueden usar los miembros de esta clase, compruebe 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.Tab clase . Para obtener más información, vea Rendering Controls with Visual Styles(Controles de representación con estilos visuales).

Propiedades

IsSupported

Obtiene un valor que indica si la clase TabRenderer puede utilizarse para dibujar un control de ficha con estilos visuales.

Métodos

DrawTabItem(Graphics, Rectangle, Boolean, TabItemState)

Dibuja una ficha en el estado y con los límites especificados, y con un rectángulo de foco opcional.

DrawTabItem(Graphics, Rectangle, Image, Rectangle, Boolean, TabItemState)

Dibuja una ficha en el estado y con los límites especificados, con la imagen especificada y con un rectángulo de foco opcional.

DrawTabItem(Graphics, Rectangle, String, Font, Boolean, TabItemState)

Dibuja una ficha en el estado y con los límites especificados, con el texto especificado y con un rectángulo de foco opcional.

DrawTabItem(Graphics, Rectangle, String, Font, Image, Rectangle, Boolean, TabItemState)

Dibuja una ficha en el estado y con los límites especificados, con la imagen y el texto especificados, y con un rectángulo de foco opcional.

DrawTabItem(Graphics, Rectangle, String, Font, TabItemState)

Dibuja una ficha en el estado y con los límites especificados, y con el texto indicado.

DrawTabItem(Graphics, Rectangle, String, Font, TextFormatFlags, Boolean, TabItemState)

Dibuja una ficha en el estado y con los límites especificados, con el texto y el formato de texto especificados, y con un rectángulo de foco opcional.

DrawTabItem(Graphics, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, TabItemState)

Dibuja una ficha en el estado y con los límites especificados, con el texto, el formato de texto y la imagen especificados, y con un rectángulo de foco opcional.

DrawTabItem(Graphics, Rectangle, TabItemState)

Dibuja una ficha en el estado y con los límites especificados.

DrawTabPage(Graphics, Rectangle)

Dibuja una página de fichas en los límites especificados.

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