MenuCommand 類別

定義

表示 Windows 功能表或工具列命令項目。

public ref class MenuCommand
public class MenuCommand
[System.Runtime.InteropServices.ComVisible(true)]
public class MenuCommand
type MenuCommand = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type MenuCommand = class
Public Class MenuCommand
繼承
MenuCommand
衍生
屬性

範例

下列程式代碼範例會 MenuCommand 建立 物件、設定其屬性,並將其新增至 IMenuCommandService

在您的表單上建立 類別的實例, Component1 並在 Visual Studio 之類的設計環境中開啟表單。 按 F1 鍵以叫 MenuCommand用 。

#using <system.dll>
#using <system.design.dll>
#using <system.windows.forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Security::Permissions;

namespace CppMenuCommand
{
   public ref class CDesigner: public ComponentDesigner
   {
   public:
    [PermissionSetAttribute(SecurityAction::Demand, Name="FullTrust")]
      virtual void Initialize( IComponent^ comp ) override
      {
         ComponentDesigner::Initialize( comp );
         IMenuCommandService^ mcs = static_cast<IMenuCommandService^>(comp->Site->GetService( IMenuCommandService::typeid ));
         MenuCommand^ mc = gcnew MenuCommand( gcnew EventHandler( this, &CDesigner::OnF1Help ),StandardCommands::F1Help );
         mc->Enabled = true;
         mc->Visible = true;
         mc->Supported = true;
         mcs->AddCommand( mc );
         System::Windows::Forms::MessageBox::Show( "Initialize() has been invoked." );
      }

   private:
      void OnF1Help( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         System::Windows::Forms::MessageBox::Show( "F1Help has been invoked." );
      }
   };
}
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace CSMenuCommand
{
    [Designer(typeof(CDesigner))]
    public class Component1 : System.ComponentModel.Component
    {
        private System.ComponentModel.Container components = null;

        public Component1(System.ComponentModel.IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        public Component1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
    }

    public class CDesigner : System.ComponentModel.Design.ComponentDesigner 
    {
        public override void Initialize(IComponent comp) 
        {
            base.Initialize(comp);

            IMenuCommandService mcs = (IMenuCommandService)comp.Site.
                        GetService(typeof(IMenuCommandService));
            MenuCommand mc = new MenuCommand(new EventHandler(OnF1Help), StandardCommands.F1Help);
            mc.Enabled = true;
            mc.Visible = true;
            mc.Supported = true;
            mcs.AddCommand(mc);
            System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.");
        }

        private void OnF1Help(object sender, EventArgs e) 
        {
            System.Windows.Forms.MessageBox.Show("F1Help has been invoked.");
        }
    }
}
Imports System.ComponentModel
Imports System.ComponentModel.Design

Namespace VbMenuCommand
    <Designer(GetType(CDesigner))> _
    Public Class Component1
        Inherits System.ComponentModel.Component
        Private components As System.ComponentModel.Container = Nothing

        Public Sub New(ByVal container As System.ComponentModel.IContainer)
            container.Add(Me)
            InitializeComponent()
        End Sub

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub InitializeComponent()
        End Sub
    End Class

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class CDesigner
        Inherits System.ComponentModel.Design.ComponentDesigner

        Public Overrides Sub Initialize(ByVal comp As IComponent)
            MyBase.Initialize(comp)

            Dim mcs As IMenuCommandService = CType(comp.Site.GetService(GetType(IMenuCommandService)), IMenuCommandService)
            Dim mc As New MenuCommand(New EventHandler(AddressOf OnF1Help), StandardCommands.F1Help)
            mc.Enabled = True
            mc.Visible = True
            mc.Supported = True
            mcs.AddCommand(mc)
            System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.")
        End Sub

        Private Sub OnF1Help(ByVal sender As Object, ByVal e As EventArgs)
            System.Windows.Forms.MessageBox.Show("F1Help has been invoked.")
        End Sub
    End Class
End Namespace

備註

類別 MenuCommand 代表 Windows 功能表或工具列命令的相關信息。 介面 IMenuCommandService 可讓您將物件新增 MenuCommand 至 Visual Studio 功能表。

此類別提供下列成員:

  • 事件處理程式屬性,您可以附加命令的事件處理程式。

  • 可唯一 CommandID 識別命令的屬性。

  • Invoke執行命令的方法。

  • OnCommandChanged方法,可覆寫以處理選取新命令時所發生的事件。

  • 布林值旗標狀態,指出命令為 CheckedEnabledSupportedVisible

  • OleStatus屬性,表示命令的 OLE 命令狀態代碼。

  • 方法的 ToString 覆寫。

建構函式

MenuCommand(EventHandler, CommandID)

初始化 MenuCommand 類別的新執行個體。

屬性

Checked

取得或設定值,表示是否檢查這個功能表項目。

CommandID

取得與這個功能表命令相關的 CommandID

Enabled

取得值,表示是否可以使用這個功能表項目。

OleStatus

取得這個功能表項目的 OLE 命令狀態程式碼。

Properties

取得與 MenuCommand 關聯的公用屬性。

Supported

取得或設定值,表示是否支援這個功能表項目。

Visible

取得或設定值,表示功能表項目是否為可見。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Invoke()

叫用命令。

Invoke(Object)

叫用具有指定參數的命令。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnCommandChanged(EventArgs)

引發 CommandChanged 事件。

ToString()

傳回這個功能表命令的字串表示。

事件

CommandChanged

發生於功能表命令變更時。

適用於

另請參閱