此示例演示如何使用 ComboBoxRenderer 类呈现组合框控件的下拉箭头。 该示例包含 OnPaint 简单自定义控件的方法。 该 ComboBoxRenderer.IsSupported 属性用于确定是否在应用程序窗口的工作区中启用视觉样式。 如果视觉样式处于活动状态,则 ComboBoxRenderer.DrawDropDownButton 该方法将使用视觉样式呈现下拉箭头;否则,该方法 ControlPaint.DrawComboButton 将在经典 Windows 样式中呈现下拉箭头。
示例:
// 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);
}
}
// 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 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
编译代码
此示例需要:
从Control类派生的自定义控件。
一个承载自定义控件的 Form。
对System、System.Drawing、System.Windows.Forms和System.Windows.Forms.VisualStyles命名空间的引用。