Login Register


[VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover filter_list
Author
Message
[VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover #1
Customizing MenuStrip to Display Buttons on Hover


[Image: DQqp9.png]

Hello there. It's been quite long since I made my last Tutorial. Smile
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 Class

Now we will make it Inherit ToolStripProfessionalRenderer

Code:
Inherits ToolStripProfessionalRenderer
Now we will override the MenuItem Render in this Subroutine

Code:
Protected Overloads Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs) 'This is our Main Sub End Sub

Rest 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 If

Now in the end add this small code in Form_Load:

Code:
MenuStrip1.Renderer = New MyRenderer

That's it. I hope you like it. Smile
[Image: rytwG00.png]
Redcat Revolution!

Reply

[VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover #2
Customizing MenuStrip to Display Buttons on Hover


[Image: DQqp9.png]

Hello there. It's been quite long since I made my last Tutorial. Smile
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 Class

Now we will make it Inherit ToolStripProfessionalRenderer

Code:
Inherits ToolStripProfessionalRenderer
Now we will override the MenuItem Render in this Subroutine

Code:
Protected Overloads Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs) 'This is our Main Sub End Sub

Rest 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 If

Now in the end add this small code in Form_Load:

Code:
MenuStrip1.Renderer = New MyRenderer

That's it. I hope you like it. Smile
[Image: rytwG00.png]
Redcat Revolution!

Reply

RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover #3
awesome! drawing a button with system.drawing! can that work for other stuff like textboxes?

PS: nice share :thumbs:

Reply

RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover #4
awesome! drawing a button with system.drawing! can that work for other stuff like textboxes?

PS: nice share :thumbs:

Reply

RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover #5
In different ways you can customize most controls.
[Image: rytwG00.png]
Redcat Revolution!

Reply

RE: [VB.Net] [Tutorial] Customizing MenuStrip to Display Buttons on Hover #6
In different ways you can customize most controls.
[Image: rytwG00.png]
Redcat Revolution!

Reply







Users browsing this thread: