通过


ContextMenu 类

定义

注意

ContextMenu is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use ContextMenuStrip instead.

表示快捷菜单。

此类在 .NET Core 3.1 及更高版本中不可用。 请 ContextMenuStrip 改用它替换和扩展 ContextMenu 控件。

public ref class ContextMenu : System::Windows::Forms::Menu
public class ContextMenu : System.Windows.Forms.Menu
[System.ComponentModel.Browsable(false)]
[System.Obsolete("`ContextMenu` is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use `ContextMenuStrip` instead.", false, DiagnosticId="WFDEV006", UrlFormat="https://aka.ms/winforms-warnings/{0}")]
public class ContextMenu : System.Windows.Forms.Menu
type ContextMenu = class
    inherit Menu
[<System.ComponentModel.Browsable(false)>]
[<System.Obsolete("`ContextMenu` is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use `ContextMenuStrip` instead.", false, DiagnosticId="WFDEV006", UrlFormat="https://aka.ms/winforms-warnings/{0}")>]
type ContextMenu = class
    inherit Menu
Public Class ContextMenu
Inherits Menu
继承
属性

示例

下面的代码示例为 Popup 该事件的事件处理程序创建一 ContextMenu个事件处理程序。 事件处理程序中的代码确定命名控件和命名textBox1控件PictureBoxpictureBox1中的哪一TextBox个是显示快捷菜单的控件。 根据哪个控件导致ContextMenu显示其快捷菜单,该控件会将相应的MenuItem对象添加到该控件。ContextMenu 此示例要求你具有在窗体中定义的类的实例 ContextMenu (命名 contextMenu1)。 此示例还要求你已向窗体添加一个 TextBoxPictureBox 已添加到窗体,并且 ContextMenu 这些控件的属性设置为 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

注解

此类在 .NET Core 3.1 及更高版本中不可用。 请改用 ContextMenuStrip

ContextMenu 类表示当用户在窗体的控件或区域上单击鼠标右键时可以显示的快捷菜单。 快捷菜单通常用于组合窗体中的不同菜单项,这些项 MainMenu 对于给定应用程序的上下文的用户很有用。 例如,可以使用分配给 TextBox 控件的快捷菜单来提供菜单项来更改文本的字体、查找控件中的文本或用于复制和粘贴文本的剪贴板功能。 还可以在快捷菜单中显示不位于MainMenu某个位置的新MenuItem对象,以提供不适合显示的特定于情况的MainMenu命令。

通常,当用户单击控件或窗体本身上的鼠标右键时,将显示快捷菜单。 可见控件并 Form 具有将 ContextMenu 类绑定到 ContextMenu 显示快捷菜单的控件的属性。 多个控件可以使用 ContextMenu. 可以使用 SourceControl 该属性来确定上次显示快捷菜单的控件,以便执行特定于控件的任务,或修改为控件显示的快捷菜单。

你可能想知道快捷菜单何时显示,以便设置复选标记、禁用项目和执行其他菜单任务,然后再向用户显示菜单。 可以处理事件 Popup 以确定何时显示快捷菜单。

注释

若要重用MenuItem在一个MainMenuContextMenu对象中使用的对象,必须使用类的方法MenuItem创建菜单CloneMenu的副本。 还可以使用MergeMenu类的方法MenuItem将菜单项及其子菜单项合并到单个MenuItem对象中。

构造函数

名称 说明
ContextMenu()
已过时.

初始化没有指定菜单项的 ContextMenu 类的新实例。

ContextMenu(MenuItem[])
已过时.

使用指定的对象集MenuItem初始化类的新实例ContextMenu

字段

名称 说明
FindHandle
已过时.

指定 FindMenuItem(Int32, IntPtr) 该方法应搜索句柄。

(继承自 Menu)
FindShortcut
已过时.

指定 FindMenuItem(Int32, IntPtr) 该方法应搜索快捷方式。

(继承自 Menu)

属性

名称 说明
CanRaiseEvents
已过时.

获取一个值,该值指示组件是否可以引发事件。

(继承自 Component)
Container
已过时.

IContainer获取包含 .Component

(继承自 Component)
DesignMode
已过时.

获取一个值,该值指示当前是否 Component 处于设计模式。

(继承自 Component)
Events
已过时.

获取附加到此 Component对象的事件处理程序的列表。

(继承自 Component)
Handle
已过时.

获取一个值,该值表示菜单的窗口句柄。

(继承自 Menu)
IsParent
已过时.

获取一个值,该值指示此菜单是否包含任何菜单项。 此属性为只读。

(继承自 Menu)
MdiListItem
已过时.

获取一个值,该值指示 MenuItem 用于显示多个文档界面 (MDI) 子窗体的列表。

(继承自 Menu)
MenuItems
已过时.

获取一个值,该值指示与菜单关联的对象的集合 MenuItem

(继承自 Menu)
Name
已过时.

获取或设置 . 的名称 Menu

(继承自 Menu)
RightToLeft
已过时.

获取或设置一个值,该值指示控件显示的文本是否从右到左显示。

Site
已过时.

获取或设置 ISite .Component

(继承自 Component)
SourceControl
已过时.

获取显示快捷菜单的控件。

Tag
已过时.

获取或设置与控件关联的用户定义的数据。

(继承自 Menu)

方法

名称 说明
CloneMenu(Menu)
已过时.

Menu将作为参数传递给当前Menu参数的项。

(继承自 Menu)
CreateMenuHandle()
已过时.

创建一个新的句柄。Menu

(继承自 Menu)
CreateObjRef(Type)
已过时.

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
Dispose()
已过时.

释放该 Component命令使用的所有资源。

(继承自 Component)
Dispose(Boolean)
已过时.

释放资源,而不是内存,由 .Menu

(继承自 Menu)
Equals(Object)
已过时.

确定指定的对象是否等于当前对象。

(继承自 Object)
FindMenuItem(Int32, IntPtr)
已过时.

MenuItem获取包含指定值的值。

(继承自 Menu)
FindMergePosition(Int32)
已过时.

返回菜单项应插入到菜单中的位置。

(继承自 Menu)
GetContextMenu()
已过时.

ContextMenu获取包含此菜单的项。

(继承自 Menu)
GetHashCode()
已过时.

用作默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetMainMenu()
已过时.

MainMenu获取包含此菜单的项。

(继承自 Menu)
GetService(Type)
已过时.

返回一个对象,该对象表示服务由 Component 或其 Container提供的服务。

(继承自 Component)
GetType()
已过时.

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()
已过时.

创建当前 Object的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)
已过时.

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
MergeMenu(Menu)
已过时.

MenuItem 一个菜单的对象与当前菜单合并。

(继承自 Menu)
OnCollapse(EventArgs)
已过时.

引发 Collapse 事件。

OnPopup(EventArgs)
已过时.

引发 Popup 事件。

ProcessCmdKey(Message, Keys, Control)
已过时.

处理命令键。

ProcessCmdKey(Message, Keys)
已过时.

处理命令键。

(继承自 Menu)
Show(Control, Point, LeftRightAlignment)
已过时.

显示位于指定位置且具有指定对齐方式的快捷菜单。

Show(Control, Point)
已过时.

显示位于指定位置的快捷菜单。

ToString()
已过时.

返回一个表示控件的Menu控件String

(继承自 Menu)

活动

名称 说明
Collapse
已过时.

快捷菜单折叠时发生。

Disposed
已过时.

当组件通过对方法的调用 Dispose() 释放时发生。

(继承自 Component)
Popup
已过时.

在显示快捷菜单之前发生。

适用于

另请参阅