由 2006-7-3 13:47:00 By:废客泉 XP风格的菜单类(VB.net) |
'/ '======================================
1
'/
2 ' 名称: XP Style Menu 3 ' 作者: DeityFox 4 ' E-Mail: daniel_0571@163.com 5 ' 修改: 2002.5.15 6 ' 类名: xpMenu 7 ' 注释: 如果要转载本代码,请注明出处 8 '/ 9 10 Imports System 11 Imports System.Windows.Forms 12 Imports System.Drawing 13 Imports System.Drawing.Text 14 Imports System.Diagnostics 15 16 Namespace xpmenuitemNamespace xpmenuitem '命名名字空间 17 Public Class xpMenuClass xpMenu '创建子类xpMenu,继承于MenuItem 18 Inherits MenuItem 19 20 定义和初始化变量、常量#Region "定义和初始化变量、常量" 21 22 Const TEXTSTART = 25 23 Private icon As Image = Nothing 24 Private shortcuttext As String = "" 25 Dim itemHeight As Integer 26 Dim bgcolor As Color = Color.FromArgb(249, 248, 247) 27 Dim ibgcolor As Color = Color.FromArgb(219, 216, 209) 28 Dim sbcolor As Color = Color.FromArgb(173, 173, 209) 29 Dim sbbcolor As Color = Color.FromArgb(0, 0, 128) 30 31 #End Region 32 33 重载MenuItem的构造函数#Region "重载MenuItem的构造函数 " 34 35 36 Public Sub New()Sub New() 37 MyBase.New() 38 Me.OwnerDraw = True 39 40 End Sub 41 42 Public Sub New()Sub New(ByVal Name As String) 43 MyBase.New(Name) 44 Me.OwnerDraw = True 45 46 End Sub 47 Public Sub New()Sub New(ByVal Name As String, ByVal EventHandler As System.EventHandler) 48 MyBase.New(Name, EventHandler) 49 Me.OwnerDraw = True 50 51 End Sub 52 Public Sub New()Sub New(ByVal name As String, ByVal items() As MenuItem) 53 MyBase.New(name, items) 54 Me.OwnerDraw = True 55 End Sub 56 Public Sub New()Sub New(ByVal Name As String, ByVal EventHandler As System.EventHandler, ByVal Shortcut As System.Windows.Forms.Shortcut) 57 MyBase.New(Name, EventHandler, Shortcut) 58 Me.OwnerDraw = True 59 End Sub 60 Public Sub New()Sub New(ByVal Name As String, ByVal img As Image) 61 MyBase.New(Name) 62 Me.OwnerDraw = True 63 icon = img 64 End Sub 65 Public Sub New()Sub New(ByVal Name As String, ByVal EventHandler As System.EventHandler, ByVal img As Image) 66 MyBase.New(Name, EventHandler) 67 Me.OwnerDraw = True 68 icon = img 69 End Sub 70 Public Sub New()Sub New(ByVal Name As String, ByVal EventHandler As System.EventHandler, ByVal Shortcut As System.Windows.Forms.Shortcut, ByVal img As Image) 71 MyBase.New(Name, EventHandler, Shortcut) 72 Me.OwnerDraw = True 73 icon = img 74 End Sub 75 #End Region 76 77 菜单项的menuitemicon属性#Region "菜单项的menuitemicon属性" 78 '添加新属性MenuItemIcon,主要是用来设置菜单项左边的图形 79 Public Property MenuItemIcon()Property MenuItemIcon() As Image 80 Get 81 Return icon 82 End Get 83 Set(ByVal Value As Image) 84 icon = Value 85 End Set 86 End Property 87 #End Region 88 89 覆盖OnMeasureItem方法#Region "覆盖OnMeasureItem方法" 90 '覆盖OnMeasureItem方法 91 Protected Overrides Sub onmeasureitem()Sub onmeasureitem(ByVal e As System.Windows.Forms.MeasureItemEventArgs) 92 MyBase.OnMeasureItem(e) 93 If Shortcut <> Shortcut.None Then 94 Dim text As String = "" 95 Dim key As Integer = System.Convert.ToInt32(Shortcut) 96 Dim ch As Integer = key And &HFF 97 If (System.Convert.ToInt32(Keys.Control) And key) > 0 Then 98 text += "Ctrl+" 99 End If 100 If (System.Convert.ToInt32(Keys.Shift) And key) > 0 Then 101 text += "Shift+" 102 End If 103 If (System.Convert.ToInt32(Keys.Alt) And key) > 0 Then 104 text += "Alt+" 105 End If 106 If (ch >= System.Convert.ToInt32(Shortcut.F1)) And (ch <= System.Convert.ToInt32(Shortcut.F12)) Then 107 text += "F" + System.Convert.ToChar((ch - System.Convert.ToInt32(Shortcut.F1) + 1)) 108 Else 109 If Shortcut = Shortcut.Del Then 110 text += "Del" 111 Else 112 text += System.Convert.ToChar(ch) 113 End If 114 End If 115 shortcuttext = text 116 End If 117 118 Dim topLevel As Boolean 119 Dim tempshortcuttext As String = shortcuttext 120 If Parent Is Parent.GetMainMenu Then 121 topLevel = True 122 End If 123 If topLevel Then 124 tempshortcuttext = "" 125 End If 126 127 Dim textwidth As Integer = System.Convert.ToInt32(e.Graphics.MeasureString(text + tempshortcuttext, SystemInformation.MenuFont).Width) 128 Dim extraheight As Integer = 4 129 e.ItemHeight = SystemInformation.MenuHeight + extraheight 130 If topLevel Then 131 e.ItemWidth = textwidth - 5 132 Else 133 e.ItemWidth = Math.Max(160, textwidth + 50) 134 End If 135 If text = "-" Then 136 e.ItemHeight = 5 137 e.ItemWidth = 4 138 End If 139 itemHeight = e.ItemHeight 140 141 End Sub 142 #End Region 143 144 覆盖OnDrawItem方法#Region "覆盖OnDrawItem方法" 145 '覆盖OnDrawItem方法 146 Protected Overrides Sub OnDrawItem()Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs) 147 MyBase.OnDrawItem(e) 148 Dim g As Graphics = e.Graphics 149 Dim bounds As Rectangle = e.Bounds 150 Dim selected As Boolean = (e.State And DrawItemState.Selected) > 0 151 Dim toplevel As Boolean = (Parent Is Parent.GetMainMenu) 152 Dim hasicon As Boolean = (Not (icon Is Nothing)) 153 Dim ena As Boolean = Enabled 154 DrawBackground(g, bounds, e.State, toplevel, hasicon, ena) 155 If hasicon Then 156 DrawIcon(g, icon, bounds, selected, Enabled, Checked) 157 Else 158 If Checked Then 159 DrawCheckmark(g, bounds, selected) 160 End If 161 162 End If 163 If Text = "-" Then 164 DrawSeparator(g, bounds) 165 Else 166 DrawMenuText(g, bounds, Text, shortcuttext, ena, toplevel, e.State) 167 End If 168 End Sub 169 #End Region 170 171 画菜单的各个功能方法,如画文字、图形、背景、分隔线等#Region "画菜单的各个功能方法,如画文字、图形、背景、分隔线等" 172 173 '功能名称: DrawCheckmark(画菜单选项) 174 '参数说明: 175 'bounds 菜单项前面表示选中的小方块 176 'selected boolean型,表示是否选中 177 Public Sub DrawCheckmark()Sub DrawCheckmark(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal selected As Boolean) 178 ControlPaint.DrawMenuGlyph(g, New Rectangle(bounds.X + 3, bounds.Y + 4, 14, 14), MenuGlyph.Checkmark) 179 End Sub 180 181 '功能名称: DrawIcon(画菜单项图形) 182 '参数说明: 183 'icon 菜单项前面的图形 184 'enabled 菜单项是否被禁用 185 'ischecked 是否被选中 186 Public Sub DrawIcon()Sub DrawIcon(ByVal g As Graphics, ByVal icon As Image, ByVal bounds As Rectangle, ByVal selected As Boolean, ByVal enabled As Boolean, ByVal ischecked As Boolean) 187 If enabled Then 188 If selected Then 189 ControlPaint.DrawImageDisabled(g, icon, bounds.Left + 2, bounds.Top + 3, Color.Black) 190 g.DrawImage(icon, bounds.Left + 1, bounds.Top + 3) 191 Else 192 g.DrawImage(icon, bounds.Left + 2, bounds.Top + 4) 193 End If 194 Else 195 ControlPaint.DrawImageDisabled(g, icon, bounds.Left + 2, bounds.Top + 4, SystemColors.HighlightText) 196 End If 197 End Sub 198 199 '功能名称: DrawSeparator(画分隔线) 200 Public Sub DrawSeparator()Sub DrawSeparator(ByVal G As Graphics, ByVal Bounds As Rectangle) 201 Dim y As Integer = Bounds.Y + Bounds.Height / 2 202 G.DrawLine(New Pen(SystemColors.ControlDark), Bounds.X + SystemInformation.SmallIconSize.Width + 7, y, Bounds.X + Bounds.Width - 2, y) 203 End Sub 204 205 '功能名称: DrawBackground(画菜单背景) 206 '参数说明: 207 'State 菜单项状态 208 'TopLevel 是否为菜单标题,即顶层菜单 209 'HasIcon 菜单项是否有图形 210 Public Sub DrawBackground()Sub DrawBackground(ByVal G As Graphics, ByVal Bounds As Rectangle, ByVal State As DrawItemState, ByVal TopLevel As Boolean, ByVal HasIcon As Boolean, ByVal enabled As Boolean) 211 Dim selected As Boolean = (State And DrawItemState.Selected) > 0 212 If (selected Or (State And DrawItemState.HotLight) > 0) Then 213 If TopLevel And selected Then 214 'draw toplevel(画顶层菜单条) 215 G.FillRectangle(New SolidBrush(ibgcolor), Bounds) 216 ControlPaint.DrawBorder3D(G, Bounds.Left, Bounds.Top, Bounds.Width, Bounds.Height, Border3DStyle.Flat, Border3DSide.Top Or Border3DSide.Left Or Border3DSide.Right) 217 Else 218 If enabled Then '如果菜单可用 219 'draw menuitem,selected or toplevel,hotlighted 220 G.FillRectangle(New SolidBrush(sbcolor), Bounds) 221 G.DrawRectangle(New Pen(sbbcolor), Bounds.X, Bounds.Y, Bounds.Width - 1, Bounds.Height - 1) 222 Else '菜单被禁用 223 G.FillRectangle(New SolidBrush(ibgcolor), Bounds) 224 Bounds.X += SystemInformation.SmallIconSize.Width + 5 225 Bounds.Width -= SystemInformation.SmallIconSize.Width + 5 226 G.FillRectangle(New SolidBrush(bgcolor), Bounds) 227 End If 228 End If 229 Else 230 If Not TopLevel Then 231 'draw menuitem,unselected 232 G.FillRectangle(New SolidBrush(ibgcolor), Bounds) 233 Bounds.X += SystemInformation.SmallIconSize.Width + 5 234 Bounds.Width -= SystemInformation.SmallIconSize.Width + 5 235 G.FillRectangle(New SolidBrush(bgcolor), Bounds) 236 Else 237 'draw toplevel,unselected menutiem 238 G.FillRectangle(SystemBrushes.Menu, Bounds) 239 End If 240 End If 241 End Sub 242 243 '功能名称: DrawMenuText(画菜单项文字) 244 '参数说明: 245 'text 菜单项文字 246 'shortcut 菜单项快捷方式 247 Public Sub DrawMenuText()Sub DrawMenuText(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal text As String, ByVal shortcut As String, ByVal enabled As Boolean, ByVal toplevel As Boolean, ByVal state As DrawItemState) 248 Dim strFormat As StringFormat = New StringFormat() 249 If (state And DrawItemState.NoAccelerator) > 0 Then 250 strFormat.HotkeyPrefix = HotkeyPrefix.Hide 251 Else 252 strFormat.HotkeyPrefix = HotkeyPrefix.Show 253 End If 254 255 Dim textwidth As Integer = System.Convert.ToInt32(g.MeasureString(text, SystemInformation.MenuFont).Width) 256 Dim x As Integer 257 Dim y As Integer = bounds.Top + (bounds.Height - SystemInformation.MenuFont.Height) / 2 258 If toplevel Then 259 x = bounds.Left + (bounds.Width - textwidth) / 2 260 Else 261 x = bounds.Left + TEXTSTART 262 End If 263 Dim brush As Brush = Nothing 264 If Not enabled Then 265 brush = New SolidBrush(Color.FromArgb(120, SystemColors.MenuText)) 266 Else 267 brush = New SolidBrush(Color.Black) 268 End If 269 '画菜单项文字 270 g.DrawString(text, SystemInformation.MenuFont, brush, x, y, strFormat) 271 strFormat.Alignment = StringAlignment.Far '设置对齐方式为右对齐 272 '画菜单项快捷方式 273 g.DrawString(shortcut, SystemInformation.MenuFont, brush, bounds.Left + 160, y, strFormat) 274 '释放资源 275 strFormat.Dispose() 276 brush.Dispose() 277 End Sub 278 #End Region 279 280 End Class 281 End Namespace 282 283
|
转载于:https://www.cnblogs.com/dazuo0312/archive/2007/04/19/720003.html