ComboBoxRenderer (Clase)
Actualización: noviembre 2007
Proporciona métodos que se utilizan para representar un control de cuadro combinado con estilos visuales. Esta clase no se puede heredar.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
La clase ComboBoxRenderer proporciona un conjunto de métodos static que se pueden utilizar para representar un control de cuadro combinado con el estilo visual actual del sistema operativo. Representar un control significa diseñar la interfaz de usuario de un control. Esto resulta útil si pretende dibujar un control personalizado que tenga el aspecto del estilo visual actual. Para dibujar un cuadro combinado, utilice el método DrawTextBox para dibujar el cuadro de texto y el método DrawDropDownButton para dibujar la flecha de lista desplegable.
Si los estilos visuales están habilitados en el sistema operativo y se aplican al área de cliente de las ventanas de la aplicación, los métodos DrawTextBox y DrawDropDownButton dibujarán el cuadro combinado con el estilo visual actual. De lo contrario, estos métodos producirán una excepción InvalidOperationException. Para determinar si pueden utilizarse los miembros de esta clase, puede comprobar el valor de la propiedad IsSupported.
Esta clase ajusta la funcionalidad de un control System.Windows.Forms.VisualStyles.VisualStyleRenderer cuyo valor está establecido en uno de los elementos expuestos por las clases System.Windows.Forms.VisualStyles.VisualStyleElement.ComboBox.DropDownButton y System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox.TextEdit. Para obtener más información, vea Representar controles con estilos visuales.
Nota de la plataforma Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003:
Los estilos visuales sólo se admiten en estas plataformas.
En el ejemplo de código siguiente se muestra cómo se crea un control personalizado que utiliza los métodos DrawTextBox y DrawDropDownButton para dibujar un cuadro combinado que responde a los clics del mouse (ratón).
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(); } } } }
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.