Editar

Compartir a través de


VisualStyleRenderer.DrawBackground Method

Definition

Draws the background image of the current visual style element.

Overloads

DrawBackground(IDeviceContext, Rectangle)

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

DrawBackground(IDeviceContext, Rectangle, Rectangle)

Draws the background image of the current visual style element within the specified bounding rectangle and clipped to the specified clipping rectangle.

DrawBackground(IDeviceContext, Rectangle)

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

public:
 void DrawBackground(System::Drawing::IDeviceContext ^ dc, System::Drawing::Rectangle bounds);
public void DrawBackground (System.Drawing.IDeviceContext dc, System.Drawing.Rectangle bounds);
member this.DrawBackground : System.Drawing.IDeviceContext * System.Drawing.Rectangle -> unit
Public Sub DrawBackground (dc As IDeviceContext, bounds As Rectangle)

Parameters

dc
IDeviceContext

The IDeviceContext used to draw the background image.

bounds
Rectangle

A Rectangle in which the background image is drawn.

Exceptions

dc is null.

Examples

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:
    virtual void OnPaint(PaintEventArgs^ e) override
    {
        __super::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.
        for each(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);
    }
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);
}
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    MyBase.OnPaint(e)

    ' Ensure that visual styles are supported.
    If Not Application.RenderWithVisualStyles Then
        Me.Text = "Visual styles are not enabled."
        TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _
            Me.Location, Me.ForeColor)
        Return
    End If

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

    ' Draw each part of the window.
    Dim entry As KeyValuePair(Of String, VisualStyleElement)
    For Each entry In windowElements
        If SetRenderer(entry.Value) Then
            renderer.DrawBackground(e.Graphics, _
                elementRectangles(entry.Key))
        End If
    Next entry

    ' Draw the caption text.
    TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _
        elementRectangles("windowCaption"), Color.White, _
        TextFormatFlags.VerticalCenter Or _
        TextFormatFlags.HorizontalCenter)
End Sub

Remarks

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.

Applies to

DrawBackground(IDeviceContext, Rectangle, Rectangle)

Draws the background image of the current visual style element within the specified bounding rectangle and clipped to the specified clipping rectangle.

public:
 void DrawBackground(System::Drawing::IDeviceContext ^ dc, System::Drawing::Rectangle bounds, System::Drawing::Rectangle clipRectangle);
public void DrawBackground (System.Drawing.IDeviceContext dc, System.Drawing.Rectangle bounds, System.Drawing.Rectangle clipRectangle);
member this.DrawBackground : System.Drawing.IDeviceContext * System.Drawing.Rectangle * System.Drawing.Rectangle -> unit
Public Sub DrawBackground (dc As IDeviceContext, bounds As Rectangle, clipRectangle As Rectangle)

Parameters

dc
IDeviceContext

The IDeviceContext used to draw the background image.

bounds
Rectangle

A Rectangle in which the background image is drawn.

clipRectangle
Rectangle

A Rectangle that defines a clipping rectangle for the drawing operation.

Exceptions

dc is null.

Remarks

This method draws the background of the current visual style element specified by the Class, Part, and State properties. The background will be clipped to the area specified by the clipRectangle parameter.

If the Width or Height of the rectangle specified by either the bounds or clipRectangle parameters 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.

Applies to