人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

用C#與VB.NET完成VS.NET或Office XP風格的菜單(二)

用C#與VB.NET完成VS.NET或Office XP風格的菜單(二)

更新時間:2022-06-07 文章作者:未知 信息來源:網絡 閱讀次數:

用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

.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.NET或Office XP那樣子。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 辽中县| 白水县| 弥勒县| 民权县| 军事| 米易县| 方正县| 长垣县| 保德县| 长白| 景洪市| 兴化市| 武乡县| 南召县| 全椒县| 隆德县| 滨州市| 平南县| 如皋市| 文山县| 平潭县| 海丰县| 阿拉尔市| 海林市| 延庆县| 太康县| 资兴市| 剑川县| 维西| 东丽区| 乐业县| 兴国县| 句容市| 东海县| 普定县| 宜丰县| 长寿区| 九江市| 琼海市| 连云港市| 荥经县|