如何:自定义绘制 ToolStrip 控件

ToolStrip 控件具有以下关联的呈现(绘制)类:

若要对 ToolStrip 进行自定义绘制(也称为所有者描述),可以重写其中一个呈现器类,并更改呈现逻辑的一个方面。

下面的过程描述自定义绘制的各个方面。

在提供的呈现器之间切换

更改 Office 样式边框

注意

此方法没有针对 ToolStripRendererToolStripSystemRendererToolStripProfessionalRenderer 的版本。

更改 ProfessionalColorTable

  • 重写 ProfessionalColorTable 并更改所需的颜色。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            var colorTable = new MyColorTable();
            toolStrip1.Renderer = new ToolStripProfessionalRenderer(colorTable);
        }
    
        class MyColorTable: ProfessionalColorTable
        {
            public override System.Drawing.Color ButtonPressedGradientBegin => Color.Red;
            public override System.Drawing.Color ButtonPressedGradientMiddle => Color.Blue;
            public override System.Drawing.Color ButtonPressedGradientEnd => Color.Green;
            public override System.Drawing.Color ButtonSelectedGradientBegin => Color.Yellow;
            public override System.Drawing.Color ButtonSelectedGradientMiddle => Color.Orange;
            public override System.Drawing.Color ButtonSelectedGradientEnd => Color.Violet;
        }
    }
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim colorTable As New MyColorTable
            ToolStrip1.Renderer = New ToolStripProfessionalRenderer(colorTable)
        End Sub
    
        Class MyColorTable
            Inherits ProfessionalColorTable
    
            Public Overrides ReadOnly Property ButtonPressedGradientBegin() As System.Drawing.Color
                Get
                    Return Color.Red
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonPressedGradientMiddle() As System.Drawing.Color
                Get
                    Return Color.Blue
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonPressedGradientEnd() As System.Drawing.Color
                Get
                    Return Color.Green
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonSelectedGradientBegin() As System.Drawing.Color
                Get
                    Return Color.Yellow
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonSelectedGradientMiddle() As System.Drawing.Color
                Get
                    Return Color.Orange
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonSelectedGradientEnd() As System.Drawing.Color
                Get
                    Return Color.Violet
                End Get
            End Property
        End Class
    End Class
    

更改所有 ToolStrips 的呈现

  1. 使用 ToolStripManager.RenderMode 属性从提供的呈现器中选择一个。

  2. 使用 ToolStripManager.Renderer 分配自定义呈现器。

  3. 确保 ToolStrip.RenderMode 已设置为默认值 ManagerRenderMode

关闭 Office 颜色

关闭一个 ToolStrip 的 Office 颜色

  • 使用类似于以下代码示例的代码。

    ProfessionalColorTable colorTable = new ProfessionalColorTable();
    colorTable.UseSystemColors = true;
    toolStrip1.Renderer = new ToolStripProfessionalRenderer(colorTable);
    
    Dim colorTable As New ProfessionalColorTable
    colorTable.UseSystemColors = True
    ToolStrip1.Renderer = new ToolStripProfessionalRenderer(colorTable)
    

另请参阅