ButtonRenderer.DrawParentBackground(Graphics, Rectangle, Control) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在指定区域中绘制控件的父级的背景。
public:
static void DrawParentBackground(System::Drawing::Graphics ^ g, System::Drawing::Rectangle bounds, System::Windows::Forms::Control ^ childControl);
public static void DrawParentBackground (System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.Control childControl);
static member DrawParentBackground : System.Drawing.Graphics * System.Drawing.Rectangle * System.Windows.Forms.Control -> unit
Public Shared Sub DrawParentBackground (g As Graphics, bounds As Rectangle, childControl As Control)
参数
- childControl
- Control
将绘制其父控件的背景的控件。
示例
下面的代码示例使用 DrawParentBackground 方法在自定义控件的某个区域上绘制。 此代码示例是为 ButtonRenderer 类提供的一个更大示例的一部分。
// Draw the large or small button, depending on the current state.
protected:
virtual void OnPaint(PaintEventArgs^ e) override
{
__super::OnPaint(e);
// Draw the smaller pressed button image
if (state == PushButtonState::Pressed)
{
// Set the background color to the parent if visual styles
// are disabled, because DrawParentBackground will only paint
// over the control background if visual styles are enabled.
if (Application::RenderWithVisualStyles)
{
this->BackColor = Color::Azure;
}
else
{
this->BackColor = this->Parent->BackColor;
}
// If you comment out the call to DrawParentBackground,
// the background of the control will still be visible
// outside the pressed button, if visual styles are enabled.
ButtonRenderer::DrawParentBackground(e->Graphics,
ClientRectangle, this);
ButtonRenderer::DrawButton(e->Graphics, ClickRectangle,
this->Text, this->Font, true, state);
}
// Draw the bigger unpressed button image.
else
{
ButtonRenderer::DrawButton(e->Graphics, ClientRectangle,
this->Text, this->Font, false, state);
}
}
// Draw the large or small button, depending on the current state.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw the smaller pressed button image
if (state == PushButtonState.Pressed)
{
// Set the background color to the parent if visual styles
// are disabled, because DrawParentBackground will only paint
// over the control background if visual styles are enabled.
this.BackColor = Application.RenderWithVisualStyles ?
Color.Azure : this.Parent.BackColor;
// If you comment out the call to DrawParentBackground,
// the background of the control will still be visible
// outside the pressed button, if visual styles are enabled.
ButtonRenderer.DrawParentBackground(e.Graphics,
ClientRectangle, this);
ButtonRenderer.DrawButton(e.Graphics, ClickRectangle,
this.Text, this.Font, true, state);
}
// Draw the bigger unpressed button image.
else
{
ButtonRenderer.DrawButton(e.Graphics, ClientRectangle,
this.Text, this.Font, false, state);
}
}
' Draw the large or small button, depending on the current state.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
' Draw the smaller pressed button image.
If state = PushButtonState.Pressed Then
' Set the background color to the parent if visual styles
' are disabled, because DrawParentBackground will only paint
' over the control background if visual styles are enabled.
If Application.RenderWithVisualStyles Then
Me.BackColor = Color.Azure
Else
Me.BackColor = Me.Parent.BackColor
End If
' If you comment out the call to DrawParentBackground,
' the background of the control will still be visible
' outside the pressed button, if visual styles are enabled.
ButtonRenderer.DrawParentBackground(e.Graphics, _
Me.ClientRectangle, Me)
ButtonRenderer.DrawButton(e.Graphics, ClickRectangle, _
Me.Text, Me.Font, True, state)
' Draw the bigger unpressed button image.
Else
ButtonRenderer.DrawButton(e.Graphics, Me.ClientRectangle, _
Me.Text, Me.Font, False, state)
End If
End Sub