Menu 类

定义

表示所有菜单的基本功能。Represents the base functionality for all menus. 虽然 ToolStripDropDownToolStripDropDownMenu 取代了以前版本的 Menu 控件并向其中添加了功能,但是也可选择保留 Menu 以备向后兼容和供将来使用。Although ToolStripDropDown and ToolStripDropDownMenu replace and add functionality to the Menu control of previous versions, Menu is retained for both backward compatibility and future use if you choose.

public ref class Menu abstract : System::ComponentModel::Component
[System.ComponentModel.ListBindable(false)]
public abstract class Menu : System.ComponentModel.Component
type Menu = class
    inherit Component
Public MustInherit Class Menu
Inherits Component
继承
派生
属性

示例

下面的代码示例使用派生类MenuItem创建窗体的菜单结构。The following code example uses the derived class, MenuItem, to create a menu structure for a form. 示例代码添加一个MenuItem来表示顶级菜单项,将一个子菜单项添加到其中,以选择字体大小,然后将两个子菜单项添加到表示应用程序中的大型和小字体选择的菜单项。The example code adds a MenuItem to represent the top-level menu item, adds a submenu item to it for selecting a font size, and then adds two submenu items to that menu item that represent large and small font choices in an application. 该示例MainMenu要求有一个名mainMenu1为的对象和MenuItem menuItem2 menuItem1 menuItem4四个名为、 、和的对象。menuItem3The example requires that there is a MainMenu object named mainMenu1 and four MenuItem objects named menuItem1, menuItem2, menuItem3, and menuItem4.

public:
   void CreateMyMenu()
   {
      // Set the caption for the top-level menu item.
      menuItem1->Text = "Edit";
      // Set the caption for the first submenu.
      menuItem2->Text = "Font Size";
      // Set the caption for menuItem2's first submenu.
      menuItem3->Text = "Small";
      // Set the checked property to true since this is the default value.
      menuItem3->Checked = true;
      // Define a shortcut key combination for the menu item.
      menuItem3->Shortcut = Shortcut::CtrlS;
      // Set the caption of the second sub menu item of menuItem2.
      menuItem4->Text = "Large";
      // Define a shortcut key combination for the menu item.
      menuItem4->Shortcut = Shortcut::CtrlL;
      // Set the index of the menu item so it is placed below the first submenu item.
      menuItem4->Index = 1;
      // Add menuItem3 and menuItem4 to menuItem2's list of menu items.
      menuItem2->MenuItems->Add( menuItem3 );
      menuItem2->MenuItems->Add( menuItem4 );
      // Add menuItem2 to menuItem1's list of menu items.
      menuItem1->MenuItems->Add( menuItem2 );
      // Add menuItem1 to the MainMenu for displaying.
      mainMenu1->MenuItems->Add( menuItem1 );
   }
public void CreateMyMenu()
    {
    // Set the caption for the top-level menu item.
    menuItem1.Text = "Edit";
    // Set the caption for the first submenu.
    menuItem2.Text = "Font Size";
    // Set the caption for menuItem2's first submenu.
    menuItem3.Text = "Small";
    // Set the checked property to true since this is the default value.
    menuItem3.Checked = true;
    // Define a shortcut key combination for the menu item.
    menuItem3.Shortcut = Shortcut.CtrlS;
    // Set the caption of the second sub menu item of menuItem2.
    menuItem4.Text = "Large";
    // Define a shortcut key combination for the menu item.
    menuItem4.Shortcut = Shortcut.CtrlL;
    // Set the index of the menu item so it is placed below the first submenu item.
    menuItem4.Index = 1;
    // Add menuItem3 and menuItem4 to menuItem2's list of menu items.
    menuItem2.MenuItems.Add(menuItem3);
    menuItem2.MenuItems.Add(menuItem4);
    // Add menuItem2 to menuItem1's list of menu items.
    menuItem1.MenuItems.Add(menuItem2);
    // Add menuItem1 to the MainMenu for displaying.
    mainMenu1.MenuItems.Add(menuItem1);
    }

Public Sub CreateMyMenu()
    ' Set the caption for the top-level menu item.
    menuItem1.Text = "Edit"
    ' Set the caption for the first submenu.
    menuItem2.Text = "Font Size"
    ' Set the caption for menuItem2's first submenu.
    menuItem3.Text = "Small"
    ' Set the checked property to true since this is the default value.
    menuItem3.Checked = True
    ' Define a shortcut key combination for the menu item.
    menuItem3.Shortcut = Shortcut.CtrlS
    ' Set the caption of the second sub menu item of menuItem2.
    menuItem4.Text = "Large"
    ' Define a shortcut key combination for the menu item.
    menuItem4.Shortcut = Shortcut.CtrlL
    ' Set the index of the menu item so it is placed below the first submenu item.
    menuItem4.Index = 1
    ' Add menuItem3 and menuItem4 to menuItem2's list of menu items.
    menuItem2.MenuItems.Add(menuItem3)
    menuItem2.MenuItems.Add(menuItem4)
    ' Add menuItem2 to menuItem1's list of menu items.
    menuItem1.MenuItems.Add(menuItem2)
    ' Add menuItem1 to the MainMenu for displaying.
    mainMenu1.MenuItems.Add(menuItem1)
End Sub

注解

此类是MainMenuMenuItemContextMenu类的基类。This class is the base class for the MainMenu, MenuItem, and ContextMenu classes. 不能创建此类的实例。You cannot create an instance of this class. 应用程序的菜单由MenuItem对象组成。The menus for an application consist of MenuItem objects. 这些对象可以包含MenuItem表示子菜单项的其他对象。These can contain other MenuItem objects, representing submenu items. 对象可以存储MainMenu在中,以便显示为窗体的整个菜单结构或ContextMenu用于显示快捷菜单的。 MenuItemThe MenuItem objects can be stored in a MainMenu for display as an entire menu structure for a form or a ContextMenu that is used to display shortcut menus. 此类提供所有菜单类通用的功能。This class provides functionality that is common for all the menu classes.

与许多基类不同, Menu类使用其派生类定义其许多属性。Unlike many base classes, the Menu class uses its derived classes to define many of its properties. 如果在多文档界面(MDI)应用程序中使用菜单,则可以使用MdiListItem属性MenuItem指定,以在应用程序中显示打开的 MDI 子窗体的列表。If you are using your menu in a multiple-document interface (MDI) application, you can use the MdiListItem property to specify a MenuItem that displays a list of open MDI child forms in your application. 属性包含存储在菜单类MenuItem中的对象的列表。 MenuItemsThe MenuItems property contains a list of MenuItem objects stored in the menu class. MainMenu对于或ContextMenu,此属性包含显示的MenuItem所有对象。For a MainMenu or ContextMenu, this property contains all the MenuItem objects that are displayed. 对于,该MenuItems属性表示与之关联的子菜单项。 MenuItemFor a MenuItem, the MenuItems property represents the submenu items associated with it.

除了为所有派生菜单类提供的属性以外, Menu类还提供了一些方法( CloneMenu如和MergeMenu),使您可以从现有菜单创建新菜单,还可以合并两个菜单结构到.In addition to the properties that are provided for all the derived menu classes, the Menu class also provides methods, such as CloneMenu and MergeMenu, that enable you to create new menus from existing menus, and also merge two menu structures together.

该类还定义了嵌套类Menu.MenuItemCollectionMenuThe Menu class also defines the nested class Menu.MenuItemCollection. 此类定义MenuItems属性使用的MenuItem对象集合。This class defines the collection of MenuItem objects used by the MenuItems property. 您可以使用Menu.MenuItemCollection类的方法在MainMenuContextMenuMenuItem中添加和移除菜单项。You can use the methods of the Menu.MenuItemCollection class to add and remove menu items from a MainMenu, ContextMenu, or MenuItem.

构造函数

Menu(MenuItem[])

初始化 Menu 类的新实例。Initializes a new instance of the Menu class.

字段

FindHandle

指定 FindMenuItem(Int32, IntPtr) 方法应搜索一个句柄。Specifies that the FindMenuItem(Int32, IntPtr) method should search for a handle.

FindShortcut

指定 FindMenuItem(Int32, IntPtr) 方法应搜索一个快捷方式。Specifies that the FindMenuItem(Int32, IntPtr) method should search for a shortcut.

属性

CanRaiseEvents

获取一个指示组件是否可以引发事件的值。Gets a value indicating whether the component can raise an event.

(继承自 Component)
Container

获取 IContainer,它包含 ComponentGets the IContainer that contains the Component.

(继承自 Component)
DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(继承自 Component)
Events

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(继承自 Component)
Handle

获取表示菜单的窗口句柄的值。Gets a value representing the window handle for the menu.

IsParent

获取一个值,通过该值指示此菜单是否包含任何菜单项。Gets a value indicating whether this menu contains any menu items. 此属性是只读的。This property is read-only.

MdiListItem

获取一个值,通过该值指示用于显示多文档界面 (MDI) 子窗体列表的 MenuItemGets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms.

MenuItems

获取一个值,通过该值指示与菜单关联的 MenuItem 对象的集合。Gets a value indicating the collection of MenuItem objects associated with the menu.

Name

获取或设置 Menu 的名称。Gets or sets the name of the Menu.

Site

获取或设置 ComponentISiteGets or sets the ISite of the Component.

(继承自 Component)
Tag

获取或设置与控件关联的用户定义的数据。Gets or sets user-defined data associated with the control.

方法

CloneMenu(Menu)

复制作为参数被传递到当前 MenuMenuCopies the Menu that is passed as a parameter to the current Menu.

CreateMenuHandle()

创建 Menu 的新句柄。Creates a new handle to the Menu.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放由 Component 使用的所有资源。Releases all resources used by the Component.

(继承自 Component)
Dispose(Boolean)

处置由 Menu 占用的资源(内存除外)。Disposes of the resources, other than memory, used by the Menu.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FindMenuItem(Int32, IntPtr)

获取 MenuItem,它包含指定的值。Gets the MenuItem that contains the value specified.

FindMergePosition(Int32)

返回菜单项应插入菜单的位置。Returns the position at which a menu item should be inserted into the menu.

GetContextMenu()

获取包含该菜单的 ContextMenuGets the ContextMenu that contains this menu.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetMainMenu()

获取包含该菜单的 MainMenuGets the MainMenu that contains this menu.

GetService(Type)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。Returns an object that represents a service provided by the Component or by its Container.

(继承自 Component)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
MergeMenu(Menu)

将一个菜单的 MenuItem 对象与当前菜单合并。Merges the MenuItem objects of one menu with the current menu.

ProcessCmdKey(Message, Keys)

处理命令键。Processes a command key.

ToString()

返回表示 String 控件的 MenuReturns a String that represents the Menu control.

事件

Disposed

当通过调用 Dispose() 方法释放组件时发生。Occurs when the component is disposed by a call to the Dispose() method.

(继承自 Component)

适用于

另请参阅