ContextMenu 类

定义

表示快捷菜单。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
继承

示例

下面的代码示例为 @no__t 的 @no__t 0 事件创建事件处理程序。The following code example creates an event handler for the Popup event of the ContextMenu. 事件处理程序中的代码确定两个控件中哪个控件的 PictureBox 名为 pictureBox1,一个名为 @no__t 的 TextBox 是显示快捷菜单的控件。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 以显示其快捷菜单,该控件将适当的 @no__t 1 对象添加到 @no__t。Depending on which control caused the ContextMenu to display its shortcut menu, the control adds the appropriate MenuItem objects to the ContextMenu. 此示例要求具有在窗体中定义的名为 contextMenu1 的 @no__t 类的实例。This example requires that you have an instance of the ContextMenu class, named contextMenu1, defined within the form. 此示例还要求将 @no__t 0 和 @no__t 添加到窗体,并将这些控件的 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

注解

@No__t-0 类表示在用户在窗体的控件或区域上单击鼠标右键时可以显示的快捷菜单。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. 例如,您可以使用分配给 @no__t 0 控件的快捷菜单来提供菜单项,用于更改文本的字体、查找控件中的文本或复制和粘贴文本的剪贴板功能。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. 你还可以在快捷菜单中显示不在 @no__t 1 中的新 MenuItem 对象,以提供不适合于 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. 可见控件和 @no__t 都有一个 @no__t 属性,该属性将 ContextMenu 类绑定到显示快捷菜单的控件。Visible controls and Form have a ContextMenu property that binds the ContextMenu class to the control that displays the shortcut menu. 多个控件可以使用 @no__t 0。More 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. 可以处理 @no__t 0 事件以确定显示快捷菜单的时间。You can handle the Popup event to determine when the shortcut menu is being displayed.

备注

若要重用 @no__t 中显示的 @no__t 0 对象以便在 ContextMenu 中使用,你必须使用 @no__t 类的 @no__t 3 方法创建菜单的副本。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. 你还可以使用 @no__t 2 类的 @no__t 方法将菜单项及其子菜单项合并为单个 @no__t 0 对象。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 类的一个新实例。Initializes a new instance of the ContextMenu class with no menu items specified.

ContextMenu(MenuItem[])

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

字段

FindHandle

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

(继承自 Menu)
FindShortcut

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

(继承自 Menu)

属性

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.

(继承自 Menu)
IsParent

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

(继承自 Menu)
MdiListItem

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

(继承自 Menu)
MenuItems

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

(继承自 Menu)
Name

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

(继承自 Menu)
RightToLeft

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

Site

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

(继承自 Component)
SourceControl

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

Tag

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

(继承自 Menu)

方法

CloneMenu(Menu)

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

(继承自 Menu)
CreateMenuHandle()

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

(继承自 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.

(继承自 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.

(继承自 Menu)
FindMergePosition(Int32)

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

(继承自 Menu)
GetContextMenu()

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

(继承自 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.

(继承自 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.

(继承自 Menu)
OnCollapse(EventArgs)

引发 Collapse 事件。Raises the Collapse event.

OnPopup(EventArgs)

引发 Popup 事件。Raises the Popup event.

ProcessCmdKey(Message, Keys)

处理命令键。Processes a command key.

(继承自 Menu)
ProcessCmdKey(Message, Keys, Control)

处理命令键。Processes a command key.

Show(Control, Point)

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

Show(Control, Point, LeftRightAlignment)

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

ToString()

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

(继承自 Menu)

事件

Collapse

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

Disposed

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

(继承自 Component)
Popup

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

适用于

另请参阅