Click here to Skip to main content
Click here to Skip to main content

Adding icons to menus in VB.NET

By , 24 Jun 2002
 

IconsMenu

Introduction

This project adds icons in menus in VB.NET. You can use the module 'IconsMenuMain.vb' as the base of your project for adding icons. This is most easier than VB 6 or other languages.

'IconsMenuMain.vb
'Module for adding icons to menus...

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Windows.Forms


Module IconsMenuMain

    Dim m_Font As New Font("Arial", 8)

    Sub MeasureItems(ByVal EvMeasureItem As _
           System.Windows.Forms.MeasureItemEventArgs, _
           ByVal Mi As MenuItem)
        Dim sf As StringFormat = New StringFormat()
        sf.HotkeyPrefix = HotkeyPrefix.Show
        sf.SetTabStops(60, New Single() {0})
        EvMeasureItem.ItemHeight = 22
        EvMeasureItem.ItemWidth = _
          CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), _
          m_Font, 10000, sf).Width) + 10
        sf.Dispose()
        sf = Nothing
    End Sub

    Sub DrawItems(ByVal EvDrawItems As _
          System.Windows.Forms.DrawItemEventArgs, _
          ByVal Mi As MenuItem, ByVal m_Icon As Icon)
        Dim br As Brush
        Dim fDisposeBrush As Boolean
        If Not m_Icon Is Nothing Then
            If Not Mi.Checked Then
                EvDrawItems.Graphics.DrawIcon(m_Icon, _
                   EvDrawItems.Bounds.Left + 2, _
                   EvDrawItems.Bounds.Top + 2)
            Else
                EvDrawItems.Graphics.DrawIcon(m_Icon, _
                   EvDrawItems.Bounds.Left + 2, _
                   EvDrawItems.Bounds.Top + 2)
                Dim nPen As System.Drawing.Pen
                If Not Mi.Enabled Then
                    NPEN = New Pen(Color.DarkGray)
                Else
                    nPen = New Pen(Color.Gray)
                End If
                EvDrawItems.Graphics.DrawRectangle(nPen, _
                  1, EvDrawItems.Bounds.Top, 20, 20)
                EvDrawItems.Graphics.DrawRectangle(nPen, _
                  3, EvDrawItems.Bounds.Top + 2, 16, 16)
            End If
        Else
            If Mi.Checked Then
                Dim nPen As System.Drawing.Pen
                If Not Mi.Enabled Then
                    NPEN = New Pen(Color.DarkGray)
                Else
                    nPen = New Pen(Color.Gray)
                End If
                EvDrawItems.Graphics.DrawRectangle(nPen, _
                  1, EvDrawItems.Bounds.Top, 20, 20)
                Dim Pnts() As Point
                ReDim Pnts(2)
                Pnts(0) = New Point(15, EvDrawItems.Bounds.Top + 6)
                Pnts(1) = New Point(8, EvDrawItems.Bounds.Top + 13)
                Pnts(2) = New Point(5, EvDrawItems.Bounds.Top + 10)
                If Mi.Enabled Then
                    EvDrawItems.Graphics.DrawLines(New _
                      Pen(Color.Black), Pnts)
                Else
                    EvDrawItems.Graphics.DrawLines(New _
                      Pen(Color.Gray), Pnts)
                End If
            End If
        End If
        Dim rcBk As Rectangle = EvDrawItems.Bounds
        rcBk.X += 24

        If CBool(EvDrawItems.State And _
                 DrawItemState.Selected) Then
            br = New LinearGradientBrush(rcBk, _
              Color.MidnightBlue, Color.LightBlue, 0)
            fDisposeBrush = True
        Else
            br = SystemBrushes.Control
        End If

        EvDrawItems.Graphics.FillRectangle(br, rcBk)
        If fDisposeBrush Then br.Dispose()
        br = Nothing

        Dim sf As StringFormat = New StringFormat()
        sf.HotkeyPrefix = HotkeyPrefix.Show
        sf.SetTabStops(60, New Single() {0})
        If Mi.Enabled Then
            br = New SolidBrush(EvDrawItems.ForeColor)
        Else
            br = New SolidBrush(Color.Gray)
        End If

        EvDrawItems.Graphics.DrawString(GetRealText(Mi), _
                 m_Font, br, EvDrawItems.Bounds.Left + 25, _
                 EvDrawItems.Bounds.Top + 2, sf)
        br.Dispose()
        br = Nothing
        sf.Dispose()
        sf = Nothing
    End Sub

    Function GetRealText(ByVal Mi As MenuItem) As String
        Dim s As String = Mi.Text
        If Mi.ShowShortcut And Mi.Shortcut <> Shortcut.None Then
            Dim k As Keys = CType(Mi.Shortcut, Keys)
            s = s & Convert.ToChar(9) & _
             TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
        End If
        Return s
    End Function

End Module


'**************
'In the items of menu which you want 
'add icon modify the property OwnerDraw to TRUE
'For use this code only add the next references in the form...

    Private Sub MenuItem3_DrawItem(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.DrawItemEventArgs) _
            Handles MenuItem3.DrawItem

        Dim Ic As New Icon("C:\Documents" & _
          " and Settings\Yo\Escritorio\iconmenu\Save.ico")
        DrawItems(e, MenuItem3, Nothing)
    End Sub

    Private Sub MenuItem3_MeasureItem(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
            Handles MenuItem3.MeasureItem

        MeasureItems(e, MenuItem3)
    End Sub

This is also an easy write in VC#.NET

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Retana Padilla
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalfeedbackmemberdeostroll24 Dec '06 - 19:17 
This is good piece of information. The credit goes entirely to you.
 
However I had to spend days figuring out just why we have to do this 'this particular way'. Because most of the functions used here have no description, I had to literally copy and paste each function in google to find out what exactly was it doing.
 
Of course for a newbie, by looking at this code there is no way he or she could figure out which function belongs to which namespace. You should at least add some info about what functions are used from which name space. For e.g.
 

imports System.Drawing 'for HotkeyPrefix.Show()

 

Moreover for articles such as this one, there should be some intro into the process or the logic adopted. And where ever necessary code snippets should be presented to explain a certain point. Then finally the whole code should be presented. This saves the application's code from being inundated with comments.
 
But once again this is nice work. I hope you keep more articles coming...
QuestionHow to create Skin From My Graphic design?memberKhanh CaCa13 Dec '06 - 14:55 
I want create Skin(button, form...)from My graphic design (but don't use software to create). I know, in VB6 use .ctl and ocx, but in VB .NET or 2005, how to do it?
Sniff | :^)
QuestionFixing the Menu Width!!!memberajalilqarshi15 Mar '06 - 18:38 
Hi,
 
I want to fix the Menu Width in 150 Pixel.
 
How to do that.
 
Regards,
 
Ahmad Jalil Qarshi
AnswerRe: Fixing the Menu Width!!!memberBrezza28 Dec '06 - 0:32 
Public Sub MeasureMyItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs) handles myItem.MeasureItem
 
e.ItemWidth = 150
 
End Sub
GeneralOut of MemorymemberNauman_Unique16 Feb '05 - 20:25 
WTF | :WTF: This error was mention '16 sep 2002' and now it is 17 feb 2005 , till this issue ,'Out of memory' is Carry on, Slove it now Mad | :mad:
GeneralRe: Out of MemorymemberTanveerBhai27 Jan '06 - 0:55 
DrawItems(e, MenuItem3, Nothing) = DrawItems(e, MenuItem3, Ic)
where Ic is icon
 
Confused | :confused:
GeneralIcon SizememberEcoSys25 Oct '04 - 13:21 
Hi
 
If you have Icons greater than 16x16 you could use the follow
 
:OLD:
 
Private Sub MenuItem2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles MenuItem2.DrawItem
Dim Ic As New Icon(".\icons\1.ico")
DrawItems(e, MenuItem2, Ic)
End Sub
 
:NEW:
 
Private Sub MenuItem4_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles MenuItem4.DrawItem
Dim IconFile As New Icon(".\icons\1.ico")
Dim Ic As New Icon(IconFile, 16, 16)
DrawItems(e, MenuItem4, Ic)
End Sub
 
You must use this cause Dim Ic as New Icon() doesn't allow the parameters for height and width if the given IconSource isn't a valid IconRessource.
 
EcoSys
GeneralWorks great for me except on little problemsussFlemster12 Aug '03 - 6:19 
I get a little gray box around where my mouse cursor enters the menu item.
 

GeneralRe: Works great for me except on little problemmemberBrezza28 Dec '06 - 0:39 
i have the same problem...
 
have you solved it?
 

thanks
Generalout of memorysussAnonymous29 May '03 - 13:29 
out of memory,out of memory,out of memory

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 25 Jun 2002
Article Copyright 2002 by Retana Padilla
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid