ContextMenu ContextMenu ContextMenu ContextMenu Class

定义

表示快捷菜单。Represents a shortcut menu. 尽管 ContextMenuStrip 替换了早期版本的 ContextMenu 控件并添加了功能;但是也可选择保留 ContextMenu 以备向后兼容和将来使用。Although ContextMenuStrip replaces and adds functionality to the ContextMenu control of previous versions, ContextMenu is retained for both backward compatibility and future use if you choose.

public ref class ContextMenu : System::Windows::Forms::Menu
public class ContextMenu : System.Windows.Forms.Menu
type ContextMenu = class
    inherit Menu
Public Class ContextMenu
Inherits Menu
继承

示例

下面的代码示例为Popup的事件ContextMenu创建一个事件处理程序。The following code example creates an event handler for the Popup event of the ContextMenu. 事件处理程序中的代码确定了两PictureBox个名pictureBox1为的控件, TextBox一个textBox1名为的控件显示快捷菜单。The code in the event handler determines which of two controls a PictureBox named pictureBox1 and a TextBox named textBox1 is the control displaying the shortcut menu. 根据哪个控件导致ContextMenu显示其快捷菜单,该控件将适当MenuItem的对象添加到ContextMenu中。Depending on which control caused the ContextMenu to display its shortcut menu, the control adds the appropriate MenuItem objects to the ContextMenu. 此示例要求在窗体中定义名为ContextMenu contextMenu1的类的实例。This example requires that you have an instance of the ContextMenu class, named contextMenu1, defined within the form. 此示例还TextBox要求您已将和PictureBox添加到窗体中,并且这些ContextMenu控件的属性设置为。 contextMenu1This example also requires that you have a TextBox and PictureBox added to a form and that the ContextMenu property of these controls is set to contextMenu1.

private:
   void MyPopupEventHandler( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Define the MenuItem objects to display for the TextBox.
      MenuItem^ menuItem1 = gcnew MenuItem( "&Copy" );
      MenuItem^ menuItem2 = gcnew MenuItem( "&Find and Replace" );
      // Define the MenuItem object to display for the PictureBox.
      MenuItem^ menuItem3 = gcnew MenuItem( "C&hange Picture" );
      
      // Clear all previously added MenuItems.
      contextMenu1->MenuItems->Clear();

      if ( contextMenu1->SourceControl == textBox1 )
      {
         
         // Add MenuItems to display for the TextBox.
         contextMenu1->MenuItems->Add( menuItem1 );
         contextMenu1->MenuItems->Add( menuItem2 );
      }
      else if ( contextMenu1->SourceControl == pictureBox1 )
      {
         // Add the MenuItem to display for the PictureBox.
         contextMenu1->MenuItems->Add( menuItem3 );
      }
   }
private void MyPopupEventHandler(System.Object sender, System.EventArgs e)
 {
    // Define the MenuItem objects to display for the TextBox.
    MenuItem menuItem1 = new MenuItem("&Copy");
    MenuItem menuItem2 = new MenuItem("&Find and Replace");
    // Define the MenuItem object to display for the PictureBox.
    MenuItem menuItem3 = new MenuItem("C&hange Picture");

    // Clear all previously added MenuItems.
    contextMenu1.MenuItems.Clear();
 
    if(contextMenu1.SourceControl == textBox1)
    {
       // Add MenuItems to display for the TextBox.
       contextMenu1.MenuItems.Add(menuItem1);
       contextMenu1.MenuItems.Add(menuItem2);
    }
    else if(contextMenu1.SourceControl == pictureBox1)
    {
       // Add the MenuItem to display for the PictureBox.
       contextMenu1.MenuItems.Add(menuItem3);
    }
 }
Private Sub MyPopupEventHandler(sender As System.Object, e As System.EventArgs)
    ' Define the MenuItem objects to display for the TextBox.
    Dim menuItem1 As New MenuItem("&Copy")
    Dim menuItem2 As New MenuItem("&Find and Replace")
    ' Define the MenuItem object to display for the PictureBox.
    Dim menuItem3 As New MenuItem("C&hange Picture")
    
    ' Clear all previously added MenuItems.
    contextMenu1.MenuItems.Clear()
    
    If contextMenu1.SourceControl Is textBox1 Then
        ' Add MenuItems to display for the TextBox.
        contextMenu1.MenuItems.Add(menuItem1)
        contextMenu1.MenuItems.Add(menuItem2)
    ElseIf contextMenu1.SourceControl Is pictureBox1 Then
        ' Add the MenuItem to display for the PictureBox.
        contextMenu1.MenuItems.Add(menuItem3)
    End If
End Sub 'MyPopupEventHandler '

注解

ContextMenu类表示当用户在控件或窗体区域上单击鼠标右键时可以显示的快捷菜单。The ContextMenu class represents shortcut menus that can be displayed when the user clicks the right mouse button over a control or area of the form. 快捷菜单通常用于合并窗体中MainMenu的不同菜单项,该窗体对于给定应用程序的上下文非常有用。Shortcut menus are typically used to combine different menu items from a MainMenu of a form that are useful for the user given the context of the application. 例如,您可以使用分配TextBox给控件的快捷菜单来提供菜单项来更改文本的字体、在控件内查找文本或复制和粘贴文本的剪贴板功能。For example, you can use a shortcut menu assigned to a TextBox control to provide menu items for changing the font of the text, finding text within the control, or Clipboard features for copying and pasting text. 您还可以在快捷MenuItem菜单中显示不在MainMenu中的新对象,以提供不适合显示的MainMenu特定情况命令。You can also display new MenuItem objects in a shortcut menu that are not located within a MainMenu to provide situation specific commands that are not appropriate for the MainMenu to display.

通常,当用户在控件或窗体本身上单击鼠标右键时,会显示快捷菜单。Typically, a shortcut menu is displayed when a user clicks the right mouse button over a control or the form itself. 可见控件和Form具有将ContextMenu ContextMenu类绑定到显示快捷菜单的控件的属性。Visible controls and Form have a ContextMenu property that binds the ContextMenu class to the control that displays the shortcut menu. 多个控件可以使用ContextMenuMore than one control can use a ContextMenu. 您可以使用SourceControl属性来确定最后显示快捷菜单的控件,以便执行特定于控件的任务或修改为控件显示的快捷菜单。You can use the SourceControl property to determine which control last displayed the shortcut menu in order to perform tasks specific to the control or to modify the shortcut menu displayed for the control.

你可能想要了解快捷菜单的显示时间,以便在向用户显示菜单之前设置复选标记、禁用项和执行其他菜单任务。You might want to know when the shortcut menu is being displayed in order to set check marks, disable items, and perform other menu tasks before the menu is displayed to the user. 您可以处理Popup事件以确定显示快捷菜单的时间。You can handle the Popup event to determine when the shortcut menu is being displayed.

备注

MenuItem要重复使用在中显示ContextMenu MainMenu的对象,必须使用MenuItem类的CloneMenu方法创建该菜单的副本。In order to reuse MenuItem objects that are displayed in a MainMenu for use in a ContextMenu, you must create a copy of the menu using the CloneMenu method of the MenuItem class. 你还可以使用MenuItem MenuItem类的方法将MergeMenu菜单项及其子菜单项合并到单个对象中。You can also merge menu items and their submenu items into a single MenuItem object using the MergeMenu method of the MenuItem class.

构造函数

ContextMenu() ContextMenu() ContextMenu() ContextMenu()

初始化没有任何指定菜单项的 ContextMenu 类的一个新实例。Initializes a new instance of the ContextMenu class with no menu items specified.

ContextMenu(MenuItem[]) ContextMenu(MenuItem[]) ContextMenu(MenuItem[]) ContextMenu(MenuItem[])

使用一组指定的 ContextMenu 对象初始化 MenuItem 类的一个新实例。Initializes a new instance of the ContextMenu class with a specified set of MenuItem objects.

属性

CanRaiseEvents CanRaiseEvents CanRaiseEvents CanRaiseEvents

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

(Inherited from Component)
Container Container Container Container

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

(Inherited from Component)
DesignMode DesignMode DesignMode DesignMode

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

(Inherited from Component)
Events Events Events Events

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

(Inherited from Component)
Handle Handle Handle Handle

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

(Inherited from Menu)
IsParent IsParent IsParent IsParent

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

(Inherited from Menu)
MdiListItem MdiListItem MdiListItem MdiListItem

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

(Inherited from Menu)
MenuItems MenuItems MenuItems MenuItems

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

(Inherited from Menu)
Name Name Name Name

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

(Inherited from Menu)
RightToLeft RightToLeft RightToLeft RightToLeft

获取或设置一个值,该值指示由控件显示的文本是否从右向左显示。Gets or sets a value indicating whether text displayed by the control is displayed from right to left.

Site Site Site Site

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

(Inherited from Component)
SourceControl SourceControl SourceControl SourceControl

获取当前显示快捷菜单的控件。Gets the control that is displaying the shortcut menu.

Tag Tag Tag Tag

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

(Inherited from Menu)

方法

CloneMenu(Menu) CloneMenu(Menu) CloneMenu(Menu) CloneMenu(Menu)

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

(Inherited from Menu)
CreateMenuHandle() CreateMenuHandle() CreateMenuHandle() CreateMenuHandle()

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

(Inherited from Menu)
CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

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

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

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

(Inherited from Component)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

(Inherited from Menu)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr)

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

(Inherited from Menu)
FindMergePosition(Int32) FindMergePosition(Int32) FindMergePosition(Int32) FindMergePosition(Int32)

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

(Inherited from Menu)
GetContextMenu() GetContextMenu() GetContextMenu() GetContextMenu()

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

(Inherited from Menu)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetMainMenu() GetMainMenu() GetMainMenu() GetMainMenu()

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

(Inherited from Menu)
GetService(Type) GetService(Type) GetService(Type) GetService(Type)

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

(Inherited from Component)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

(Inherited from MarshalByRefObject)
MergeMenu(Menu) MergeMenu(Menu) MergeMenu(Menu) MergeMenu(Menu)

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

(Inherited from Menu)
OnCollapse(EventArgs) OnCollapse(EventArgs) OnCollapse(EventArgs) OnCollapse(EventArgs)

引发 Collapse 事件。Raises the Collapse event.

OnPopup(EventArgs) OnPopup(EventArgs) OnPopup(EventArgs) OnPopup(EventArgs)

引发 Popup 事件Raises the Popup event

ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys)

处理命令键。Processes a command key.

(Inherited from Menu)
ProcessCmdKey(Message, Keys, Control) ProcessCmdKey(Message, Keys, Control) ProcessCmdKey(Message, Keys, Control) ProcessCmdKey(Message, Keys, Control)

处理命令键。Processes a command key.

Show(Control, Point) Show(Control, Point) Show(Control, Point) Show(Control, Point)

在指定位置显示快捷菜单。Displays the shortcut menu at the specified position.

Show(Control, Point, LeftRightAlignment) Show(Control, Point, LeftRightAlignment) Show(Control, Point, LeftRightAlignment) Show(Control, Point, LeftRightAlignment)

在指定的位置使用指定的对齐方式显示快捷菜单。Displays the shortcut menu at the specified position and with the specified alignment.

ToString() ToString() ToString() ToString()

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

(Inherited from Menu)

事件

Collapse Collapse Collapse Collapse

当快捷菜单折叠时发生。Occurs when the shortcut menu collapses.

Disposed Disposed Disposed Disposed

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

(Inherited from Component)
Popup Popup Popup Popup

在快捷菜单显示之前发生。Occurs before the shortcut menu is displayed.

字段

FindHandle FindHandle FindHandle FindHandle

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

(Inherited from Menu)
FindShortcut FindShortcut FindShortcut FindShortcut

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

(Inherited from Menu)

适用于

另请参阅