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

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
用C#和 VB. NET 实现 VS. NETOffice 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

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

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

seover="window.status='正文--用C#和 VB. NET 实现 VS. NETOffice XP 风格菜单(二)';return true">
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值