Export (0) Print
Expand All

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
)

Parameters

dc
Type: System.Drawing.IDeviceContext

The IDeviceContext used to draw the background image.

bounds
Type: System.Drawing.Rectangle

A Rectangle in which the background image is drawn.

ExceptionCondition
ArgumentNullException

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)
{
    base.OnPaint(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);
        return;
    }

    // Set the clip region to define the curved corners  
    // of the caption.
    SetClipRegion();

    // Draw each part of the window. 
    foreach (KeyValuePair<string, VisualStyleElement> entry
        in windowElements)
    {
        if (SetRenderer(entry.Value))
        {
            renderer.DrawBackground(e.Graphics,
                elementRectangles[entry.Key]);
        }
    }

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft