Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MSDN Library

How to: Render a Visual Style Element

The System.Windows.Forms.VisualStyles namespace exposes VisualStyleElement objects that represent the Windows user interface (UI) elements supported by visual styles. This topic demonstrates how to use the VisualStyleRenderer class to render the VisualStyleElement that represents the Log Off and Shut Down buttons of the Start menu.

To render a visual style element

  1. Create a VisualStyleRenderer and set it to the element you want to draw. Note the use of the Application.RenderWithVisualStyles property and the VisualStyleRenderer.IsElementDefined method; the VisualStyleRenderer constructor will throw an exception if visual styles are disabled or an element is undefined.

    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);
        }
    }
    
  2. Call the DrawBackground method to render the element that the VisualStyleRenderer currently represents.

    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);
        }
    }
    

This example requires:

Community Additions

Show:
© 2015 Microsoft