ContextMenu ContextMenu ContextMenu ContextMenu Class

定義

代表捷徑功能表。Represents a shortcut menu. 雖然 ContextMenuStrip 會取代並且將功能加入至之前版本的 ContextMenu 控制項;不過,也可以選擇保留 ContextMenu,以提供回溯相容性 (Backward Compatibility) 以及供未來使用。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. 事件處理常式中的程式碼會判斷兩個控制項PictureBoxpictureBox1名為TextBoxtextBox1 ,和名為的是控制項,其中顯示快捷方式功能表。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的物件加入至ContextMenuDepending 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

備註

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. 有一個以上的ContextMenu控制項可以使用。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. 您可以處理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 MergeMenu使用MenuItem類別的方法,將功能表項目及其子功能表專案合併成單一物件。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

取得包含 IContainerComponentGets 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

取得值,表示功能表的視窗控制代碼 (Window 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)

將以參數傳遞的 Menu 複製到目前的 MenuCopies 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)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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)

取得包含指定值的 MenuItemGets 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()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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 的淺層複本 (Shallow Copy)。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)

適用於

另請參閱