MenuCommand Klasa

Definicja

Reprezentuje menu systemu Windows lub element polecenia paska narzędzi.

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
Dziedziczenie
MenuCommand
Pochodne
Atrybuty

Przykłady

Poniższy przykład kodu tworzy MenuCommand obiekt, konfiguruje jego właściwości i dodaje go do obiektu IMenuCommandService.

Utwórz wystąpienie Component1 klasy w formularzu i otwórz formularz w środowisku projektowym, na przykład Visual Studio. Naciśnij klawisz F1, aby wywołać element 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

Uwagi

Klasa MenuCommand reprezentuje informacje dotyczące menu systemu Windows lub polecenia paska narzędzi. Interfejs IMenuCommandService umożliwia dodawanie MenuCommand obiektów do menu programu Visual Studio.

Ta klasa udostępnia następujące elementy członkowskie:

  • Właściwość obsługi zdarzeń, do której można dołączyć procedurę obsługi zdarzeń dla polecenia.

  • Właściwość CommandID , która jednoznacznie identyfikuje polecenie.

  • Metoda wykonująca Invoke polecenie.

  • OnCommandChanged Metoda, która może zostać zastąpiona w celu obsługi zdarzenia występującego po wybraniu nowego polecenia.

  • Flaga logiczna wskazuje, czy polecenie to Checked, Enabled, lub SupportedVisible.

  • Właściwość wskazująca OleStatus kod stanu polecenia OLE dla polecenia.

  • Przesłonięć metodę ToString .

Konstruktory

MenuCommand(EventHandler, CommandID)

Inicjuje nowe wystąpienie klasy MenuCommand.

Właściwości

Checked

Pobiera lub ustawia wartość wskazującą, czy ten element menu jest zaznaczony.

CommandID

CommandID Pobiera skojarzone z tym poleceniem menu.

Enabled

Pobiera wartość wskazującą, czy ten element menu jest dostępny.

OleStatus

Pobiera kod stanu polecenia OLE dla tego elementu menu.

Properties

Pobiera właściwości publiczne skojarzone z elementem MenuCommand.

Supported

Pobiera lub ustawia wartość wskazującą, czy ten element menu jest obsługiwany.

Visible

Pobiera lub ustawia wartość wskazującą, czy ten element menu jest widoczny.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
Invoke()

Wywołuje polecenie .

Invoke(Object)

Wywołuje polecenie za pomocą danego parametru.

MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
OnCommandChanged(EventArgs)

CommandChanged Zgłasza zdarzenie.

ToString()

Zwraca reprezentację ciągu tego polecenia menu.

Zdarzenia

CommandChanged

Występuje, gdy polecenie menu zmieni się.

Dotyczy

Zobacz też