Henry Instructions - NET WinForm Menu DIY

zhaozj2021-02-16  49

Henry Instructions - NET WinForm Menu DIY

Han Rui (2003.7.29)

The weather is very hot, and Shanghai has reached the highest temperature in the past six decades. In order to prevent the heat stroke, he has to retain self-study. Good mood, write a little small things with the reader, I hope everyone will get a relaxed and comfortable in the heat.

Today, I will discuss the transformation of the menu. There are many good controls on the Internet, you can implement it in the Menu, but there are few fonts in the Menu with colors. In fact, we can achieve the goal of plus pictures and changed fonts and colors through the operation of the .NET comes with the control. This article is mainly to discuss changes in fonts and colors. As shown below:

Through the change of menu items, "Weather Trim" Label on the form changes the font and foreground color accordingly.

Some friends will ask, is the general control not to set up font, forecolor, the backcolor property does not reach Henry to do something? But Menu can't work, because Menu's parent class is Component, not Control! And those attributes are Control classes, if MENU is inherited from the Control class, of course, there will be no meaning of this Xiaowen. So we should call menu as a component to show the difference between Control's derived control. When you use the control in the future, you should also pay attention to what its parent class is to clarify whether you can implement a function.

Menu contains three classes: MainMenu (main menu), ContextMenu (pop-up menu), MenuItem (menu item) where MainMenu and ContextMenu are containers, and the MenuItem class provides properties that enable us to configure menu items. Therefore, our work is mainly for Menuitem. The above figure is implemented in the ContextMenu menu, the form of MainMenu is also the same.

Now we have to break the routine thing, so set MenuiteM's OwnerDraw to True, and take over the weight of the menu item, and take a DIY.

Then let's analyze it. After the OwnerDraw is set to True, which events or methods are needed to be rewritten. You can see the MSDN: DrawItem event occurs when OwnerDraw is set to True, then check it to see how the event data in the event handler is defined.

[MSDN]: The event handler receives a parameter for a DrawItemMeventArgs type that contains data related to this event. The following DrawITeventArgs properties provide information specific to this event.

Attributes

Description

Attributes

Description

Backcolor Gets the background color of the item drawn

Graphics

Get graphics surfaces to be drawn on it

Bounds

Get rectangles that represent the boundaries of the drawn

Index

Get the index value of the drawn items

Font gets fonts assigned to the drawn

State

Get the status of the drawn

Forecolor Gets the foreground color of the drawn

Sure enough, the three properties we need are all here! So, don't we do this? I implemented it by an example of MSDN, but the effect of coming is not right, and the width and height of ContextMenu are only a little big. It seems that there is no width and height of Menuitem.

Where is the wide and high of Item? There is no two items in the MenuItem properties, found to find it, and finally found in the MeasureItem event.

Attributes

Description

Graphics

Get the graphics object to be measured

Index

Get or set items that require high and width ItemHeight

Get or set the height specified by INDEX

Itemwidth

Get or set the width specified by INDEX

That is, we want to draw MenuItem, we must first calculate the width and height of Items to be drawn. It will be described herein that the width and height of the item are not only determined by the text section, but also depends on the image portion in the item, so our calculation formula is:

Item width = width of the picture width of the text

The height of the item = max (the height of the picture, the height of the text)

This article does not join a custom image, just joining the option symbol of a dot, which is implemented by the DrawMenuglyph sharing method. So how is its height and width defined? Very simple, using SystemInformation.MenuChecksize to identify the size of the image selected by the selected menu item. The default size of the dot is 13 pixels. DrawMenuglyph is drawn on the size of the dot according to the position of the picture we give. So, we must also give the size of the picture in the DrawItem event:

Image of the height = the height of the text

Picture of width = picture default width * Zoom in multiple = the default width of the picture * The default height of the height of the text

So much in dizzy, or use code to describe clearer:

Dim OldMenuItem as new menuItem ()

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

Me.ContextMenu = new contextmenu ()

Dim menutext as new arraylist ()

Menutext.Add ("Song")

Menutext.Add ("black body")

Menutext.add ("体 _GB2312")

DIM MyMenuItem (Menutext.count - 1) AS MenuItem

DIM I as integer

For i = 0 to mymenuitem.length - 1

MyMenuItem (i) = new menuitem (menutext (i))

MyMenuItem (i) .OWNERDRAW = true 'Every item is set

MyMenuItem (i) .radiocheck = true 'Every option button is turned on

'Pointing the handler that clicks the event

AddHandler MyMenuItem (i) .click, _

New EventHandler_Click (Addressof Menuitem_Click)

'Point a handler for MeasureItem events

AddHandler MyMenuItem (i) .measureItem, _

New MeasureItem_Measure (Addressof MenuItem_MeasureItem)

'Pointing a handler for DrawItem events

AddHandler MymenuItem (i) .drawItem, _

New DrawItem_DrawItem_DrawItem

ContextMenu.MenuItems.Add (MyMenuItem (i))

Next I

'Record the initial selected item

OldMenuItem = contextMenu.MenuItems (0)

OldMenuItem.checked = true

End Sub

Private Sub Menuitem_Click (Byval Obj As Object, Byval E As Eventargs) OldMenuItem.checked = false 'changes the selected status of the original menu item

OldMenuItem = CTYPE (OBJ, MENUITEM) 'Transformation Processing

OldMenuItem.checked = true 'Sets the selection status of the menu item in the selection

SELECT CASE OLDMENUITEM.TEXT 'Sets Label's color according to the menu content

Case "Song"

Label1.forecolor = color.red

Case "black body"

Label1.Forecolor = color.green

Case "体 _GB2312"

Label1.Forecolor = color.blue

End SELECT

'Set the Label font based on the menu content

Label1.font = new font (OldMenuItem.text, label1.font.size)

Invalidate ()

End Sub

Private Sub Menuitem_MeasureItem (Byval Obj As Object, ByVal E As MeasureITeventArgs)

DIM MI As MenuItem = CType (Obj, MenuItem)

DIM FNT AS New Font (MI.TEXT, 12, FONTSTYLE.BOLD) '12 is the size of the font

DIM SZF as sizef = E.Graphics.MeasureString (mi.text, fnt, 1000)

'Get the width of the text

E.ItemWidth = CINT (Math.ceiling (SZF.WIDTH))

'Get the height of the text

E.ItemHeight = CINT (Math.ceiling (SZF.HEIGHT))

'The width of the item = the width of the text the width of the picture

E.Itemwidth = systeminformation.MenuChecksize.width * _ _

E.itemheight / systeminformation.Menuchecksize.Height

End Sub

Private Sub Menuitem_DrawItem (Byval Obj As Object, _

ByVal E as DrawIndaTargs)

DIM MI As MenuItem = CType (Obj, MenuItem)

Dim g as graphics = E.Graphics

DIM Br AS BRUSH

DIM FNT AS New Font (MI.TEXT, 12, FONTSTYLE.BOLD) '12 is the size of the font

Dim RectCheck as Rectangle = E.Bounds' This is the height of the tab of the banner to the height of the text

RectCheck.width = systeminformation.MenuChecksize.width * _

RectCheck.height / SystemInformation.MenuChecksize.height

Dim RectText As Rectangle = E.BOUNDs

RectText.x = RectCheck.width 'iTEM width = width text width

E. DrawBackground () 'Rewinds this function, you can implement the XP effect of the selected item

'Draw the representation of the selected dots. If you want to join the picture, please consider if (E.State and DrawItemState.checked <> 0 THEN)

Controlpaint.drawmenuglyph (g, reccheck, menuglyph.bullet)

END IF

'Setting the outlambers of the option

IF (E.State and DrawItemState.selected) <> 0 THEN

BR = systembrushes.highlighttext 'selected will become blue bottom white

Else

Select Case Mi.Text

Case "Song"

Br = new solidbrush (color.red)

Case "black body"

Br = new solidbrush (color.green)

Case "体 _GB2312"

Br = new solidbrush (color.blue)

End SELECT

END IF

g.drawstring (mi.text, fnt, br, rectanglef.op_implicit (RectText))

End Sub

If you do it, you will soon understand the principle of implementation. Then expand it, you can achieve the ubiquitous menu. This article only has a head, more in-depth research, and you need you to study it yourself.

----

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

QQ: 18349592

E-mail: Henry7685@hotmail.com

Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/

转载请注明原文地址:https://www.9cbs.com/read-25536.html

New Post(0)