Page 1 of 1

Customize your toolbars and menus with a custom renderer Rate Topic: -----

#1 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 07 August 2008 - 09:33 PM

In .NET 2.0, they added the ability to render toolstrips and menustrips with the professional renderer, so your toolstrips could look like the Microsoft Office tool bars. Thing is, you can't customize the individual colors that make up the control. However, it's not impossible, and this tutorial explains exactly how to do it. Every toolstrip and menustrip has a Renderer property which tells the toolstrip how it will look. That property can take a value of type System.Windows.Forms.ToolStripRenderer or System.Windows.Forms.ToolStripProfessionalRenderer. The ProfessionalRenderer is the one we will be concerned with in this tutorial, since you will be modifying the professional color table of the toolstrips.

This is the default XP toolstrip professional renderer:
Attached Image

This is the default Vista toolstrip professional renderer:
Attached Image

First, create a new form. Put a toolstrip on it called ToolStrip1. Then, put this code in it:
Dim ProRender As New System.Windows.Forms.ToolStripProfessionalRenderer()


That object you just created is a toolstrip professional renderer. It contains information on how your OS renders its toolstrips in professional mode. Notice it has a property called ColorTable. Browse through that property with intellisense. You will see it contains about 50 properties. Each of those properties represents an individual color that goes into rendering the toolstrip. So, to modify the toolstrip's look seem easy now. Just change those properties to the color you want, and set the toolstrip to render from it with a ToolStrip1.Renderer = ProRenderer statement, right? Of course not. If it was that easy, i wouldn't be writing this tutorial. Look closer, and you will see that each of the properties in the ColorTable are Read Only. They only get the system professional renderer colors, they can't be changed. So, now it seems impossible to change the look of your toolstrips that way. But it's not. Here's an easy way to do it. Create a new class. Call it, "MyProfessionalColorTable". Make it inherit from System.Windows.ProfessionalColorTable. So, now it should look like this:
Public Class MyProfessionalColorTable
    Inherits ProfessionalColorTable

End Class


The ProfessionalColorTable class is what the ColorTable property in the ToolStripProfessionalRenderer object is. You know, that object with 50+ properties in it that defined all the colors needed to render a toolstrip in professional mode? That's the object your class just inherited from. If your a bit lost, rember this:


1. The ToolStrip and MenuStrip controls have a Renderer property that takes a value of ToolStripProfessionalRenderer. That property defines how the toolstrip will look. It can be set.

2. The ToolStripProfessionalRenderer class has read-only a property of type ProfessionalColorTable.

3. The ProfessionalColorTable class contains a list of colors that the system uses to render toolstrips in professional mode. They are all read-only.

4. You just created a class that inherits from the ProfessionalColorTable class.

Now, remember, every property in the ProfessionalColorTable class is read-only. But, you need to be able to set them. There is an easy solution. Override every property of the ProfessionalColorTable class in your class. They too, should be read-only. Then, in the get function of the properties, simply type "Return" and the color you wish returned. Then, you in essence can "Set" the values of those properties. But wait, your still not done. In order for this to work, you need to set the ToolStrip1.Renderer property to the ProRenderer variable, and you need to set the ProRenderer.ColorTable property to the MyProfessionalColorTable class you just made. But you can't. The ProRenderer.ColorTable property is read-only too. So, to get around that, go back to the line where you declared the ProRenderer variable. Look at all it's overloads for sub New. You will see one of them takes a ProfessionalColorTable variable. So, to create the final renderer, type
Dim ProRender As New System.Windows.Forms.ToolStripProfessionalRenderer(New MyProfessionalColorTable)


Then, put this line to assign that renderer to your toolstrip:
ToolStrip1.Renderer = ProRender


And that's it. All that's left now is to figure out what each of those 50+ colors in the ProfessionalColorTable class represent, and deciding what color you want them to be.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1