Bewerken

Delen via


How to: Custom Draw a ToolStrip Control

The ToolStrip controls have the following associated rendering (painting) classes:

To custom draw (also known as owner draw) a ToolStrip, you can override one of the renderer classes and change an aspect of the rendering logic.

The following procedures describe various aspects of custom drawing.

Switch between the provided renderers

Change the Office–style borders

Note

There is a version of this method for ToolStripRenderer, ToolStripSystemRenderer, and ToolStripProfessionalRenderer.

Change the ProfessionalColorTable

  • Override ProfessionalColorTable and change the colors you want.

    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
    

Change rendering for all ToolStrips

  1. Use the ToolStripManager.RenderMode property to choose one of the provided renderers.

  2. Use ToolStripManager.Renderer to assign a custom renderer.

  3. Ensure that ToolStrip.RenderMode is set to the default value of ManagerRenderMode.

Turn off the Office colors

Turn off the Office colors for one ToolStrip

  • Use code similar to the following code example.

    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)
    

See also