다음을 통해 공유


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 System.Windows.Forms.ToolStrip.Renderer property or the System.Windows.Forms.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 System.Windows.Forms.ToolStrip.RenderMode property to System.Windows.Forms.ToolStripRenderMode.ManagerRenderMode.

NoteNote

RenderMode returns Custom only if the value of System.Windows.Forms.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 System.Windows.Forms.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 System.Windows.Forms.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

Reference

ToolStrip Control Overview (Windows Forms)
Renderer
ToolStripRenderer
RenderMode

Concepts

ToolStrip Control Architecture
ToolStrip Technology Summary