[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

MainMenu Icons

Posted on 2004-11-01
2
Medium Priority
?
267 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
Comment
Question by:teamdad
2 Comments
 
LVL 34

Accepted Solution

by:
flavo earned 2000 total points
ID: 12469833
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 71

Expert Comment

by:Éric Moreau
ID: 12469837
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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 …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

613 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