VisualStyleRenderer.GetBackgroundRegion(IDeviceContext, Rectangle) 方法

定义

返回当前视觉样式元素背景的区域。Returns the region for the background of the current visual style element.

public:
 System::Drawing::Region ^ GetBackgroundRegion(System::Drawing::IDeviceContext ^ dc, System::Drawing::Rectangle bounds);
public System.Drawing.Region GetBackgroundRegion (System.Drawing.IDeviceContext dc, System.Drawing.Rectangle bounds);
member this.GetBackgroundRegion : System.Drawing.IDeviceContext * System.Drawing.Rectangle -> System.Drawing.Region
Public Function GetBackgroundRegion (dc As IDeviceContext, bounds As Rectangle) As Region

参数

dc
IDeviceContext

此操作将使用的 IDeviceContextThe IDeviceContext this operation will use.

bounds
Rectangle

一个 Rectangle,它包含当前视觉样式元素的整个背景区域。A Rectangle that contains the entire background area of the current visual style element.

返回

一个 Region,它包含当前视觉样式元素的背景。The Region that contains the background of the current visual style element.

异常

dcnulldc is null.

示例

下面的代码示例演示如何在自定义控件中使用 GetBackgroundRegion 方法。The following code example demonstrates how to use the GetBackgroundRegion method in a custom control. 此示例使用 GetBackgroundRegion 获取 VisualStyleElement.Window.Caption.Active 属性返回的窗口标题栏元素的 RegionThis example uses GetBackgroundRegion to get the Region of the window title bar element returned by the VisualStyleElement.Window.Caption.Active property. Region 用于设置控件的 Control.Region 属性,使窗口标题栏将与标准 Windows XP 视觉样式定义的圆角一起显示。This Region is used to set the Control.Region property of the control, so that the window title bar will appear with the rounded corners defined by the standard Windows XP visual style. 此代码示例是为 VisualStyleRenderer 类概述提供的更大示例的一部分。This code example is part of a larger example provided for the VisualStyleRenderer class overview.

    // Calculate and set the clipping region for the control
    // so that the corners of the title bar are rounded.
private:
    void SetClipRegion()
    {
        if (!Application::RenderWithVisualStyles)
        {
            return;
        }

        Graphics^ g = this->CreateGraphics();
        // Get the current region for the window caption.
        if (SetRenderer(windowElements["windowCaption"]))
        {
            System::Drawing::Region^ clipRegion =
                renderer->GetBackgroundRegion(g,
                elementRectangles["windowCaption"]);

            // Get the client rectangle, but exclude the region
            // of the window caption.
            int height = (int)clipRegion->GetBounds(g).Height;
            System::Drawing::Rectangle nonCaptionRect = Rectangle(
                ClientRectangle.X, ClientRectangle.Y + height,
                ClientRectangle.Width, ClientRectangle.Height - height);

            // Add the rectangle to the caption region, and
            // make this region the form's clipping region.
            clipRegion->Union(nonCaptionRect);
            this->Region = clipRegion;
        }

    }
// Calculate and set the clipping region for the control  
// so that the corners of the title bar are rounded.
private void SetClipRegion()
{
    if (!Application.RenderWithVisualStyles)
    {
        return;
    }

    using (Graphics g = this.CreateGraphics())
    {
        // Get the current region for the window caption.
        if (SetRenderer(windowElements["windowCaption"]))
        {
            Region clipRegion = renderer.GetBackgroundRegion(
                g, elementRectangles["windowCaption"]);

            // Get the client rectangle, but exclude the region 
            // of the window caption.
            int height = (int)clipRegion.GetBounds(g).Height;
            Rectangle nonCaptionRect = new Rectangle(
                ClientRectangle.X,
                ClientRectangle.Y + height,
                ClientRectangle.Width,
                ClientRectangle.Height - height);

            // Add the rectangle to the caption region, and  
            // make this region the form's clipping region.
            clipRegion.Union(nonCaptionRect);
            this.Region = clipRegion;
        }
    }
}
' Calculate and set the clipping region for the control  
' so that the corners of the title bar are rounded.
Private Sub SetClipRegion()
    If Not Application.RenderWithVisualStyles Then
        Return
    End If

    Using g As Graphics = Me.CreateGraphics()
        ' Get the current region for the window caption.
        If SetRenderer(windowElements("windowCaption")) Then
            Dim clipRegion As Region = _
                renderer.GetBackgroundRegion(g, _
                elementRectangles("windowCaption"))

            ' Get the client rectangle, but exclude the   
            ' region of the window caption.
            Dim height As Integer = _
                CInt(clipRegion.GetBounds(g).Height)
            Dim nonCaptionRect As _
                New Rectangle(ClientRectangle.X, _
                ClientRectangle.Y + height, _
                ClientRectangle.Width, _
                ClientRectangle.Height - height)

            ' Add the rectangle to the caption region, and  
            ' make this region the form's clipping region.
            clipRegion.Union(nonCaptionRect)
            Me.Region = clipRegion
        End If
    End Using
End Sub

注解

此方法可用于获取其背景中包含部分透明或 alpha 混合部分的视觉样式元素的 RegionThis method can be used to get the Region of a visual style element that has partially transparent or alpha-blended parts in its background.

适用于