El espacio de nombres System.Windows.Forms.VisualStyles expone objetos VisualStyleElement que representan los elementos de la interfaz de usuario de Windows admitidos por estilos visuales. En este tema se muestra cómo utilizar la clase VisualStyleRenderer para representar el VisualStyleElement que representa los botones Cerrar sesión y Apagar del menú Inicio.
Para representar un elemento de estilo visual
-
Cree VisualStyleRenderer y establézcalo en el elemento que desea dibujar. Tenga el cuenta el uso de la propiedad System.Windows.Forms.Application.RenderWithVisualStyles y del método System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsElementDefined(System.Windows.Forms.VisualStyles.VisualStyleElement); el constructor VisualStyleRenderer producirá una excepción si los estilos visuales están deshabilitados o un elemento no está definido.
Private renderer As VisualStyleRenderer = Nothing
Private element As VisualStyleElement = _
VisualStyleElement.StartPanel.LogOffButtons.Normal
Public Sub New()
Me.Location = New Point(50, 50)
Me.Size = New Size(200, 200)
Me.BackColor = SystemColors.ActiveBorder
If Application.RenderWithVisualStyles And _
VisualStyleRenderer.IsElementDefined(element) Then
renderer = New VisualStyleRenderer(element)
End If
End Sub
private VisualStyleRenderer renderer = null;
private readonly VisualStyleElement element =
VisualStyleElement.StartPanel.LogOffButtons.Normal;
public CustomControl()
{
this.Location = new Point(50, 50);
this.Size = new Size(200, 200);
this.BackColor = SystemColors.ActiveBorder;
if (Application.RenderWithVisualStyles &&
VisualStyleRenderer.IsElementDefined(element))
{
renderer = new VisualStyleRenderer(element);
}
}
private:
VisualStyleRenderer^ renderer;
VisualStyleElement^ element;
public:
CustomControl()
{
this->Location = Point(50, 50);
this->Size = System::Drawing::Size(200, 200);
this->BackColor = SystemColors::ActiveBorder;
this->element =
VisualStyleElement::StartPanel::LogOffButtons::Normal;
if (Application::RenderWithVisualStyles &&
VisualStyleRenderer::IsElementDefined(element))
{
renderer = gcnew VisualStyleRenderer(element);
}
}
-
Llame al método DrawBackground para representar el elemento que VisualStyleRenderer representa actualmente.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' Draw the element if the renderer has been set.
If Not (renderer Is Nothing) Then
renderer.DrawBackground(e.Graphics, Me.ClientRectangle)
' Visual styles are disabled or the element is undefined,
' so just draw a message.
Else
Me.Text = "Visual styles are disabled."
TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _
New Point(0, 0), Me.ForeColor)
End If
End Sub
protected override void OnPaint(PaintEventArgs e)
{
// Draw the element if the renderer has been set.
if (renderer != null)
{
renderer.DrawBackground(e.Graphics, this.ClientRectangle);
}
// Visual styles are disabled or the element is undefined,
// so just draw a message.
else
{
this.Text = "Visual styles are disabled.";
TextRenderer.DrawText(e.Graphics, this.Text, this.Font,
new Point(0, 0), this.ForeColor);
}
}
protected:
virtual void OnPaint(PaintEventArgs^ e) override
{
// Draw the element if the renderer has been set.
if (renderer != nullptr)
{
renderer->DrawBackground(e->Graphics, this->ClientRectangle);
}
// Visual styles are disabled or the element is undefined,
// so just draw a message.
else
{
this->Text = "Visual styles are disabled.";
TextRenderer::DrawText(e->Graphics, this->Text, this->Font,
Point(0, 0), this->ForeColor);
}
}
Compilar el código
Para este ejemplo se necesita:
Vea también