Visual Basic .NET ?? Visual Basic 2005 ??? ??? ????? ???? ?? ????? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 888168 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

??????? ?? ???????? ?? ??? ?? ?????? Microsoft Visual Basic .NET ?? Microsoft Visual Basic 2005 ??? ??? ????? ???? ????? ?? ??? ??????

?????

This article describes how to create a custom menu in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005. You create a custom menu by inheriting theMainMenucontrol and by adding properties to the items of theMainMenu???????? ???

Extend the MenuItem class

TheMenuItemclass does not provide a built-in way of displaying an icon. For example, there is no?????property that you can set to display an icon. When a Microsoft .NET Framework class does not provide a feature that you want, you can extend that class, and you can provide the feature yourself.

??????? ?? ???? ???MenuItem???? ?? ?????? ?????? ???? ???? ??? ???? ???? ?????? ?????? ???? ???? ?? ???? ?? ?? ?????? ????? ?? ??? ????????? ??MenuItem???? ?? ??????? ?? ???

??????? ???? ?? ???MenuItem????, ????? ???? ?????????? ???????? ?? ????? ?????
Public Class MyIconMenu
    Inherits MenuItem
End Class

??? ????? ???????? ?? MenuItem ???? ?? ??? ???? ????? ???????? ?????

?? ?? inherit ??MenuItem???? ?? ???, ???? inherit ???? ???, ???????, ?? ?????? ??? ???? ??? ????? ???? ????? ???? ????? ???? ?? ??? ?? ??????? inherit ??? ?? ??????? ?? ??????? ??? ?? ??????? ?? ???????OnMeasureItemmethod and of theOnDrawItem?????? ??? You must also declare private fields within your custom class to hold a?????object and an????????????? ??? You need a?????object so that your custom class can contain text in addition to icons.

If you require a way for developers who use your custom class to initialize your private fields, use the constructor of your custom class. A Visual Basic .NET 2003 constructor is a special method that is called???. The???method is called by the .NET Framework when your custom class is instantiated. ?? ????? ?? Visual Basic .NET 2003, ???? ???????? ?? ???? constructor ?? ??? ?? ???? ????

Visual Basic .NET 2003 ???? overloading ?? ?????? ????? ?? ?? ???? ??? ????????? ??? ???? ?? ???, ?? ?? ???? ????????? ????? ????

?? ????? ???? constructors ?? ???? overloading, ???????? ???? ????? ???? ?? ???? ????????? ?? ??????? ??? ???? ???? ??? ?? ???? ????? ???? ?? ??? ????? ??? ?? ?????? ?? ??? ????? ?? ???? ???
Public Class MyIconMenu Inherits MenuItem
       	Private font As Font
        Private icon As Icon

            Sub New(ByVal menuText As String)
                   MyClass.New(menuText, Nothing, Shortcut.None, Nothing)
           	End Sub

            Sub New(ByVal menuText As String, ByVal handler As EventHandler,  ByVal shortcut As Shortcut, ByVal ico As icon)
                MyBase.New(menuText, handler, shortcut)
                Me.icon = ico
                Me.font = New Font("Arial", 8)
                Me.OwnerDraw = True
            End Sub
   End Class
Notice that you have used method overloading on your constructor so that the developer has the option of having a menu item with an icon or a menu item without an icon. You must also set theOwnerDrawproperty of your custom class to theTruevalue if you want to display an icon. This step is important because if this step is not completed, no icons are displayed on your menu.

Add items to your menu

Examine the two methods that must be overridden. TheOnMeasureItemmethod is called when your menu is drawn. TheOnMeasureItem???? ???? ??? ?? ?? ??? ??? ?? ???? ???? ?? ???? ????????? ????MeasureItemEventArgsconstructor. ?? ?? ???MeasureItemEventArgs?? ??? constructor ???ItemHeight??? ??ItemWidth???? ?? ???????? ?? ???????? ?? ??? ??? ??? ??? ???OnMeasureItem?????? ???

?? ????? ?? ????? ?? ?????? ???? ???? ?? ????? ?? ????????? ???? ?? ??? ?? ????? ?? ?? ???? ?? ?? ???? ????? ??????? ??? ??????????, ???? ???? ?? ??? ?? ????? ????? ?? ????? ?? ???? ??? ???? ??????

???? ???? ?? ?????? ?? ?????????, ?? ??? ???????? ??? ???? ???? ????????? ?? ????? ??? ???? ?????? ?? ????? ?? ???? ???StringFormat?? ??????? ??????? ???? ?? ??? ???????? ??? ?? ???????? ??? ?????? ???? ?? ????System.Drawing??? ????? ??? ?? ?? ???? ???? ?? ????? ????? ??, ?? ?? ????? ?? ????? ???? ?????

??? ?? ??? ????OnMeasureItem???? ????? ??? ?? ?????? ?? ???? ?? ???? ???
Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)
        MyBase.OnMeasureItem(e)
        Dim sf As StringFormat = New StringFormat()

        sf.HotkeyPrefix = HotkeyPrefix.Show
        sf.SetTabStops(50, New Single() {0})

        e.ItemHeight = Me.icon.Height + 6
        e.ItemWidth = CInt(e.Graphics.MeasureString(AppendShortcut(), _ 
                           Me.font, 1000, sf).Width) + Me.icon.Width + 5
        sf.Dispose()
        sf = Nothing
End Sub

???? ???? ?????

?? ?? ???? ???? ?? ???? ????????? ????, ?????? ?????? ???? ???? ???? ?? ????? ??OnDrawItem???? ????? ???? ?? ?????? ??? ?? ???? ?? ??? ???? ??? ??DrawItemEventArgs???????? ??? ?? ????? ?? ???? ???DrawItemEventArgs???????? ?? ??????? ???? ?? ??? ???????????????????? ???? ???? ???? ?? ??? ??? ?? ???? ????? GDI + ???????? ??? ?? .NET Framework ??? ?????? ?? ?? ????? ?? ???? ???? ?? ??? ?? ??? ???? ??? ????, ??? ????????? ??? ?????? ???, ????? ??????? ????? ???, ???? ???? ?? ??? ?? ???????

??? ?? ???? ???? ?? ??? ????? ??? ?? ?????? ?? ???? ?? ???? ???
Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
   Dim br As Brush
   Dim sf As StringFormat

   MyBase.OnDrawItem(e)
   e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds)
   If Not (Me.icon Is Nothing) Then
      e.Graphics.DrawIcon(Me.icon, e.Bounds.Left + 3, e.Bounds.Top + 3)
   End If

   sf = New StringFormat()
   sf.HotkeyPrefix = HotkeyPrefix.Show
   sf.SetTabStops(50, New Single() {0})
   br = New SolidBrush(SystemColors.WindowText)
   e.Graphics.DrawString(AppendShortcut(), Me.font, br, e.Bounds.Left + _ 
                         Me.icon.Width + 10, e.Bounds.Top + 2, sf)
   'Clean up resources.
   br.Dispose()
   br = Nothing
   sf.Dispose()
   sf = Nothing
End Sub

???? ???? ?? ??? ???????? ??????? ?????

??? ???????? ???AppendShortcut????? ??? ?????? ??? ???? ??OnMeasureItem???? ??OnDrawItem?????? ??? ?? ????? ?? ???? ???????? ?? ???? ?? ??? ??????property of the menu item. However, this does not consider the fact that the menu item might have a keyboard shortcut. If the menu item does have a keyboard shortcut, you must display the keyboard shortcut and increase the width of the menu item. You must represent the keyboard shortcut as a string. TheAppendShortcutfunction represents the keyboard shortcut as a string and appends the shortcut string to the existing menu item text.

The code for theAppendShortcutfunction may be similar to the following code example.
Private Function AppendShortcut() As String
   Dim s As String
   s = Me.Text
   ' Check to see if we have a shortcut.
   ' If so, append it to our existing text.  
   If Me.ShowShortcut And Me.Shortcut <> Shortcut.None Then
      Dim k As Keys = CType(Shortcut, Keys)
      s = s & Convert.ToChar(9) & _ 
             TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
      End If
   Return s
End Function

Highlight the menu item

?? ???? ??? ????? ???? ???? ????? ???, ?? ??? ?? ????? ?? ?? ??? ??? ???? ???? ?? ??????? ???? ?? ??? ?? ????? ?? ???? ?????? ?????? ???? ???? ?? ??? ??? ??????? ????OnDrawItem?????? ???

??? ?? ??????

????? ??? ?????? ?? ???? ????? ???? ?? ??? ????? ??? ??? ?? ??? ?? ?????? ???? ???? ?? ??????? ???? ?? ??? ??? ????? ???? ???
' Form1.vb
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        MyBase.New()

        'This call is required by the Windows Form Designer.

        InitializeComponent()

        'Add any initialization after the InitializeComponent() call.

    End Sub

 

    'Form overrides dispose to clean up the component list.

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then

            If Not (components Is Nothing) Then

                components.Dispose()

            End If

        End If

        MyBase.Dispose(disposing)

    End Sub

 

    'Required by the Windows Form Designer.

    Private components As System.ComponentModel.IContainer

 

    'Note The following procedure is required by the Windows Form Designer.

    'It can be modified by using the Windows Form Designer.  

    'Do not modify it by using the code editor.

    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        Me.MainMenu1 = New System.Windows.Forms.MainMenu

        Me.MenuItem1 = New System.Windows.Forms.MenuItem

    

        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})

        Me.MenuItem1.Index = 0

        Me.MenuItem1.Text = ""

       

        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

        Me.ClientSize = New System.Drawing.Size(292, 273)

        Me.Menu = Me.MainMenu1

        Me.Name = "Form1"

        Me.Text = "Form1"

 

    End Sub

 

#End Region

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Loading the MyIconMenu onto the first menu in MainMenu1.

        Dim ico As Icon = New Icon("c:\GSA.ico")

        Dim fm As MyIconMenu = New MyIconMenu("Testing", Nothing, Shortcut.None, ico)

        MainMenu1.MenuItems(0).MenuItems.Add(fm)

    End Sub

 

End Class




' MyIconMenu.vb

Imports System

Imports System.ComponentModel

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Drawing.Text

Imports System.Windows.Forms

 

Public Class MyIconMenu

    Inherits MenuItem

 

    Private font As font

    Private icon As icon

 

    'Set properties for border highlighting.

    Private borderWidth As Integer = 1

    Private borderColor As Color = Color.DarkBlue

 

 

    Sub New(ByVal menuText As String, ByVal handler As EventHandler, _

            ByVal shortcut As Shortcut, ByVal ico As icon)

 

        MyBase.New(menuText, handler, shortcut)

        Me.icon = ico

        Me.font = New Font("Arial", 8)

        Me.OwnerDraw = True

 

    End Sub

 

    Public Overloads Sub Dispose()

 

        Me.font.Dispose()

        Me.font = Nothing

        Me.icon.Dispose()

        Me.icon = Nothing

        MyBase.Dispose()

 

    End Sub

 

    Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)

 

        MyBase.OnMeasureItem(e)

        Dim sf As StringFormat = New StringFormat

 

        sf.HotkeyPrefix = HotkeyPrefix.Show

        sf.SetTabStops(50, New Single() {0})

 

        e.ItemHeight = Me.icon.Height + 6

        e.ItemWidth = CInt(e.Graphics.MeasureString(AppendShortcut(), _

                           Me.font, 1000, sf).Width) + Me.icon.Width + 5

 

        sf.Dispose()

        sf = Nothing

 

    End Sub

 

    Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)

 

        Dim br As Brush

        Dim sf As StringFormat

 

        MyBase.OnDrawItem(e)

 

        If CBool(e.State And DrawItemState.Selected) Then

            'Draw a border to highlight the menu.

            e.Graphics.FillRectangle(SystemBrushes.HighlightText, _

                                     e.Bounds)

 

            ControlPaint.DrawBorder(e.Graphics, e.Bounds, _

                                    Me.borderColor, Me.borderWidth, _

                                    ButtonBorderStyle.Solid, _

                                    Me.borderColor, Me.borderWidth, _

                                    ButtonBorderStyle.Solid, _

                                    Me.borderColor, Me.borderWidth, _

                                    ButtonBorderStyle.Solid, _

                                    Me.borderColor, Me.borderWidth, _

                                    ButtonBorderStyle.Solid)

 

        Else

            e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds)

        End If

 

        If Not (Me.icon Is Nothing) Then

            e.Graphics.DrawIcon(Me.icon, e.Bounds.Left + 3, _

                                e.Bounds.Top + 3)

        End If

 

        sf = New StringFormat

        sf.HotkeyPrefix = HotkeyPrefix.Show

        sf.SetTabStops(50, New Single() {0})

        br = New SolidBrush(SystemColors.WindowText)

        e.Graphics.DrawString(AppendShortcut(), Me.font, br, _

                              e.Bounds.Left + Me.icon.Width + 10, _

                              e.Bounds.Top + 2, sf)

 

        'Clean up resources.

        br.Dispose()

        br = Nothing

        sf.Dispose()

        sf = Nothing

 

    End Sub

 

    Private Function AppendShortcut() As String

 

        Dim s As String

        s = Me.Text

 

        ' Check to see if we have a shortcut.

        ' If so, append it to our existing text.  

        If Me.ShowShortcut And Me.Shortcut <> Shortcut.None Then

            ' Use TypeDescriptor to get a string representation of a

            ' Shortcut class.

             Dim k As Keys = CType(Shortcut, Keys)

 

            s = s & Convert.ToChar(9) & _

            TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)

        End If

        Return s

    End Function

 

End Class
???:?? Visual Basic 2005 ??? ??? ?? ????????? ???? ?????? ??? ???????? ??? ??, Visual Basic ????? ????? ??????? ?? ????????? ?? ??? ?? ?? ?? Windows ??????? ????????? ????? ???? ??? ?????? ?? Form1 ??? ??, ??????? ?? ???????????? ???? ???? ?? ??????? Form1.vb ?? Form1.Designer.vb ??? ???? Form1.vb ????? ??? ??? ?????? Windows ??????? ??????? Form1.Designer.vb ????? ??? ??? ????? ??? Form1 ?? ??????????? ?? ?? ??? ??????? ??? ??????? ???? ?? ??? Windows ??????? ??????? ????? ?????? ?? ????? ???? ??? ?? ??????? designer-????? ???? ??? ??? ?? ???? ??? ?? ??? interspersed ?? ????? ???

Visual Basic 2005 ?? ?? ???? ????????????? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????:
(vs.80) http://msdn2.Microsoft.com/en-us/library/ms379584 .aspx
????? ???? ?? Windows ??????? ??????? ?? ???? ??? ???? ??????? ?? ??? ????? MSDN ??? ???? ?? ????:
HTTP://msdn2.Microsoft.com/en-us/library/ms171843.aspx

???

???? ID: 888168 - ????? ???????: 05 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005swept kbvs2005applies kbhowtomaster kbprogramming kbmacroexample kbexpertiseinter kbhowto kbmt KB888168 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:888168

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com