次の方法で共有


IMenuCommandService インターフェイス

グローバル デザイナ動詞とデザイン モードで使用できるメニュー コマンドを管理し、いくつかの種類のコンテキスト メニューを表示するためのメソッドを提供します。

この型のすべてのメンバの一覧については、IMenuCommandService メンバ を参照してください。

<ComVisible(True)>
Public Interface IMenuCommandService
[C#]
[ComVisible(true)]
public interface IMenuCommandService
[C++]
[ComVisible(true)]
public __gc __interface IMenuCommandService
[JScript]
public
   ComVisible(true)
interface IMenuCommandService

解説

このインターフェイスは、以下の処理を行うメソッドを提供します。

  • グローバル デザイナ動詞コマンドの検索、呼び出し、追加、および削除。
  • 標準メニュー コマンドの検索、呼び出し、追加、および削除。
  • 標準メニュー コマンドに関連付けられているイベント ハンドラの変更。
  • メニューの CommandID に関連付けられている標準コマンドのコンテキスト メニューの表示。

デザイナ動詞は、デザイン モードでショートカット メニューに表示されるカスタム定義のコマンドを表します。デザイナ動詞は、指定したテキスト ラベルを提供できます。各デザイナ動詞には、一意の CommandID が自動的に割り当てられます。デザイナは、その Verbs プロパティを通じてデザイナ動詞を提供できますが、これらを使用できるのは、デザイナのコンポーネントが現在選択されているときだけです。グローバル デザイナ動詞は、選択されているコンポーネントに関係なく、デザイン モードのショートカット メニューからアクセスできるデザイナ動詞コマンドです。このインターフェイスを使用すると、デザイン モードで使用できるグローバル デザイナ動詞のセットを管理できます。

AddVerb メソッドを使用するとグローバル デザイナ動詞を追加でき、 RemoveVerb メソッドを使用するとグローバル デザイナ動詞を削除できます。デザイナ動詞の CommandID がわかっていれば、 GlobalInvoke メソッドを使用してその動詞を呼び出せます。このインターフェイスの Verbs プロパティには、ショートカット メニューに表示するデザイナ動詞コマンドの現在のセットが格納されています。このデザイナ動詞コマンドのセットは、すべてのグローバル デザイナ動詞と、現在選択されているコンポーネントのデザイナによって提供されるデザイナ動詞で構成されます。この動詞のセットは、デザイナ動詞コマンドを提供しているデザイナに対応するコンポーネントが選択されたり、選択が解除されたりするたびに更新されます。

メニュー コマンドは、定義済み標準コマンドのセットに限定されます。定義済み標準コマンドのほとんどは、 StandardCommands 列挙体と MenuCommands 列挙体で定義されています。各メニュー コマンドは追加、削除、呼び出しが行えます。また、このインターフェイスのメソッドを使用して、メニューに追加されたメニュー コマンドを検索できます。

AddCommand メソッドを使用すると標準メニュー コマンドを追加でき、 RemoveCommand メソッドを使用すると標準メニュー コマンドを削除できます。 AddCommand メソッドのドキュメントに記述されている手順に従うと、定義済みメニュー コマンドにイベント ハンドラを結び付けることができます。 FindCommand メソッドを使用してメニュー コマンドがメニューに追加されていた場合、 CommandID により、そのメニュー コマンドを検索できます。 GlobalInvoke メソッドを使用して、 CommandID でメニュー コマンドやデザイナ動詞コマンドを呼び出せます。

メモ   既に存在している CommandID でメニュー コマンドを追加しようとすると、 InvalidOperationException がスローされます。メニュー コマンドを追加するときは、 FindCommand メソッドを使用して、そのコマンドがメニュー上に存在していないことを確認するか、例外処理を使用して適宜必要な処理を行ってください。

メモ   メニュー コマンドは、メニューに追加して、その Visible プロパティや Enabled プロパティを false に設定できます。メニューに追加したメニュー コマンドが表示されない場合は、これらのプロパティのいずれかが false に設定されている可能性があります。

ShowContextMenu メソッドを使用して、メニュー コマンドを格納している特定の標準ショートカット メニューを指定した位置に表示できます。このメソッドのドキュメントには、表示する有効なメニューを指定するコマンド ID の一覧表が記載されています。

使用例

[Visual Basic, C#, C++] IMenuCommandService を使用して、 MenuCommand を追加する例を次に示します。

 
' This example illustrates how to add a command of type StandardCommand to a
'   service of type IMenuCommandService.  It defines a class that is a designer
'   to a component, called CDesigner.  Then it creates a MenuCommand object using
'   one of the commands in the StandardCommands class.  Finally, it sets several
'   of the properties in the MenuCommand object and adds the MenuCommand object
'   to the MenuCommandService object.

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 OnReplace), StandardCommands.Group)
      mc.Enabled = True
      mc.Visible = True
      mc.Supported = True
      mcs.AddCommand(mc)
      System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.")
   End Sub 'Initialize

   Private Sub OnReplace(ByVal sender As Object, ByVal e As EventArgs)
      System.Windows.Forms.MessageBox.Show("Replace() has been invoked.")
   End Sub 'OnReplace
End Class 'CDesigner

[C#] 
/* This example illustrates how to add a command of type StandardCommand to a
    service of type IMenuCommandService.  It defines a class that is a designer
    to a component, called CDesigner.  Then it creates a MenuCommand object using
    one of the commands in the StandardCommands class.  Finally, it sets several
    of the properties in the MenuCommand object and adds the MenuCommand object
    to the MenuCommandService object.
*/

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(OnReplace),StandardCommands.Group);
        mc.Enabled = true;
        mc.Visible = true;
        mc.Supported = true;
        mcs.AddCommand(mc);
        System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.");
    }

    private void OnReplace(object sender, EventArgs e) 
    {
        System.Windows.Forms.MessageBox.Show("Replace() has been invoked.");
    }
} // End Class Component1

[C++] 
/* This example illustrates how to add a command of type StandardCommand to a
    service of type IMenuCommandService.  It defines a class that is a designer
    to a component, called CDesigner.  Then it creates a MenuCommand object using
    one of the commands in the StandardCommands class.  Finally, it sets several
    of the properties in the MenuCommand object and adds the MenuCommand object
    to the MenuCommandService object.
*/

public __gc class CDesigner : public ComponentDesigner {
public:

    void Initialize(IComponent* comp)
    {
        ComponentDesigner::Initialize(comp);

        IMenuCommandService* mcs =
            static_cast<IMenuCommandService*>(comp->Site->GetService(__typeof(IMenuCommandService)));
        MenuCommand* mc = new MenuCommand(new EventHandler(this, &CDesigner::OnReplace), StandardCommands::Group);
        mc->Enabled = true;
        mc->Visible = true;
        mc->Supported = true;
        mcs->AddCommand(mc);
        System::Windows::Forms::MessageBox::Show(S"Initialize() has been invoked.");
    }

private:

    void OnReplace(Object* /*sender*/, EventArgs* /*e*/)
    {
        System::Windows::Forms::MessageBox::Show(S"Replace() has been invoked.");
    }
}; // End Class CDesigner

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.ComponentModel.Design

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System (System.dll 内)

参照

IMenuCommandService メンバ | System.ComponentModel.Design 名前空間 | MenuCommand | DesignerVerb | CommandID | MenuCommands | StandardCommands