用C#和VB.NET实现VS.NET或Office XP风格的菜单(二)

类别:.NET开发 点击:0 评论:0 推荐:

用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的投诉,说不能把VBC#化,Bit是什么东东),这样你需要把这类函数改掉;

然后是NameObjectCollectionBase从原来的system.collections中删除了,Beta2放在system.collections.specialized 中,真的有些昏倒,开始我还以为Beta2中删除了这个类。

最后是一些Overrides Overloads的问题,具体的看VS.NETFramework 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,焦点在OnMeasureItemOnDrawItem这两个函数或说事件处理程序上。OnMeasureItem主要是处理MenuItemItemHeightItemWidth的,从它传的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个属性GraphisIndexItemHeightItemWidthMe相当于C#或Javathis关键字。fnt.Dispose()Dispose是一个很有意思的函数调用,在以往的Windows编程中象字体、画笔等许多资源都希望快使用快释放,这个语句是用来控制GCgarbage collection)的,意思是我已使用完了这个设备或资源,GC你可以收回了。

 

70146行是有关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

                        .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](rf))

        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个属性:BoundsFontForeColorGraphicsIndexStates。如果你以前用过Windows下的GDI函数,那一定很熟悉这些函数,不是很复杂只需要你一点点算术知识和美术观点就可以了,如果你是第一次那么在纸上画几个矩形块就可以了理解和做的很好,比起以前TC下的菜单编程容易得多。主要是作者是如何把Icon画在菜单上的,然后是根据不同的States表现一下菜单的ForeColor, Bounds就是菜单项最前面的表示选中等等的小方块。

好了第二部分涉及到了大部分技术细节了,这里你需要关注的是,如何画出来,下一部分我们来看如何画的好看些,象VS.NETOffice XP那样子。

 

本文地址:http://com.8s8s.com/it/it46305.htm