Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MainMenu Icons

Posted on 2004-11-01
2
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

Expert Comment

by:Éric Moreau
ID: 12469837
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

597 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