[VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover 03-21-2011, 04:21 PM
#1
Customizing MenuStrip to Display Buttons on Hover
![[Image: DQqp9.png]](http://i.imgur.com/DQqp9.png)
Hello there. It's been quite long since I made my last Tutorial.

Today I'm going to show you how to customize a MenuStrip, and add awesome Button Image as Hover effect.
Credits to [link=http://social.msdn.microsoft.com/profile/rudedog2/?type=forum&referrer=http://social.msdn.microsoft.com/Forums/en/winforms/thread/2492c798-63ed-44f2-a9b7-39f197893210]Rudedog2[/link] at [link=http://social.msdn.microsoft.com/Forums/en/winforms/thread/2492c798-63ed-44f2-a9b7-39f197893210]this MSDN topic[/link] for changing BackColor of MenuItem.
What you need in your Form:
- MenuStrip Control with some Menus.
- Some patience and beginner knowledge to read and understand the Code.
When you have the Items ready we can begin.
The Code:
First we will make a new Class in your Form file:
Code:
Public Class Form1
End Class
Public Class MyRenderer
'This
End ClassNow we will make it Inherit ToolStripProfessionalRenderer
Code:
Inherits ToolStripProfessionalRendererCode:
Protected Overloads Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
'This is our Main Sub
End SubRest of the Code is explained by the comments:
Code:
Dim rc As New Rectangle(Point.Empty, e.Item.Size) 'Create a Rectangle for our Menu Item
If e.Item.Selected Then 'If MenuItem is selected
Dim btn As New Button 'Creating a Button on Runtime
btn.Size = New Size(e.Item.Size) 'Making Button Size equal to our MenuItem Size
Dim bmp As New Bitmap(e.Item.Width - 1, e.Item.Height - 1) 'Creating a Bitmap to hold the Button Image
btn.DrawToBitmap(bmp, rc) 'Drawing the Button to the Bitmap bmp with Rectangle rc
bmp.SetPixel(1, 1, Color.Transparent)
bmp.SetPixel(bmp.Width - 1, 1, Color.Transparent)
e.Graphics.DrawImage(bmp, New Point(-1, -1)) 'Finally Rendering bmp on the MenuItem
e.Item.ForeColor = Color.Black
Else
If e.Item.IsOnDropDown = False Then e.Item.ForeColor = Color.White
End IfNow in the end add this small code in Form_Load:
Code:
MenuStrip1.Renderer = New MyRendererThat's it. I hope you like it.
![[Image: rytwG00.png]](http://i.imgur.com/rytwG00.png)
Redcat Revolution!

![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)