VisualStyleRenderer.DrawBackground Method (IDeviceContext, Rectangle)


Draws the background image of the current visual style element within the specified bounding rectangle.

Namespace:   System.Windows.Forms.VisualStyles
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void DrawBackground(
	IDeviceContext dc,
	Rectangle bounds


Type: System.Drawing.IDeviceContext

The IDeviceContext used to draw the background image.

Type: System.Drawing.Rectangle

A Rectangle in which the background image is drawn.

Exception Condition

dc is null.

This method draws the background of the current visual style element specified by the Class, Part, and State properties.

If the Width or Height of the rectangle specified by the bounds parameter is less than 0, the DrawBackground method will return without drawing the background.

The background of a visual style element can be a bitmap file or a filled border. To determine the background type, call the GetEnumValue method with an argument value of EnumProperty.BackgroundType. To determine whether the element background will scale to fit the specified bounds, call the GetEnumValue method with an argument value of EnumProperty.SizingType.

The following code example demonstrates how to use the DrawBackground(IDeviceContext, Rectangle) method to a draw a VisualStyleElement within a custom control's OnPaint method. This code example is part of a larger example provided for the VisualStyleRenderer class overview.

protected override void OnPaint(PaintEventArgs e)

    // Ensure that visual styles are supported.
    if (!Application.RenderWithVisualStyles)
        this.Text = "Visual styles are not enabled.";
        TextRenderer.DrawText(e.Graphics, this.Text,
            this.Font, this.Location, this.ForeColor);

    // Set the clip region to define the curved corners 
    // of the caption.

    // Draw each part of the window.
    foreach (KeyValuePair<string, VisualStyleElement> entry
        in windowElements)
        if (SetRenderer(entry.Value))

    // Draw the caption text.
    TextRenderer.DrawText(e.Graphics, this.Text, this.Font,
        elementRectangles["windowCaption"], Color.White,
        TextFormatFlags.VerticalCenter |

.NET Framework
Available since 2.0
Return to top