How to: Custom Draw a ToolStrip Control
The ToolStrip controls have the following associated rendering (painting) classes:
ToolStripSystemRenderer provides the appearance and style of your operating system.
ToolStripProfessionalRenderer provides the appearance and style of Microsoft Office.
ToolStripRenderer is the abstract base class for the other two rendering 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.
Custom Drawing User Control Sample
The following procedures describe various aspects of custom drawing.
To switch between the provided renderers
Set the RenderMode property to the ToolStripRenderMode value you want.
With ManagerRenderMode, the static RenderMode determines the renderer for your application. The other values of ToolStripRenderMode are Custom, Professional, and System.
To change the Microsoft Office–style borders to straight
- Override System.Windows.Forms.ToolStripProfessionalRenderer.OnRenderToolStripBorder(System.Windows.Forms.ToolStripRenderEventArgs), but do not call the base class.
Note |
---|
There is a version of this method for ToolStripRenderer, ToolStripSystemRenderer, and ToolStripProfessionalRenderer. |
To change the ProfessionalColorTable
Override ProfessionalColorTable and change the colors you want.
[Visual Basic]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Me.Load Dim t As MyColorTable = New MyColorTable ToolStrip1.Renderer = New ToolStripProfessionalRenderer(t) End Sub Class MyColorTable Inherits ProfessionalColorTable Public Overrides ReadOnly Property ButtonPressedGradientBegin() As 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 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
To change the rendering for all ToolStrip controls in your application
Use the System.Windows.Forms.ToolStripManager.RenderMode property to choose one of the provided renderers.
Use System.Windows.Forms.ToolStripManager.Renderer to assign a custom renderer.
Ensure that System.Windows.Forms.ToolStrip.RenderMode is set to the default value of ManagerRenderMode.
To turn off the Microsoft Office colors for the entire application
To turn off the Microsoft Office colors for one ToolStrip control
Use code similar to the following code example.
[Visual Basic]
Dim colorTable As ProfessionalColorTable() colorTable.UseSystemColors = True Dim toolStrip.Renderer As ToolStripProfessionalRenderer(colorTable)
[C#]
ProfessionalColorTable colorTable = new ProfessionalColorTable(); colorTable.UseSystemColors = true; toolStrip.Renderer = new ToolStripProfessionalRenderer(colorTable);
See Also
Tasks
How to: Create and Set a Custom Renderer for the ToolStrip Control in Windows Forms
Reference
ToolStrip Control Overview (Windows Forms)
ToolStripSystemRenderer
ToolStripProfessionalRenderer
ToolStripRenderer