XP风格的菜单类(VB.net)以及使用方法

由 2006-7-3 13:47:00 By:废客泉

XP风格的菜单类(VB.net)

'/
'  名  称:   XP Style Menu
'  作  者:   DeityFox
'  E-Mail:   daniel_0571@163.com
'  修  改:   2002.5.15
'  类  名:   xpMenu
'/

'======================================
'废客提供使用说明
'======================================
'首先确认你的工程项目里有菜单项
'然后添加一个新类并命名为xpmenu.vb
'把这个文当原文不动的复制到 xpmenu.vb 类文件里
'再打开你的菜单使用窗体的代码页
'展开"Windows 窗体设计器生成的代码"项
'将所有的 System.Windows.Forms.MenuItem 替换为
'xpmenuitem.xpMenu 即可
'当你选中菜单项点属性时,里面会多出一项MenuItemIcon
'这样你就可以设置菜单图标了.

 

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

 

转载于:https://www.cnblogs.com/dazuo0312/archive/2007/04/19/720003.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值