CheckBoxRenderer (Clase)
Actualización: noviembre 2007
Proporciona métodos utilizados para representar un control de casilla de verificación con o sin estilos visuales. Esta clase no se puede heredar.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
La clase CheckBoxRenderer proporciona un conjunto de métodos static que se pueden usar para representar un control de casilla de verificación. Representar un control se refiere a dibujar la interfaz de usuario de un control. Para dibujar una casilla de verificación, use uno de los métodos DrawCheckBox. Estos métodos proporcionan varias opciones, como dibujar texto o una imagen con la casilla de verificación.
Si los estilos visuales están habilitados en el sistema operativo y se aplican a la aplicación actual, DrawCheckBox dibujará la casilla de verificación con el estilo visual actual. En caso contrario, DrawCheckBox dibujará la casilla de verificación con el estilo clásico de Windows. Esto resulta útil si dibuja un control personalizado que debe coincidir automáticamente con la actual configuración de estilos visuales del sistema operativo.
Esta clase ajusta la funcionalidad de System.Windows.Forms.VisualStyles.VisualStyleRenderer cuyo valor está establecido en uno de los elementos expuestos por la clase System.Windows.Forms.VisualStyles.VisualStyleElement.Button.CheckBox. 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.
El ejemplo de código siguiente muestra cómo escribir un control personalizado que utiliza el método DrawCheckBox para dibujar una casilla de verificación que responde a los clics del mouse.
using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace CheckBoxRendererSample { class Form1 : Form { public Form1() : base() { CustomCheckBox CheckBox1 = new CustomCheckBox(); Controls.Add(CheckBox1); if (Application.RenderWithVisualStyles) this.Text = "Visual Styles Enabled"; else this.Text = "Visual Styles Disabled"; } [STAThread] static void Main() { // If you do not call EnableVisualStyles below, then // CheckBoxRenderer.DrawCheckBox automatically detects // this and draws the check box without visual styles. Application.EnableVisualStyles(); Application.Run(new Form1()); } } public class CustomCheckBox : Control { private Rectangle textRectangleValue = new Rectangle(); private Point clickedLocationValue = new Point(); private bool clicked = false; private CheckBoxState state = CheckBoxState.UncheckedNormal; public CustomCheckBox() : base() { this.Location = new Point(50, 50); this.Size = new Size(100, 20); this.Text = "Click here"; this.Font = SystemFonts.IconTitleFont; } // Calculate the text bounds, exluding the check box. public Rectangle TextRectangle { get { using (Graphics g = this.CreateGraphics()) { textRectangleValue.X = ClientRectangle.X + CheckBoxRenderer.GetGlyphSize(g, CheckBoxState.UncheckedNormal).Width; textRectangleValue.Y = ClientRectangle.Y; textRectangleValue.Width = ClientRectangle.Width - CheckBoxRenderer.GetGlyphSize(g, CheckBoxState.UncheckedNormal).Width; textRectangleValue.Height = ClientRectangle.Height; } return textRectangleValue; } } // Draw the check box in the current state. protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); CheckBoxRenderer.DrawCheckBox(e.Graphics, ClientRectangle.Location, TextRectangle, this.Text, this.Font, TextFormatFlags.HorizontalCenter, clicked, state); } // Draw the check box in the checked or unchecked state, alternately. protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (!clicked) { clicked = true; this.Text = "Clicked!"; state = CheckBoxState.CheckedPressed; Invalidate(); } else { clicked = false; this.Text = "Click here"; state = CheckBoxState.UncheckedNormal; Invalidate(); } } // Draw the check box in the hot state. protected override void OnMouseHover(EventArgs e) { base.OnMouseHover(e); state = clicked ? CheckBoxState.CheckedHot : CheckBoxState.UncheckedHot; Invalidate(); } // Draw the check box in the hot state. protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); this.OnMouseHover(e); } // Draw the check box in the unpressed state. protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); state = clicked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; 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.