ContextMenu 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
注意
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)。 此示例还要求你已向窗体添加一个 TextBox 并 PictureBox 已添加到窗体,并且 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 |
已过时.
(继承自 Component)
|
| SourceControl |
已过时.
获取显示快捷菜单的控件。 |
| Tag |
已过时.
获取或设置与控件关联的用户定义的数据。 (继承自 Menu) |
方法
活动
| 名称 | 说明 |
|---|---|
| Collapse |
已过时.
快捷菜单折叠时发生。 |
| Disposed |
已过时.
当组件通过对方法的调用 Dispose() 释放时发生。 (继承自 Component) |
| Popup |
已过时.
在显示快捷菜单之前发生。 |