MenuCommand Classe

Definizione

Rappresenta un comando di un menu di Windows o di una barra degli strumenti.

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
Ereditarietà
MenuCommand
Derivato
Attributi

Esempio

L'esempio di codice seguente crea un MenuCommand oggetto, ne configura le proprietà e lo aggiunge a IMenuCommandService.

Creare un'istanza della classe nel Component1 form e aprire il modulo in un ambiente di progettazione come Visual Studio. Premere F1 per richiamare l'oggetto 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

Commenti

La MenuCommand classe rappresenta informazioni su un menu o un comando della barra degli strumenti di Windows. L'interfaccia IMenuCommandService consente di aggiungere MenuCommand oggetti al menu di Visual Studio.

Questa classe fornisce i membri seguenti:

  • Proprietà del gestore eventi a cui è possibile associare un gestore eventi per il comando.

  • Proprietà CommandID che identifica in modo univoco il comando.

  • Metodo Invoke che esegue il comando .

  • Metodo OnCommandChanged di cui è possibile eseguire l'override per gestire l'evento che si verifica quando viene selezionato un nuovo comando.

  • Flag booleano indica se il comando è Checked, , EnabledSupportedo Visible.

  • Proprietà OleStatus che indica il codice di stato del comando OLE per il comando.

  • Override per il ToString metodo .

Costruttori

MenuCommand(EventHandler, CommandID)

Inizializza una nuova istanza della classe MenuCommand.

Proprietà

Checked

Ottiene o imposta un valore che indica se la voce di menu è selezionata.

CommandID

Ottiene l'oggetto CommandID associato a questo comando di menu.

Enabled

Ottiene un valore che indica se questa voce di menu è disponibile.

OleStatus

Ottiene il codice di stato del comando OLE per questa voce di menu.

Properties

Ottiene le proprietà pubbliche associate all'oggetto MenuCommand.

Supported

Ottiene o imposta un valore che indica se la voce di menu è supportata.

Visible

Ottiene o imposta un valore che indica se la voce di menu è visibile.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
Invoke()

Richiama il comando.

Invoke(Object)

Richiama il comando con il parametro dato.

MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
OnCommandChanged(EventArgs)

Genera l'evento CommandChanged.

ToString()

Restituisce una rappresentazione in forma di stringa di questo comando di menu.

Eventi

CommandChanged

Viene generato quando viene modificato il comando di menu.

Si applica a

Vedi anche