胖在一方

出得厅堂入得厨房的胖子

导航

自定义MenuItem

Posted on 2006-09-12 17:07  胖在一方  阅读(716)  评论(0)    收藏  举报


代码如下:

  1 '------------------------------------------------------------
  2 ' 
  3 ' Author    : Stpangpang 
  4 ' Created   : 2005/11/04
  5 ' Purpose   : Custom IconMenuItem 
  6 '
  7 '------------------------------------------------------------

  8 
  9 Imports System
 10 Imports
 System.ComponentModel
 11 Imports
 System.Drawing
 12 Imports
 System.Drawing.Drawing2D
 13 Imports
 System.Drawing.Text
 14 Imports
 System.Reflection
 15 Imports
 System.Resources
 16 Imports
 System.Windows.Forms
 17 

 18 Public Class IconMenuItem : Inherits MenuItem
 19 

 20     Private m_Icon As Icon
 21     Private m_Font As
 Font
 22 

 23     Dim maxLength As Integer
 24 
 25     Public Sub New()
 26         MyClass.New(""NothingNothing
, System.Windows.Forms.Shortcut.None)
 27     End Sub

 28 
 29     Public Sub New(ByVal text As StringByVal icon As Icon, ByVal onClick As EventHandler, ByVal shortcut As Shortcut)
 30         MyBase
.New(text, onClick, shortcut)
 31         OwnerDraw = True

 32         m_Font = New Font("宋体"9)
 33         m_Icon =
 icon
 34     End Sub

 35 
 36     'Get really text
 37     Private Function GetRealText() As String
 38         Dim s As String = Text
 39 

 40         Dim shortString As String
 41         If ShowShortcut And Shortcut <> Shortcut.None Then
 42             Dim k As Keys = CType(Shortcut, Keys)
 43             shortString = TypeDescriptor.GetConverter(GetType
(Keys)).ConvertToString(k)
 44         End If

 45 
 46         Return String.Format("{0}  {1}", s, shortString)
 47     End Function

 48 
 49     Private Function getShortcut() As String
 50         Dim shortString As String
 51         If ShowShortcut And Shortcut <> Shortcut.None Then
 52             Dim k As Keys = CType(Shortcut, Keys)
 53             shortString = TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k).PadLeft(15" "
c)
 54         End If

 55 
 56         Return shortString
 57 

 58     End Function
 59 
 60     Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
 61 

 62 
 63         Dim br As Brush
 64         Dim rcBk As Rectangle =
 e.Bounds
 65         Dim icoRc As Rectangle =
 e.Bounds
 66 

 67         If e.State And DrawItemState.Selected Then
 68             'when mouse hover Fill MenuItem Backcolor 
 69             br = New SolidBrush(Color.FromArgb(193210238))
 70 
            e.Graphics.FillRectangle(br, rcBk)
 71         Else

 72             br = New SolidBrush(Color.FromArgb(252252249))
 73 
            e.Graphics.FillRectangle(br, rcBk)
 74 

 75             'Fill Icon Rectangle 
 76             icoRc.Width = 22
 77             br = New SolidBrush(Color.FromArgb(236233216))
 78 
            e.Graphics.FillRectangle(br, icoRc)
 79         End If

 80 
 81         'Draw Icon 
 82         If Not m_Icon Is Nothing Then
 83             e.Graphics.DrawIcon(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 2)
 84         End If

 85 
 86 
 87         ' Leave room for accelerator key
 88         Dim sf As StringFormat = New StringFormat
 89         sf.HotkeyPrefix =
 HotkeyPrefix.Show
 90 

 91         ' Draw   menu text
 92         br = New SolidBrush(e.ForeColor)
 93         e.Graphics.DrawString(GetRealText(), m_Font, br, e.Bounds.Left + 25, e.Bounds.Top + 4
, sf)
 94         ' e.Graphics.DrawString(getShortcut(), m_Font, br, e.Bounds.Left + 25 + e.Graphics.MeasureString(GetRealText(), m_Font, 10000, sf).Width, e.Bounds.Top + 4, sf)

 95 
 96 
 97 
 98         'Draw Hover menuitem rectangle 
 99         Dim r As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1)
100         If CBool(e.State And DrawItemState.Selected) Then

101             e.Graphics.DrawRectangle(New Pen(Color.FromArgb(49106197)), r)
102         End If

103 
104         br.Dispose()
105 

106         MyBase.OnDrawItem(e)
107 

108     End Sub
109 
110 
111     Function GetRealLength(ByVal strText As StringAs Integer
112         Dim length As Integer = 0
113         Dim i As Integer
114         For i = 0 To strText.Length
115             If System.Text.RegularExpressions.Regex.IsMatch(strText.Substring(i, 1), "[\u4e00-\u9fa5]"Then

116                 length += 2
117             Else
118                 length += 1
119             End If
120         Next
121         Return length
122 

123     End Function
124 
125     Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)
126 

127         Dim sf As New StringFormat
128         sf.HotkeyPrefix =
 HotkeyPrefix.Show
129         e.ItemHeight = 23

130         e.ItemWidth = CInt(e.Graphics.MeasureString(GetRealText(), m_Font, 10000, sf).Width) + 14
131         MyBase.OnMeasureItem(e)
132     End Sub

133 End Class
134 
135 代码使用如下:
136   Dim menu As New
 MainMenu
137         Dim mItem As
 MenuItem
138 

139         [color=Green]'Fingerprint Menu[color]
140         mItem = New MenuItem("指纹(&F)")
141         mItem.MenuItems.Add(New IconMenuItem("指纹登记(&R)"New Icon(Me.GetType(), "ICO_DJZW.ico"), New EventHandler(AddressOf
 Menu_RegFingerprint), Shortcut.None))
142         mItem.MenuItems.Add(New IconMenuItem("科目一比对(&V)"New Icon(Me.GetType(), "ICO_KM1.ico"), New EventHandler(AddressOf
 Menu_KM1), Shortcut.None))
143         mItem.MenuItems.Add(New IconMenuItem("科目二比对(&V)"New Icon(Me.GetType(), "ICO_KM2.ico"), New EventHandler(AddressOf
 Menu_KM2), Shortcut.None))
144         mItem.MenuItems.Add(New IconMenuItem("科目三比对(&V)"New Icon(Me.GetType(), "ICO_KM3.ico"), New EventHandler(AddressOf
 Menu_KM3), Shortcut.None))
145         mItem.MenuItems.Add(New IconMenuItem("退出系统(&E)"New Icon(Me.GetType(), "ICO_TC.ico"), New EventHandler(AddressOf
 Menu_exit), Shortcut.CtrlX))
146 
        menu.MenuItems.Add(mItem)
147 

148 [color=Green]
149   '第一种方法

150     '---------------------------------------------------------
151     'Dim ass As [Assembly] = [Assembly].GetExecutingAssembly()
152     'Dim s As String() = ass.GetManifestResourceNames() '可以得到所有的资源文件名
153     '   Dim str As String
154     '   For Each str In s
155     '        Debug.WriteLine(str)
156     '    Next
157 
158     ' 前面要加上当前项目的Namespace  "ExamFingerprint" 
159     'Dim icoStream As Stream = ass.GetManifestResourceStream("ExamFingerprint.登记指纹.ico")
160     'Dim b As New Icon(icoStream)
161 
162     '-----------------------------------------------------------
163 
164     '第二种方法 (登记指纹.ico的 Build Action 设置成 Embedded Resource 类型)
165     '-----------------------------------------------------------
166     '   从指定程序集中的资源初始化 Icon 类的新实例。
167     '   Dim a As Icon = New Icon(Me.GetType(), "登记指纹.ico")
168     '-----------------------------------------------------------
169 
170