用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](e.Bounds))
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