MenuCommand 类

表示 Windows 菜单项或工具栏命令项。

**命名空间:**System.ComponentModel.Design
**程序集:**System(在 system.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public Class MenuCommand
用法
Dim instance As MenuCommand
[ComVisibleAttribute(true)] 
public class MenuCommand
[ComVisibleAttribute(true)] 
public ref class MenuCommand
/** @attribute ComVisibleAttribute(true) */ 
public class MenuCommand
ComVisibleAttribute(true) 
public class MenuCommand

备注

MenuCommand 类表示有关 Windows 菜单或工具栏命令的信息。IMenuCommandService 接口使您可以向 Visual Studio .NET 菜单添加 MenuCommand 对象。

此类提供下列成员:

  • 可将命令的事件处理程序附加到的事件处理程序属性。

  • 唯一标识命令的 CommandID 属性。

  • 执行命令的 Invoke 方法。

  • 一个 OnCommandChanged 方法,经过重写后可处理在选择新命令时所发生的事件。

  • 布尔型标志状态,它们指示命令是否处于下列状态:CheckedEnabledSupportedVisible

  • 一个 OleStatus 属性,它指示命令的 OLE 命令状态代码。

  • ToString 方法的重写。

提示

应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:SharedStateHostProtectionAttribute 不影响桌面应用程序(这些应用程序通常通过双击图标、键入命令或在浏览器中输入 URL 来启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

示例

下面的代码示例创建一个 MenuCommand 对象,配置其属性,并将其添加到 IMenuCommandService

在窗体上创建一个 Component1 类的实例,然后在设计环境(例如 Visual Studio)中打开该窗体。按 F1 键可调用 MenuCommand

Imports System
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
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();
        }
    }

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    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.");
        }
    }
} 
#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." );
      }
   };
}

.NET Framework 安全性

继承层次结构

System.Object
  System.ComponentModel.Design.MenuCommand
     System.ComponentModel.Design.DesignerVerb

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

MenuCommand 成员
System.ComponentModel.Design 命名空间
StandardCommands
IMenuCommandService 接口
CommandID 类

其他资源

Windows 窗体的设计器命令和 DesignerAction 对象模型