Creativity is unlimited. Use VB7 to create your personalized menu

Source: Internet
Author: User

Creativity is unlimited. Use VB7 to create your personalized menu
Modern software development, due to the significant progress in development tools, has already made programming quite simple. Can you write a good program using the same tool, to a large extent, it is not just a technical competition, but more creative competition. In addition to excellent technologies, we also need some novel things to make users feel refreshed! Today, I will start with the most commonly used menu in the software interface and teach you how to create a personalized menu!
If you haven't compiled a special processing program for the menu in VB7, although it can't be said to be ugly, it is really not beautiful compared with the menus of popular Office, XP and other programs, if you don't want your software to look like a work of the last century, start learning to write new menus!
In VB7, there are two Menu Generation Methods: one is to make the system generate automatically, and the other is to let the program generate according to a specified method. The former can generate menus without programming, but this is not what we want; so we select the latter.
Well, the first step is to put a MainMenu control on the form first! As for the menu title, you can write it as you like. For demonstration, my menu is as follows:
File MenuItem1
Create MenuItem2
Enable MenuItem3
Save MenuItem4
Exit MenuItem5
Run the form and we can see four menu items about the file appear under the "file" menu. Okay. The first step is successful! But this is only the first step! Next, let's assume that there are some icon files on your hand, which are C:/file. ico, C:/new. ico, C:/open. ico, C:/save. ico, C:/exit. ico, of course, if you don't have it, you can also use other icons to temporarily replace it, as long as it works! Now, since we do not want the program to automatically generate menus, we can modify the OwnerDraw attribute of these menu items, find OwnerDraw in the menu attribute box, and set it to True. (If you run it again at this time, we will not be able to see the menu because there is no way to generate the menu !)
Before proceeding, I would like to briefly introduce the Menu Generation principle. The Menu Generation depends on two processes: MeasureItems and DrawItem. The former is used to Measure the menu size (Measure), while the latter is used to Draw (Draw) based on the measurement ), what we need to do is to re-write these two protected events so that the menus can be drawn (displayed) as needed ).
Add a module. The code for adding these two events below may be a little long for beginners, but reading them all over is definitely a lot of benefit !! I will try to give all comments.
Imports System
Imports System. ComponentModel
Imports System. Drawing
Imports System. Drawing. Drawing2D
Imports System. Drawing. Text
Imports System. Windows. Forms
'The above are some namespaces used by this module (just some prefixes! Note that writing this is only to simplify the following writing, and there is no other practical function .)
Module IconsMenuMain
Sub MeasureItems (ByVal EvMeasureItem As System. Windows. Forms. MeasureItemEventArgs, ByVal Mi As MenuItem, ByVal MyFont As Font)
'Start the measurement menu event
Dim sf As StringFormat = New StringFormat ()
'New character format object
Sf. HotkeyPrefix = HotkeyPrefix. Show
'Specifies the display type of text-related hotkey prefixes. Set it to display
Sf. SetTabStops (60, New Single () {0 })
'The first parameter firstTabOffset indicates
'The number of spaces between the beginning of a text line and the first tab.
'Tabstops () indicates the second parameter.
'Distance between the operators (expressed by spaces.
EvMeasureItem. ItemHeight = 22 'sets the height of the project, preferably the height of the font !!
EvMeasureItem. ItemWidth = CInt (EvMeasureItem. Graphics. MeasureString (GetRealText (Mi), MyFont, 10000, sf). Width) + 10' set the project Width
Sf. Dispose ()
Sf = Nothing 'Destroy object
End Sub
Note: The above event determines the menu size, that is, the menu height and width. If you are interested, you can set the height constant to 22, the value-added constant 10 in the width is changed to the event parameter! In this way, there are menus of different heights! In the following event, I changed the starting color and final color of the form menu to a parameter to control the text gradient background in each menu!
Sub DrawItems (ByVal EvDrawItems As System. Windows. Forms. DrawItemEventArgs, ByVal Mi As MenuItem, ByVal m_Icon As Icon, ByVal StartColor As Color, ByVal EndColor As Color, ByVal MyFont As Font)
Dim br As Brush 'defines the Brush
Dim fdisposebrush as Boolean 'whether to destroy the brush flag
If not m_icon is nothing then
'If no icon exists
If not mi. Checked then
'No matter whether the menu item is selected or not, draw the icon first, even though the latter has no evdrawitems. graphics. drawicon (m_icon, evdrawitems. bounds. left + 2, evdrawitems. bounds. top + 2)
Else
Evdrawitems. Graphics. drawicon (m_icon, evdrawitems. bounds. Left + 2, evdrawitems. bounds. Top + 2)
Dim npen as system. Drawing. Pen
If not mi. enabled then
Npen = new pen (color. darkgray) 'Fill in the color darkgray if available
Else
Npen = new pen (color. Gray) 'otherwise use the color gray
End if
Evdrawitems. Graphics. drawrectangle (npen, 1, evdrawitems. bounds. Top, 20, 20)
Evdrawitems. Graphics. drawrectangle (npen, 3, evdrawitems. bounds. Top + 2, 16, 16)
'Draw two boxes, and the two boxes form a "back" font
End if
Else
'If an icon exists
If Mi. Checked then', if the menu is in the check status
Dim npen as system. Drawing. pen creates a paint brush object.
If not mi. enabled then determines whether the paint brush color is available.
Npen = new pen (color. darkgray)
Else
Npen = new pen (color. Gray)
End if
Evdrawitems. Graphics. drawrectangle (npen, 1, evdrawitems. bounds. Top, 20, 20)
'If an icon is selected at the same time, you only need to draw a large box in the "back" font.
Dim PNTs () as point
The three points of ReDim Pnts (2) are a "checkmark" position.
Pnts (0) = New Point (15, EvDrawItems. Bounds. Top + 6)
Pnts (1) = New Point (8, EvDrawItems. Bounds. Top + 13)
Pnts (2) = New Point (5, EvDrawItems. Bounds. Top + 10)
'The color of the tick is determined based on whether it is available and it is checked"
Of course, you can create your own menu Check image as needed.
If Mi. Enabled Then
EvDrawItems. Graphics. DrawLines (New Pen (Color. Black), Pnts)
Else
EvDrawItems. Graphics. DrawLines (New Pen (Color. Gray), Pnts)
End If
End If
End If

Dim rcBk As Rectangle = EvDrawItems. Bounds
RcBk. X + = 24' first obtains a rectangle of the same size as the menu, and then shortens the X length of the rectangle. The shortened size is the size of the icon, this rectangle will be used as the filling range of the background color of the menu text.
'Determines whether to use a color gradient brush based on whether the menu item is selected,
'If Yes, set the paint color and set whether to destroy the paint flag to true.
If CBool (EvDrawItems. State And DrawItemState. Selected) Then
Br = New LinearGradientBrush (rcBk, StartColor, EndColor, 0)
Set the start color and final color
FDisposeBrush = true' sets the paint destruction flag
Else
Br = SystemBrushes. Control 'the color is the default color of the system.
End If
'Draw the background color of the gradient menu text
EvDrawItems. Graphics. FillRectangle (br, rcBk)
If fDisposeBrush Then br. Dispose () ', the instance of the gradient color brush created using the New method must be destroyed, but the previous statement is still valid.
Br = Nothing: temporarily clear the entire brush
Dim sf As StringFormat = New StringFormat ()
Sf. HotkeyPrefix = HotkeyPrefix. Show
'Specifies the display type of text-related hotkey prefixes. Set it to display
Sf. SetTabStops (60, New Single () {0 })
'The first parameter firstTabOffset indicates
'The number of spaces between the beginning of a text line and the first tab.
'Tabstops () indicates the second parameter.
'Distance between the operators (expressed by spaces.
If Mi. enabled then' determines the color of the image brush based on whether the menu is available.
BR = new solidbrush (evdrawitems. forecolor)
Else
BR = new solidbrush (color. Gray)
End if
'Write on the menu
Evdrawitems. Graphics. drawstring (getrealtext (MI), myfont, BR, evdrawitems. bounds. Left + 25, evdrawitems. bounds. Top + 2, SF)
Br. Dispose ()
Br = Nothing
Sf. Dispose ()
Sf = Nothing
End Sub
Function GetRealText (ByVal Mi As MenuItem) As String
This event is used to obtain the menu text.
Dim s As String = Mi. Text 'to obtain the original menu Text
If Mi. ShowShortcut And Mi. Shortcut <> Shortcut. None Then
Dim k As Keys = CType (Mi. Shortcut, Keys)
'If there is a menu shortcut key, add the shortcut key text to the menu text
S = S & convert. tochar (9) & typedescriptor. getconverter (GetType (KEYS). converttostring (k)
End if
Return s
End Function
Note: You can change the menu text to any text you want!
End Module
Now, let's take a look at our actual results. Write the code on the original form !!
The following uses the "beautification" first and second menu items as an example. Of course, the general main menu should not have a graph. After reading this, you can draw your menus based on your imagination.
Private sub menuitemdetail measureitem (byval sender as object, byval e as system. Windows. Forms. measureitemeventargs) handles menuitem1.measureitem
The first measurement event, pay special attention to the "handles" mark!
Dim myfont as new system. Drawing. Font ("Arial", 8) 'defines a font you like
Measureitems (E, menuitem1, myfont)
End sub
Private sub menuitemincludrawitem (byval sender as object, byval e as system. Windows. Forms. drawitemeventargs) handles menuitem1.drawitem
Dim IC as new icon ("C:/file. ICO") 'defines an icon you like
Dim myfont as new system. drawing. font ("Arial", 8) 'should be the same as the front font. Of course, if you want to, you can also use the new font to write within the specified range of the front font, but it is better not to set the Font too much, or unexpected consequences may occur!
Drawitems (E, menuitem2, IC, color. Blue, color. Yellow, myfont)
End sub
Private sub menuitem2_drawitem (byval sender as object, byval e as system. Windows. Forms. drawitemeventargs) handles menuitem2.drawitem
E. Graphics. compositingquality = drawing. drawing2d. compositingquality. highquality
Dim Ic As New Icon ("C:/open. ico ")
Dim MyFont As New System. Drawing. Font ("Arial", 14)
DrawItems (e, MenuItem2, Ic, Color. Tomato, Color. LavenderBlush, MyFont)
End Sub
Private Sub MenuItem2_MeasureItem (ByVal sender As Object, ByVal e As System. Windows. Forms. MeasureItemEventArgs) Handles MenuItem2.MeasureItem
Dim MyFont As New System. Drawing. Font ("Arial", 14)
MeasureItems (e, MenuItem2, MyFont)
End Sub
Run it and you will get a main menu with icons. The font and background colors of each menu item are different! This is just a demonstration, but it is best not to design it like this in real software. The menu should be consistent.
This article is just a basic way to modify menus. I hope you can make full use of your creativity and customize your menus!
 

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.