如何:使用控件呈现类
更新:2007 年 11 月
此示例演示如何使用 ComboBoxRenderer 类呈现组合框控件的下拉箭头。该示例包含一个简单自定义控件的 OnPaint 方法。ComboBoxRenderer.IsSupported 属性用于确定是否在应用程序窗口的工作区中启用了视觉样式。如果已启用了视觉样式,则 ComboBoxRenderer.DrawDropDownButton 方法将使用视觉样式呈现下拉箭头;否则 ControlPaint.DrawComboButton 方法将以 Windows 经典样式呈现下拉箭头。
示例
' Render the drop-down arrow with or without visual styles.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
If Not ComboBoxRenderer.IsSupported Then
ControlPaint.DrawComboButton(e.Graphics, _
Me.ClientRectangle, ButtonState.Normal)
Else
ComboBoxRenderer.DrawDropDownButton(e.Graphics, _
Me.ClientRectangle, ComboBoxState.Normal)
End If
End Sub
// Render the drop-down arrow with or without visual styles.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (!ComboBoxRenderer.IsSupported)
{
ControlPaint.DrawComboButton(e.Graphics,
this.ClientRectangle, ButtonState.Normal);
}
else
{
ComboBoxRenderer.DrawDropDownButton(e.Graphics,
this.ClientRectangle, ComboBoxState.Normal);
}
}
// Render the drop-down arrow with or without visual styles.
protected:
virtual void OnPaint(PaintEventArgs^ e) override
{
__super::OnPaint(e);
if (!ComboBoxRenderer::IsSupported)
{
ControlPaint::DrawComboButton(e->Graphics,
this->ClientRectangle, ButtonState::Normal);
}
else
{
ComboBoxRenderer::DrawDropDownButton(e->Graphics,
this->ClientRectangle, ComboBoxState::Normal);
}
}
编译代码
此示例需要:
派生自 Control 类的自定义控件。
承载该自定义控件的 Form。
对 System、System.Drawing、System.Windows.Forms 和 System.Windows.Forms.VisualStyles 命名空间的引用。