首页 > 编程 > .NET > 正文

在VB.NET中给菜单加上图标

2024-07-10 13:04:03
字体:
来源:转载
供稿:网友
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)
        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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表