如何:使用控制項呈現類別
這個範例示範如何使用 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 命名空間的參考。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應