VisualStyleRenderer.GetBackgroundRegion(IDeviceContext, Rectangle) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回目前視覺化樣式項目的背景區域。
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);
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
參數
此作業將使用的 IDeviceContext。
傳回
Region,包含目前視覺化樣式項目的背景。
例外狀況
dc
為 null
。
範例
下列程式碼範例示範如何在自訂控制項中使用 GetBackgroundRegion 方法。 這個範例會使用 GetBackgroundRegion 取得 Region 屬性所 VisualStyleElement.Window.Caption.Active 傳回之視窗標題列專案的 。 這是 Region 用來設定 Control.Region 控制項的 屬性,讓視窗標題列會顯示為標準Windows XP 視覺化樣式所定義的圓角。 此程式碼範例是類別概觀所提供較大範例的 VisualStyleRenderer 一部分。
// 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
備註
這個方法可以用來取得 Region 在背景中具有部分透明或 Alpha 混合部分的視覺樣式專案。