Current location - Recipe Complete Network - Dinner recipes - VB.NET menu design elementary introduction [3]
VB.NET menu design elementary introduction [3]
5. To draw a personalized menu, first perform the following steps. The following steps are to design a simple menu through the menu editor to make the foundation for redrawing later.

Start Visual Studio Net

Select menu file | New | Project and the New Project dialog box will pop up.

Set the project type to Visual Basic project.

Set the template as a Windows application.

Enter your own menu in the name text box.

Enter the E:\VS NET project in the text box at the location, and then click the OK button, so that a folder named Draw Menu for Yourself is created in the E:\VS NET project directory, and a project file named Draw Menu for Yourself is created in it.

Switch the current window of Visual Studio Net to the Form vb (Design) window and drag the following components into the Form from the Windows Forms Components tab in the toolbox.

A MainMenu component is named MainMenu.

Select the MainMenu component, click the right mouse button, select the edit menu from the pop-up menu, and design the menu according to the interface shown in the figure.

One of the design interfaces of drawing menu items by yourself.

At this time, save the above steps and click the shortcut key F to get the interface as shown in the figure.

Draw one of the menu running interfaces by yourself.

In this way, a very common menu is completed through the menu editor, and the following menu is reformed. Before the reform, the property value of OwnerDraw of three instances of MenuItem class in the project should be set to True, because only when this property value is True will the DrawItem event and MeasureItem event required for drawing the menu be triggered, and then the next operation will be performed on the basis of the above project.

Switch the current window of Visual Stuido Net to the code editing window of Form vb and add the following code after the InitializeComponent process. The following code is used to draw the file menu item, and its function is to change the font size of the file menu item and its specific drawing method. Please refer to the comments in the following code.

Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItem Dim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) According to the DrawItemEventArgs parameter, the rectangular area of the menu item is obtained and stored in the RectangleF type instance: dim RF bounds as rectangle = new rectangle (e bounds x e bounds y e bounds height). According to the DrawItemEventArgs parameter, the rectangular area of the menu item is obtained and stored in the Rectangle type instance. The Rectangle type instance is similar to the RectangleF type instance, but the function of drawing the menu in the later code is different. LightGreen) RF bound) fills the rectangular area corresponding to the MenuItem menu item with light green color, dim s as menuitem = ctype (sender menuitem). Dim s As String = s Text the name of MenuItem menu item dim SF temp as stringformat = new stringformat (). Sf temp alignment = string alignment center sets the alignment of menu names to be drawn, and aligns e Graphics DrawString (s New Font) in the middle. New solid brush (color black) RF bound SF temp) specifies the font size in the middle alignment and redraws the menu if e state = (drawitem state no accelerator or drawitem state selected) in the specified rectangular area. Then draws the menu item e graphics fill rectangle (new solid brush (color light yellow) RF bound) according to the current drawing state of the menu item. Fill the rectangular area where the menu item is located with e Graphics DrawString (s New Font (font style bold) new solid brush (color black) RF bound SF temp). Draw an End If e DrawFocusRectangle () for the menu item name. Draw a focus box eGraphics draw rectangle (New Pen (New Solid Brush (Color Black)) RF bound within the rectangular range obtained by the DrawItemEventArgs parameter. Draw a rectangular box End Sub for the rectangular area of the menu item.

After the operation is completed, save and modify, and then click the shortcut key F to run the program to get the interface as shown in the figure.

Draw your own menu operation interface (2)

It can be seen that the MenuItem of the drawn file is not fully displayed, and the MenuItems behind it are not displayed either. This is because the display area of the menu item is not set and the default space cannot be fully displayed. Setting the display area size of the menu item is done through the MeasureItem event. The specific operation is to add the following code after the DrawItem event of Menuitem. The following code is to define the MeasureItem event of Menuitem. In this event, the width of the menu item can be set (of course, the height can also be set, etc.).

Private sub menuitem _ measure item (byval sender as object byval e as system windows forms measurementargs) handles menuitem measure item e item width = set the width of menu item End Sub.

After saving the above changes, click the shortcut key F to run the program to get the interface as shown in the figure.

Draw your own menu operation interface (3)

It can be seen that even if the file menu item is drawn, it is not displayed because other menu items are not drawn. The drawing method of other menu items is basically similar to that of file menu items. The following are the specific implementation steps for drawing other menu items on the basis of the above completion to get the menu shown in the figure.

Draw your own menu operation interface (IV)

Add the following code after the MeasureItem event handler of MenuItem in Form vb. The following code is the DrawItem event that defines MenuItem, and its function is to redraw the new menu item.

Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItem Dim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) According to the DrawItemEventArgs parameter, the rectangular area of the menu item is obtained and stored in the RectangleF type instance: dim RF bounds as rectangle = new rectangle (e bounds x e bounds y e bounds height). According to the DrawItemEventArgs parameter, the rectangular area of the menu item is obtained and stored in the Rectangle type instance. The Rectangle type instance is similar to the RectangleF type instance, but the function of drawing the menu in the later code is different. r LightGray ) rfBound )Dim s As MenuItem = CType ( sender MenuItem ) Dim s As String = s Text gets the text name dim SF temp as string format = new string format () corresponding to the menu item. Sf temp alignment = string alignment center sets the alignment of text in rectangular area SF temp line alignment = string alignment center dim RC text as rectangle f = RF bound RC text width. =e Graphics DrawString (s New Font (ì) new solid brush (color blue) RC text SF temp) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color LightGray ) ) rfBound ) If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Then  e Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound ) E Graphics DrawString (s New Font (font style bold or font style underline) new solid brush (color red) RC text SF temp)  e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) e DrawFocusRectangle ( ) Add the following code after the DrawItem event handling code of ENDIFEND SUMenuItem. The following code defines the MeasureItem event of MENUITEM. In this event, the length and height of the newly created menu item are set.

Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e item width = set the width of MenuItem e ItemHeight = set the height of menu item. End Sub adds the following code after completing the above operation steps. The following code is the DrawItem event that defines menuitem, and its function is to redraw the opened menu item.

Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItem  Dim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) According to the DrawItemEventArgs parameter, the rectangular area of the menu item is obtained and stored in the RectangleF type instance: dim RF bounds as rectangle = new rectangle (e bounds x e bounds y e bounds height). According to the DrawItemEventArgs parameter, the rectangular area of menu item is obtained and stored in the Rectangle type instance. The Rectangle type instance is similar to the RectangleF type instance, but the function of drawing menu in the following code is different: dim s as menuitem = ctype (sender menuitem).  Dim s As String = s Text  Dim sfTemp As StringFormat = New StringFormat ( )  sfTemp Alignment = StringAlignment Center  sfTemp LineAlignment = StringAlignment Center  Dim rcText As RectangleF = rfBound  rcText Width = e Graphic s DrawString ( s New Font ( Veranda ) New SolidBrush ( Color Blue ) rcText sfTemp )  e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color LightGray ) ) rfBound )  If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Then   e Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound )  e Graphics DrawString ( s New Font ( Veranda FontStyle Bold Or FontStyle Underline ) New SolidBrush ( Color Red ) rcText sfTemp )   e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound )  e DrawFocusRectangle ( ) Add the following code after the DrawItem event handling code of ENDIFEND SUMenuItem. The following code defines the MeasureItem event of MENUITEM. In this event, the length and height of the newly created menu item are set.

Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles menuitem measure item e item width = set the width of the menu item e ItemHeight = set the height of the menu item End Sub After all the above steps are correctly completed, the hand-drawn menu introduced in this paper is completed. At this time, click the shortcut key F to run the program, and you can get the running interface as shown in the figure.

VI. Summary

The main content of this paper is to introduce the design and creation of menus by VB NET, which not only introduces the static design of menus by using the menu designer, but also introduces the implementation method of dynamically creating menus by using the MainMenu class MenuItem class and the ContextMenu class. When dynamically creating, we must first understand that the menu type to be created is a drop-down menu, first create an instance of the MainMenu and then create a pop-up menu. Build a ContextMenu instance, then create the corresponding Menu according to the composition structure of the menu, that is, the parent-child relationship in the menu items, and finally display the menu. If it is a drop-down menu, the menu attribute assigned to the Form, if it is a pop-up menu, it is assigned to the visual component or the ContextMenu attribute of the Form, so that the dynamic menu creation can be displayed, and the work of dynamically creating the menu can be completed.

In addition, it also introduces the realization method and precautions of drawing personalized menu in Visual Basic Net. When drawing personalized menu, the most important thing is to master the usage of DrawItem event and MeasureItem event and the method to be used when drawing menu. Although the menu drawn in this paper is not beautiful, you can modify it through the method introduced in this paper to realize a more beautiful and personalized menu. Finally, please remember to set the OwnerDraw property of menu items to True first when drawing menu.

?

lishixinzhi/Article/program/net/20 13 1 1/ 15454