如何:在运行时设置 ToolStrip 呈现程序

更新:2007 年 11 月

通过创建自定义的 ProfessionalColorTable 类,可以自定义 ToolStrip 控件的外观。

示例

下面的代码示例演示如何创建自定义的 ProfessionalColorTable 类。该类为 MenuStripToolStrip 控件定义渐变。

若要使用此代码示例,请编译并运行应用程序,然后单击“更改颜色”应用在自定义 ProfessionalColorTable 类中定义的渐变。

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Drawing


...


' This code example demonstrates how to use a ProfessionalRenderer
' to define custom professional colors at runtime.
Class Form2
   Inherits Form

   Public Sub New()
      ' Create a new ToolStrip control.
      Dim ts As New ToolStrip()

      ' Populate the ToolStrip control.
      ts.Items.Add("Apples")
      ts.Items.Add("Oranges")
      ts.Items.Add("Pears")
      ts.Items.Add("Change Colors", Nothing, New EventHandler(AddressOf ChangeColors_Click))

      ' Create a new MenuStrip.
      Dim ms As New MenuStrip()

      ' Dock the MenuStrip control to the top of the form.
      ms.Dock = DockStyle.Top

      ' Add the top-level menu items.
      ms.Items.Add("File")
      ms.Items.Add("Edit")
      ms.Items.Add("View")
      ms.Items.Add("Window")

      ' Add the ToolStrip to Controls collection.
      Me.Controls.Add(ts)

      ' Add the MenuStrip control last.
      ' This is important for correct placement in the z-order.
      Me.Controls.Add(ms)
    End Sub

   ' This event handler is invoked when the "Change colors"
   ' ToolStripItem is clicked. It assigns the Renderer
   ' property for the ToolStrip control.
    Sub ChangeColors_Click(ByVal sender As Object, ByVal e As EventArgs)
        ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomProfessionalColors())
    End Sub
End Class

' This class defines the gradient colors for 
' the MenuStrip and the ToolStrip.
Class CustomProfessionalColors
   Inherits ProfessionalColorTable

   Public Overrides ReadOnly Property ToolStripGradientBegin() As Color
      Get
         Return Color.BlueViolet
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientMiddle() As Color
      Get
         Return Color.CadetBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientEnd() As Color
      Get
         Return Color.CornflowerBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientBegin() As Color
      Get
         Return Color.Salmon
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientEnd() As Color
      Get
         Return Color.OrangeRed
      End Get
    End Property

End Class
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;


...


// This code example demonstrates how to use a ProfessionalRenderer
// to define custom professional colors at runtime.
class Form2 : Form
{
    public Form2()
    {
        // Create a new ToolStrip control.
        ToolStrip ts = new ToolStrip();

        // Populate the ToolStrip control.
        ts.Items.Add("Apples");
        ts.Items.Add("Oranges");
        ts.Items.Add("Pears");
        ts.Items.Add(
            "Change Colors", 
            null, 
            new EventHandler(ChangeColors_Click));

        // Create a new MenuStrip.
        MenuStrip ms = new MenuStrip();

        // Dock the MenuStrip control to the top of the form.
        ms.Dock = DockStyle.Top;

        // Add the top-level menu items.
        ms.Items.Add("File");
        ms.Items.Add("Edit");
        ms.Items.Add("View");
        ms.Items.Add("Window");

        // Add the ToolStrip to Controls collection.
        this.Controls.Add(ts);

        // Add the MenuStrip control last.
        // This is important for correct placement in the z-order.
        this.Controls.Add(ms);
    }

    // This event handler is invoked when the "Change colors"
    // ToolStripItem is clicked. It assigns the Renderer
    // property for the ToolStrip control.
    void ChangeColors_Click(object sender, EventArgs e)
    {
        ToolStripManager.Renderer = 
            new ToolStripProfessionalRenderer(new CustomProfessionalColors());
    }
}

// This class defines the gradient colors for 
// the MenuStrip and the ToolStrip.
class CustomProfessionalColors : ProfessionalColorTable
{
    public override Color ToolStripGradientBegin
    { get { return Color.BlueViolet; } }

    public override Color ToolStripGradientMiddle
    { get { return Color.CadetBlue; } }

    public override Color ToolStripGradientEnd
    { get { return Color.CornflowerBlue; } }

    public override Color MenuStripGradientBegin
    { get { return Color.Salmon; } }

    public override Color MenuStripGradientEnd
    { get { return Color.OrangeRed; } }
}

定义自定义的 ProfessionalColorTable 类

自定义渐变是在 CustomProfessionalColors 类中定义的。

' This class defines the gradient colors for 
' the MenuStrip and the ToolStrip.
Class CustomProfessionalColors
   Inherits ProfessionalColorTable

   Public Overrides ReadOnly Property ToolStripGradientBegin() As Color
      Get
         Return Color.BlueViolet
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientMiddle() As Color
      Get
         Return Color.CadetBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientEnd() As Color
      Get
         Return Color.CornflowerBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientBegin() As Color
      Get
         Return Color.Salmon
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientEnd() As Color
      Get
         Return Color.OrangeRed
      End Get
    End Property

End Class
// This class defines the gradient colors for 
// the MenuStrip and the ToolStrip.
class CustomProfessionalColors : ProfessionalColorTable
{
    public override Color ToolStripGradientBegin
    { get { return Color.BlueViolet; } }

    public override Color ToolStripGradientMiddle
    { get { return Color.CadetBlue; } }

    public override Color ToolStripGradientEnd
    { get { return Color.CornflowerBlue; } }

    public override Color MenuStripGradientBegin
    { get { return Color.Salmon; } }

    public override Color MenuStripGradientEnd
    { get { return Color.OrangeRed; } }
}

分配自定义提供程序

使用 CustomProfessionalColors 类创建一个新的 ToolStripProfessionalRenderer,然后将它分配给 ToolStripManager.Renderer 属性。

' This event handler is invoked when the "Change colors"
' ToolStripItem is clicked. It assigns the Renderer
' property for the ToolStrip control.
 Sub ChangeColors_Click(ByVal sender As Object, ByVal e As EventArgs)
     ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomProfessionalColors())
 End Sub
// This event handler is invoked when the "Change colors"
// ToolStripItem is clicked. It assigns the Renderer
// property for the ToolStrip control.
void ChangeColors_Click(object sender, EventArgs e)
{
    ToolStripManager.Renderer = 
        new ToolStripProfessionalRenderer(new CustomProfessionalColors());
}

编译代码

此示例要求:

  • 对 System.Design、System.Drawing 和 System.Windows.Forms 程序集的引用。

有关从 Visual Basic 或 Visual C# 的命令行生成此示例的信息,请参见从命令行生成 (Visual Basic)在命令行上使用 csc.exe 生成。也可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例

请参见

参考

ToolStripManager

ProfessionalColorTable

MenuStrip

ToolStrip

ToolStripProfessionalRenderer

其他资源

ToolStrip 控件(Windows 窗体)