Custom Menus Made Easy
An easy way to make your menus stand out.
Introduction
Microsoft has upgraded the basic MenuStrip
object. Now you can easily have images with it, woohoo! What they didn't do, is make you able to change colors (and by change colors, I mean gradient colors), and let's face it, if your GUI isn't blue, then why would you have a blue menu? The user might be a little confused. And, that's where this comes in; instead of paying for some over-priced control, you can use the ToolstripProfessionalRenderer
, and the best part is that I have wrapped everything you need into a nice little control.
How it Works
This control was a bit interesting to make. I am sure people will point out code issues and stuff, but keep in mind, I wasn't originally going to put this on the web. Back to the code, it uses the ToolstripProfessionalRenderer
with an inherited ProfessionalColorTable
. What I then did was override almost all MenuStrip
properties with my own. This is where it gets messy. Since the original properties are ReadOnly
and we want to write them, I had to create another set of properties to write to the local variables the ReadOnly
variables get their information from. Then, to make it accessible from an object, I had to put the writeable properties in a custom object, then create a way for the object and the class to share data.
How to Use
Overall, not so bad, eh? This control is pretty easy to use, and from what I can find, there is only one bug which I have yet to find an answer to. This is how to use the ColorObject
:
First, you must declare the ColorObjectRenderer
to be the renderer of the MenuStrip
.
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
ToolStripManager.Renderer = Me.ColorObject1.Renderer
End Sub
This tells the MenuStrip
to be rendered using Me.ColorObject1.Renderer
. All the other things are taken care of. Renderer
is a property of my object, just FYI. Now, here is how to use it and where the bug is. In any method, just change one of the color properties. Simple, right? But there is a problem, the control isn't automatically redrawn; after you change the color, you have to Invalidate
the MenuStrip
to redraw. I haven't found a way to programmatically do this, seeing as there isn't a way to access the menu item my object renders. If anyone knows a way around this, please tell me so I can update my code.
Private Sub AnySub()
Dim clr As New ColorDialog
With clr
If .ShowDialog = Windows.Forms.DialogResult.OK And .Color <> Nothing Then
Me.ColorObject1.SecondMenuBarColor = .Color
Me.mnuNew.Invalidate()
End If
End With
End Sub
Closing
I hope you found this article helpful. If you didn't, please leave a comment telling me why, so next time I can do better. Thanks!