Leer en inglés

Compartir a través de


ComboBoxRenderer Clase

Definición

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

C#
public sealed class ComboBoxRenderer
C#
public static class ComboBoxRenderer
Herencia
ComboBoxRenderer

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear un control personalizado que use los DrawTextBox métodos y DrawDropDownButton para dibujar un cuadro combinado que responda a los clics del mouse.

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

namespace ComboBoxRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            this.Size = new Size(300, 300);
            CustomComboBox ComboBox1 = new CustomComboBox();
            Controls.Add(ComboBox1);
        }

        [STAThread]
        static void Main()
        {
            // The call to EnableVisualStyles below does not affect
            // whether ComboBoxRenderer.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 CustomComboBox : Control
    {
        private Size arrowSize;
        private Rectangle arrowRectangle;
        private Rectangle topTextBoxRectangle;
        private Rectangle bottomTextBoxRectangle;
        private ComboBoxState textBoxState = ComboBoxState.Normal;
        private ComboBoxState arrowState = ComboBoxState.Normal;
        private String bottomText = "Using ComboBoxRenderer";
        private bool isActivated = false;
        private const int minHeight = 38;
        private const int minWidth = 40;

        public CustomComboBox()
            : base()
        {
            this.Location = new Point(10, 10);
            this.Size = new Size(140, 38);
            this.Font = SystemFonts.IconTitleFont;
            this.Text = "Click the button";

            // Initialize the rectangles to look like the standard combo 
            // box control.
            arrowSize = new Size(18, 20);
            arrowRectangle = new Rectangle(ClientRectangle.X +
                ClientRectangle.Width - arrowSize.Width - 1,
                ClientRectangle.Y + 1,
                arrowSize.Width,
                arrowSize.Height);
            topTextBoxRectangle = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y,
                ClientRectangle.Width,
                arrowSize.Height + 2);
            bottomTextBoxRectangle = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y + topTextBoxRectangle.Height,
                ClientRectangle.Width,
                topTextBoxRectangle.Height - 6);
        }

        // Draw the combo box in the current state.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (!ComboBoxRenderer.IsSupported)
            {
                this.Parent.Text = "Visual Styles Disabled";
                return;
            }

            this.Parent.Text = "CustomComboBox Enabled";

            // Always draw the main text box and drop down arrow in their 
            // current states
            ComboBoxRenderer.DrawTextBox(e.Graphics, topTextBoxRectangle,
                this.Text, this.Font, textBoxState);
            ComboBoxRenderer.DrawDropDownButton(e.Graphics, arrowRectangle,
                arrowState);

            // Only draw the bottom text box if the arrow has been clicked
            if (isActivated)
            {
                ComboBoxRenderer.DrawTextBox(e.Graphics,
                    bottomTextBoxRectangle, bottomText, this.Font,
                    textBoxState);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            // Check whether the user clicked the arrow.
            if (arrowRectangle.Contains(e.Location) &&
                ComboBoxRenderer.IsSupported)
            {
                // Draw the arrow in the pressed state.
                arrowState = ComboBoxState.Pressed;

                // The user has activated the combo box.
                if (!isActivated)
                {
                    this.Text = "Clicked!";
                    textBoxState = ComboBoxState.Pressed;
                    isActivated = true;
                }

                // The user has deactivated the combo box.
                else
                {
                    this.Text = "Click here";
                    textBoxState = ComboBoxState.Normal;
                    isActivated = false;
                }

                // Redraw the control.
                Invalidate();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (arrowRectangle.Contains(e.Location) &&
                ComboBoxRenderer.IsSupported)
            {
                arrowState = ComboBoxState.Normal;
                Invalidate();
            }
        }
    }
}

Comentarios

La ComboBoxRenderer clase proporciona un conjunto de static métodos que se pueden usar para representar un control de cuadro combinado 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 cuadro combinado, use el DrawTextBox método para dibujar el cuadro de texto y el DrawDropDownButton método para dibujar la flecha desplegable.

Si los estilos visuales están habilitados en el sistema operativo y los estilos visuales se aplican al área cliente de ventanas DrawTextBox de la aplicación y DrawDropDownButton dibujarán el cuadro combinado con el estilo visual actual. De lo contrario, estos métodos 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 las System.Windows.Forms.VisualStyles.VisualStyleElement.ComboBox.DropDownButton clases y System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox.TextEdit . 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 ComboBoxRenderer puede utilizarse para dibujar un cuadro combinado con estilos visuales.

Métodos

DrawDropDownButton(Graphics, Rectangle, ComboBoxState)

Dibuja una flecha desplegable con el estilo visual actual del sistema operativo.

DrawTextBox(Graphics, Rectangle, ComboBoxState)

Dibuja un cuadro de texto en el estado especificado y con los límites indicados.

DrawTextBox(Graphics, Rectangle, String, Font, ComboBoxState)

Dibuja un cuadro de texto en el estado especificado y con los límites indicados, junto con el texto especificado.

DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, ComboBoxState)

Dibuja un cuadro de texto en el estado especificado y con los límites indicados, junto con el texto y los límites de texto especificados.

DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, TextFormatFlags, ComboBoxState)

Dibuja un cuadro de texto en el estado especificado y con los límites indicados, junto con el texto, el formato de texto y los límites de texto especificados.

DrawTextBox(Graphics, Rectangle, String, Font, TextFormatFlags, ComboBoxState)

Dibuja un cuadro de texto en el estado especificado y con los límites indicados, junto con el texto y el formato de texto 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