如何:呈现视觉样式元素
更新:2007 年 11 月
System.Windows.Forms.VisualStyles 命名空间公开了表示由视觉样式支持的 Windows 用户界面 (UI) 元素的 VisualStyleElement 对象。本主题演示如何使用 VisualStyleRenderer 类呈现表示“开始”菜单中的“注销”和“关机”按钮的 VisualStyleElement。
呈现视觉样式元素
创建一个 VisualStyleRenderer 并将其设置为您想绘制的元素。请注意 Application.RenderWithVisualStyles 属性和 VisualStyleRenderer.IsElementDefined 方法的使用;如果视觉样式被禁用或某个元素未定义,则 VisualStyleRenderer 构造函数将引发异常。
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
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: 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); } }
调用 DrawBackground 方法呈现 VisualStyleRenderer 当前所表示的元素。
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
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: 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); } }
编译代码
此示例需要:
派生自 Control 类的自定义控件。
承载该自定义控件的 Form。
对 System、System.Drawing、System.Windows.Forms 和 System.Windows.Forms.VisualStyles 命名空间的引用。