Adding Icons In MenuItems In Vb.Net

The following class is responsible for providing a type of MenuItem object which can contain an Icon along with the Name and Shortcut and EventHandler for it. Create an object of the following class and use it as the way as you use in case of default MenuItem objects.

Here goes the class.

Public Class MyIconMenuItem

    Inherits MenuItem
    'the private members
    Private font As font 'as we also want some text on the menu
    Private icon As icon 'for the icon on the menu
    'the constructor for the class
    Sub New(ByVal menuText As String, ByVal handler As EventHandler, _
    ByVal shortcut As Shortcut, ByVal ico As icon)
        MyBase.New(menuText, handler, shortcut)
        Me.icon = ico
        Me.font = SystemInformation.MenuFont
        Me.OwnerDraw = True 'must be called, otherwise we won't be able to see the icon when the menuitem will be drawn

    End Sub

     Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)
        MyBase.OnMeasureItem(e) ' MUST be called, best to call first
        Dim sf As StringFormat = New StringFormat
        sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show
        sf.SetTabStops(50, New Single() {0})

       
If Me.icon.Height > Me.font.Height Then
            e.ItemHeight = Me.icon.Height + 6
        Else
            e.ItemHeight = Me.font.Height + 6
        End If
        e.ItemWidth = CInt(e.Graphics.MeasureString(AppendShortcut(), _
               Me.font, 1000, sf).Width) + Me.icon.Width + 5
        sf.Dispose()
    End Sub

   
Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
        Dim br As Brush
        Dim sf As StringFormat

         MyBase.OnDrawItem(e)
        e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds)
        If Not (Me.icon Is Nothing) Then
            e.Graphics.DrawIcon(Me.icon, e.Bounds.Left + 3, e.Bounds.Top + 3)
        End If

       
sf = New StringFormat
        sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show
        sf.SetTabStops(50, New Single() {0})
        br = New SolidBrush(SystemColors.WindowText)
        e.Graphics.DrawString(AppendShortcut(), Me.font, br, e.Bounds.Left _
     + Me.icon.Width + 10, e.Bounds.Top + 2, sf)

       
'Clean up resources

        br.Dispose()
        sf.Dispose()
    End Sub

    Private Function AppendShortcut() As String
        Dim s As String
        s = Me.Text

       
' Check to see if you have a shortcut
        ' If so, append it to your existing text  
        If Me.ShowShortcut And Me.Shortcut <> Shortcut.None Then
            Dim k As Keys = CType(Shortcut, Keys)
            s = s & Convert.ToChar(9) &
System.ComponentModel.TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
       End If
        Return s
    End Function
End Class
..............................................................................................................................................................

For the sake of understanding I am providing a sample code snippet on how to work with the class.

Just copy and paste the code in a module and call the following sub from the click event of a button. You can also call the sub from the Load event of the form. Basically you can call it from almost any event of the windows form and its controls.


Here goes the example.

Public Sub CreateMyIconMenu()

        ' Create a main menu object.
        Dim mainMenu1 As New MainMenu
        Dim iconPath As String

       
' Create empty menu item objects.
        Dim topMenuItem As New MenuItem

      
'get the icon
       iconPath = "F:\Program Files\Microsoft Visual Studio .NET 2003_Win2K\Common7\Graphics\icons\Computer\DISKS04.ico"

        'while creating the icon replace the icon path with your own values
        Dim ic As New Icon(iconPath)
        'while creating the event handler write the name of your event handler
        'after the  word [AddressOf]. eg.: AddressOf YourEventHandlerName

       
Dim ev As New EventHandler(AddressOf Me.myMenu_Click)
        Dim myMenu As New MyIconMenuItem("&Save All", ev, Shortcut.CtrlF, ic)

       
'Set the caption of the main menu
        topMenuItem.Text = "&File"

       
'Add the menu item to the main menu.
        topMenuItem.MenuItems.Add(myMenu)
        mainMenu1.MenuItems.Add(topMenuItem) 'now add to the main menu of the form

       
'Assign mainMenu1 to the form.

        Me.Menu = mainMenu1
    End Sub

    'Add functionality to the menu items using the Click event.  

   
Private Sub myMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MessageBox.Show("Now Icons are available on menus in your .NET apps! ENJOY", "Icon on menu", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
..............................................................................................................................................................

This simple article is really cool is not it.  Best of luck to all. HAPPY CODING.




Added on December 26, 2007 Comment

Comments

Post a comment