如何:呈現視覺化樣式項目
命名空間 System.Windows.Forms.VisualStyles 會 VisualStyleElement 公開物件,這些物件代表視覺化樣式支援的 Windows 使用者介面 (UI) 元素。 本主題示範如何使用 VisualStyleRenderer 類別來轉譯 VisualStyleElement 代表 [開始] 功能表的 [登出 ] 和 [關機 ] 按鈕。
若要轉譯視覺化樣式專案
建立 , VisualStyleRenderer 並將它設定為您想要繪製的專案。 請注意屬性和 VisualStyleRenderer.IsElementDefined 方法的使用 Application.RenderWithVisualStyles ;如果停用視覺化樣式或未定義專案,建 VisualStyleRenderer 構函式會擲回例外狀況。
private: VisualStyleRenderer^ renderer; VisualStyleElement^ element; public: CustomControl() { this->Location = Point(50, 50); this->Size = System::Drawing::Size(200, 200); this->BackColor = SystemColors::ActiveBorder; this->element = VisualStyleElement::StartPanel::LogOffButtons::Normal; if (Application::RenderWithVisualStyles && VisualStyleRenderer::IsElementDefined(element)) { renderer = gcnew VisualStyleRenderer(element); } }
private VisualStyleRenderer renderer = null; private readonly VisualStyleElement element = VisualStyleElement.StartPanel.LogOffButtons.Normal; public CustomControl() { this.Location = new Point(50, 50); this.Size = new Size(200, 200); this.BackColor = SystemColors.ActiveBorder; if (Application.RenderWithVisualStyles && VisualStyleRenderer.IsElementDefined(element)) { renderer = new VisualStyleRenderer(element); } }
Private renderer As VisualStyleRenderer = Nothing Private element As VisualStyleElement = _ VisualStyleElement.StartPanel.LogOffButtons.Normal Public Sub New() Me.Location = New Point(50, 50) Me.Size = New Size(200, 200) Me.BackColor = SystemColors.ActiveBorder If Application.RenderWithVisualStyles And _ VisualStyleRenderer.IsElementDefined(element) Then renderer = New VisualStyleRenderer(element) End If End Sub
DrawBackground呼叫 方法,以轉譯 VisualStyleRenderer 目前所代表的專案。
protected: virtual void OnPaint(PaintEventArgs^ e) override { // Draw the element if the renderer has been set. if (renderer != nullptr) { renderer->DrawBackground(e->Graphics, this->ClientRectangle); } // Visual styles are disabled or the element is undefined, // so just draw a message. else { this->Text = "Visual styles are disabled."; TextRenderer::DrawText(e->Graphics, this->Text, this->Font, Point(0, 0), this->ForeColor); } }
protected override void OnPaint(PaintEventArgs e) { // Draw the element if the renderer has been set. if (renderer != null) { renderer.DrawBackground(e.Graphics, this.ClientRectangle); } // Visual styles are disabled or the element is undefined, // so just draw a message. else { this.Text = "Visual styles are disabled."; TextRenderer.DrawText(e.Graphics, this.Text, this.Font, new Point(0, 0), this.ForeColor); } }
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) ' Draw the element if the renderer has been set. If (renderer IsNot Nothing) Then renderer.DrawBackground(e.Graphics, Me.ClientRectangle) ' Visual styles are disabled or the element is undefined, ' so just draw a message. Else Me.Text = "Visual styles are disabled." TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _ New Point(0, 0), Me.ForeColor) End If End Sub
編譯程式碼
這個範例需要:
衍生自 類別的 Control 自訂控制項。
Form裝載自訂控制項的 。
System、 System.Drawing 、 System.Windows.Forms 和 System.Windows.Forms.VisualStyles 命名空間的參考。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應