Implement Vs.net or Office XP-style menus with C # and vb.net (ii)
Source: Internet
Author: User
Menus implement Vs.net or Office XP-style menus with C # and vb.net
Stingy God, 2001.08.18
2. "Owner-drawn Menus" technology
This example is vb.net syntax. I removed classes that are not related to the menu, because there are too many errors, and you encounter problems with the porting of class libraries and namespaces:
The most is the Beta1 system.winforms and Beta 2 of the System.Windows.Froms namespace problem;
Then the Beta1 in the Bitand, bitor, etc. bitxxx function in the BETA2 has been removed bit and VB in the same (it is said that the Beta1 of this change has been the total number of VB fans complaints, that can not be VB also C #, bit is what dongdong), This way you need to get rid of this type of function;
Then is nameobjectcollectionbase from the original system.collections deleted, Beta2 put in system.collections.specialized, really some faint, At first I thought that the class was deleted in Beta2.
Finally, some overrides and overloads issues, specific to see vs.net or framework SDK Beta 2 compile-time prompts on it, this aspect of Ms do well, the Task list tells you specific suggestions, just do it.
Specifically, you can find these two files in the Doc directory of the Framework SDK Beta 2 installation directory, which is a good guide file for porting from Beta1 to Beta2: apichangesbeta1tobeta2.htm and Change List- Beta1 to Beta2.doc especially this doc file has more than 90 pages, but it is helpful.
I hope you will be able to stay awake after all the errors have been eliminated and find the core and useful code to analyze. Mainly Cactionmenu.vb, the focus is on the OnMeasureItem and OnDrawItem functions or the event handlers. OnMeasureItem mainly deals with the ItemHeight and Itemwidth of MenuItem, which is known from the number of MeasureItemEventArgs parameters it passes. OnDrawItem is mainly about how to draw the menu question. The keyword overrides indicates that we want to redefine the two methods in the MenuItem in the subclass.
Rows from 56 to 58 are OnMeasureItem functions:
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 provides 4 properties Graphis, Index, ItemHeight, and Itemwidth. Me is equivalent to the this keyword in C # or Java. Fnt. Dispose () is a very interesting function call, in the past in Windows programming, such as fonts, brushes and many other resources are expected to quickly use fast release, this statement is used to control the GC (garbage collection), I have used up this equipment or resources, GC you can take it back.
Rows from 70 to 146 are related to the Onitemdraw function:
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 are nothing Then
Dim il as ImageList = Me.Action.ActionList.ImageList
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
The DrawItemEventArgs parameter gives you all the environment and information related to the menu, which includes 6 attributes: Bounds, Font, ForeColor, Graphics, Index, States. If you have previously used GDI functions under Windows, that must be very familiar with these functions, not very complex only need you a little arithmetic knowledge and art point of view on it, if you are the first time then draw a few rectangular pieces on the paper can be understood and done well, compared to the previous TC under the menu programming much easier. Is mainly the author is how to put icon on the menu, and then according to different states performance of the menu ForeColor, bounds is the menu item in front of the selection and so on the small square.
Okay, the second part involves most of the technical details, and here's what you need to focus on, how to draw it, and the next section we'll look at how it looks better, like vs.net or Office XP.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service