MenuCommand Classe

Définition

Représente une commande de menu Windows ou de barre d'outils.

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
Héritage
MenuCommand
Dérivé
Attributs

Exemples

L’exemple de code suivant crée un MenuCommand objet, configure ses propriétés et l’ajoute à .IMenuCommandService

Créez un instance de la Component1 classe sur votre formulaire et ouvrez le formulaire dans un environnement de conception comme Visual Studio. Appuyez sur la touche F1 pour appeler .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

Remarques

La MenuCommand classe représente des informations sur un menu ou une commande de barre d’outils Windows. L’interface IMenuCommandService vous permet d’ajouter MenuCommand des objets au menu Visual Studio.

Cette classe fournit les membres suivants :

  • Propriété de gestionnaire d’événements à laquelle vous pouvez attacher un gestionnaire d’événements pour la commande .

  • Propriété CommandID qui identifie de manière unique la commande.

  • Méthode Invoke qui exécute la commande .

  • Méthode OnCommandChanged qui peut être remplacée pour gérer l’événement qui se produit lorsqu’une nouvelle commande est sélectionnée.

  • États d’indicateur booléen qui indiquent si la commande est Checked, Enabled, Supportedou Visible.

  • Propriété OleStatus qui indique la commande OLE status code pour la commande .

  • Remplacement de la ToString méthode .

Constructeurs

MenuCommand(EventHandler, CommandID)

Initialise une nouvelle instance de la classe MenuCommand.

Propriétés

Checked

Obtient ou définit une valeur indiquant si cet élément de menu est coché.

CommandID

Obtient le CommandID associé à cette commande de menu.

Enabled

Obtient une valeur indiquant si cet élément de menu est disponible.

OleStatus

Obtient le code d'état de la commande OLE pour cet élément de menu.

Properties

Obtient les propriétés publiques associées au MenuCommand.

Supported

Obtient ou définit une valeur indiquant si cet élément de menu est pris en charge.

Visible

Obtient ou définit une valeur indiquant si cet élément de menu est visible.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
Invoke()

Appelle la commande.

Invoke(Object)

Appelle la commande avec le paramètre donné.

MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
OnCommandChanged(EventArgs)

Déclenche l’événement CommandChanged.

ToString()

Retourne une représentation de type chaîne de cette commande de menu.

Événements

CommandChanged

Se produit lorsque la commande de menu est modifiée.

S’applique à

Voir aussi