用C#和VB.NET实现VS.NET或Office XP风格的菜单
小气的神 2001.08.18
2 . “Owner-drawn menus” 技术
这个例子是 VB.NET 语法的 . 我去掉了和 Menu 无关的 Class ,原因是错误太多,你会遇到类库和命名空间的移植性的问题:
最多的是 Beta1 System.WinForms 和 Beta 2 的 System.Windows.Froms 的命名空间问题;
然后是 Beta1 中的 BitAnd 、 BitOR 等等 Bitxxx 的函数在 Beta2 中已去掉了 Bit 又和 VB 中一样了(据说 Beta1 的这项改动遭到了总多 VB Fans 的投诉,说不能把 VB 也 C #化, Bit 是什么东东) , 这样你需要把这类函数改掉;
然后是 NameObjectCollectionBase 从原来的 system.collections 中删除了, Beta2 放在 system.collections.specialized 中,真的有些昏倒,开始我还以为 Beta2 中删除了这个类。
最后是一些 Overrides 和 Overloads 的问题,具体的看 VS.NET 或 Framework SDK Beta 2 编译时的提示就可以了,这方面 MS 做得不错, Task list 中告诉你具体得建议,照做就是了。
具体一点你可以在 Framework SDK Beta 2 安装目录的 Doc 目录中找到这两个文件,这是从 Beta1 移植到 Beta2 上不错的指导文件: APIChangesBeta1toBeta2.htm 和 Change List - Beta1 to Beta2.doc 特别是这个 doc 文件洋洋洒洒 90 多页,但很有帮助。
希望你还能在排除所有的错误之后保持清醒,找到最核心有用的代码,来分析。主要是 CActionMenu.vb ,焦点在 OnMeasureItem 和 OnDrawItem 这两个函数或说事件处理程序上。 OnMeasureItem 主要是处理 MenuItem 的 ItemHeight 和 ItemWidth 的,从它传的 MeasureItemEventArgs 参数数就知道。 OnDrawItem 主要是如何画菜单的问题。关键字 Overrides 表明我们要在子类中重新定义 MenuItem 中的这两个方法。
从 56 行到 58 行是 OnMeasureItem 函数:
Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
If Me.Action.Caption = "-" Then
e.ItemHeight = 5
Else
e.ItemHeight = 20
End If
Dim fs As FontStyle
If Me.DefaultItem = True Then fs = fs Or FontStyle.Bold
Dim fnt As New Font("Tahoma", 8, fs)
Dim sf As SizeF = e.Graphics.MeasureString(Me.Action.Caption, fnt)
fnt.Dispose()
e.ItemWidth = CInt(sf.Width) + 20
End Sub
MeasureItemEventArgs 提供 4 个属性 Graphis 、 Index 、 ItemHeight 和 ItemWidth 。 Me 相当于 C #或 Java 的 this 关键字。 fnt.Dispose() 中 Dispose 是一个很有意思的函数调用,在以往的 Windows 编程中象字体、画笔等许多资源都希望快使用快释放,这个语句是用来控制 GC ( garbage collection )的,意思是我已使用完了这个设备或资源, GC 你可以收回了。
从 70 到 146 行是有关 OnItemDraw 函数的:
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
' colors, fonts
Dim clrBgIcon, clrBgText, clrText As Color, fs As FontStyle, fnt As Font
Dim b As SolidBrush, p As Pen
Dim fEnabled As Boolean = Not CType(e.State And DrawItemState.Disabled, Boolean)
Dim fSelected As Boolean = CType(e.State And DrawItemState.Selected, Boolean)
Dim fDefault As Boolean = CType(e.State And DrawItemState.Default, Boolean)
Dim fBreak As Boolean = (Me.Action.Caption = "-")
If fEnabled And fSelected And Not fBreak Then
clrBgIcon = Color.Silver
clrBgText = Color.White
clrText = Color.Blue
fs = fs Or FontStyle.Underline
Else
clrBgIcon = Color.Gray
clrBgText = Color.Silver
clrText = Color.Black
End If
If Not fEnabled Then
clrText = Color.White
End If
If fDefault Then
fs = fs Or FontStyle.Bold
End If
fnt = New Font("Tahoma", 8, fs)
' total background (partly to remain for icon)
b = New SolidBrush(clrBgIcon)
e.Graphics.FillRegion(b, New Region)
b.Dispose()
' icon?
If Not Me.Action.ActionList Is Nothing Then
Dim il As ImageList = Me.Action.ActionList.ImageList
If Not il Is Nothing Then
Dim index As Integer = Me.Action.Image
If index > -1 And index < il.Images.Count Then
Dim rect As Rectangle = e.Bounds
With rect
.X += 2
.Y += 2
.Width = 16
.Height = 16
End With
e.Graphics.DrawImage(il.Images.Item(index), rect)
End If
End If
End If
' text background
Dim rf As RectangleF
With rf
.X = 18
.Y = e.Bounds.Y
.Width = e.Bounds.Width - .X
.Height = e.Bounds.Height
End With
b = New SolidBrush(clrBgText)
e.Graphics.FillRegion(b, New Region)
b.Dispose()
' text/line
rf.Y += 3 : rf.Height -= 3
If Not fBreak Then
b = New SolidBrush(clrText)
e.Graphics.DrawString(Me.Action.Caption, fnt, b, rf)
fnt.Dispose()
b.Dispose()
Else
p = New Pen(Color.Black)
rf.Y -= 1
e.Graphics.DrawLine(p, rf.X, rf.Y, rf.Right, rf.Y)
p.Dispose()
End If
' border
If fEnabled And fSelected And Not fBreak Then
p = New Pen(Color.Black)
e.Graphics.DrawRectangle(p, e.Bounds)
p.Dispose()
End If
End Sub
DrawItemEventArgs 参数给了你和菜单相关的所有环境和信息,它包括 6 个属性: Bounds 、 Font 、 ForeColor 、 Graphics 、 Index 、 States 。如果你以前用过 Windows 下的 GDI 函数,那一定很熟悉这些函数,不是很复杂只需要你一点点算术知识和美术观点就可以了,如果你是第一次那么在纸上画几个矩形块就可以了理解和做的很好,比起以前 TC 下的菜单编程容易得多。主要是作者是如何把 Icon 画在菜单上的,然后是根据不同的 States 表现一下菜单的 ForeColor, Bounds 就是菜单项最前面的表示选中等等的小方块。
好了第二部分涉及到了大部分技术细节了,这里你需要关注的是,如何画出来,下一部分我们来看如何画的好看些,象 VS.NET 或 Office XP 那样子。