Solved

MainMenu Icons

Posted on 2004-11-01
257 Views
Last Modified: 2010-04-23
Does anyone have a good *FREE* control or a module I can use for adding icons to to owner drawn menus in VB.NET?  I use version 1.0 of the framework.
0
Question by:teamdad
    2 Comments
     
    LVL 34

    Accepted Solution

    by:
    i found this somewhere once (dont reme,ber where??)

    used like this in your form

      Private Sub MenuItem2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem2.DrawItem
            Dim Ic As New Icon("C:\myIcons\Open.ico") 'change path's
            DrawItems(e, MenuItem2, Ic)
        End Sub
        Private Sub MenuItem2_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MenuItem2.MeasureItem
            MeasureItems(e, MenuItem2)
        End Sub


    and this goes in a new module *********************************************

    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
    0
     
    LVL 69

    Expert Comment

    by:Éric Moreau
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now