MenuCommand 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 Windows 菜单项或工具栏命令项。
public ref class MenuCommandpublic class MenuCommand[System.Runtime.InteropServices.ComVisible(true)]
public class MenuCommandtype MenuCommand = class[<System.Runtime.InteropServices.ComVisible(true)>]
type MenuCommand = classPublic 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 可以重写的方法,用于处理选择新命令时发生的事件。 
- 指示 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 | 在菜单命令出现更改时发生。 |