日期:2012-02-01  浏览次数:20354 次

Adding icons to menus in VB.NET

'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)