![]() |
|
[VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework) +--- Thread: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover (/Thread-VB-Net-Tutorial-Customizing-MenuStrip-to-Display-Buttons-on-Hover) |
[VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover - Coder-san - 03-21-2011 Customizing MenuStrip to Display Buttons on Hover ![]() 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:
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.
[VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover - Coder-san - 03-21-2011 Customizing MenuStrip to Display Buttons on Hover ![]() 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:
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.
RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover - system32_mybb_import5961 - 03-22-2011 awesome! drawing a button with system.drawing! can that work for other stuff like textboxes? PS: nice share :thumbs: RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover - system32_mybb_import5961 - 03-22-2011 awesome! drawing a button with system.drawing! can that work for other stuff like textboxes? PS: nice share :thumbs: RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover - Coder-san - 03-23-2011 In different ways you can customize most controls. RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover - Coder-san - 03-23-2011 In different ways you can customize most controls. |