Udostępnij za pośrednictwem


How to: Create and Set a Custom Renderer for the ToolStrip Control in Windows Forms

ToolStrip controls give easy support to themes and styles. You can achieve completely custom appearance and behavior (look and feel) by setting either the ToolStrip.Renderer property or the ToolStripManager.Renderer property to a custom renderer.

You can assign renderers to each individual ToolStrip, MenuStrip, ContextMenuStrip, or StatusStrip control, or you can use the Renderer property to affect all objects by setting the ToolStrip.RenderMode property to ToolStripRenderMode.ManagerRenderMode.

Note

RenderMode returns Custom only if the value of ToolStrip.Renderer is not null.

To create a custom renderer

  1. Extend the ToolStripRenderer class.

  2. Implement desired custom rendering by overriding appropriate On… members

    Public Class RedTextRenderer
        Inherits System.Windows.Forms.ToolStripRenderer
        Protected Overrides Sub OnRenderItemText(ByVal e As _
            ToolStripItemTextRenderEventArgs) 
            e.TextColor = Color.Red
            e.TextFont = New Font("Helvetica", 7, FontStyle.Bold)
            MyBase.OnRenderItemText(e)
        End Sub
    End Class
    

    [C#]

    public class RedTextRenderer : _
        System.Windows.Forms.ToolStripRenderer
    {
        protected override void _
            OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextColor = Color.Red;
            e.TextFont = new Font("Helvetica", 7, FontStyle.Bold);
           base.OnRenderItemText(e);
        }
    }
    

To set the custom renderer to be the current renderer

  1. To set the custom renderer for one ToolStrip, set the ToolStrip.Renderer property to the custom renderer.

    toolStrip1.Renderer = New RedTextRenderer()
    

    [C#]

    toolStrip1.Renderer = new RedTextRenderer();
    
  2. Or to set the custom renderer for all ToolStrip classes contained in your application: Set the ToolStripManager.Renderer property to the custom renderer and set the RenderMode property to ManagerRenderMode.

    toolStrip1.RenderMode = ToolStripRenderMode.ManagerRenderMode
    ToolStripManager.Renderer = New RedTextRenderer()
    

    [C#]

    toolStrip1.RenderMode = ToolStripRenderMode.ManagerRenderMode;
    ToolStripManager.Renderer = new RedTextRenderer();
    

See Also

Concepts

ToolStrip Control Architecture

ToolStrip Technology Summary

Reference

Renderer

ToolStripRenderer

RenderMode

ToolStrip Control Overview (Windows Forms)