方法 : オートメーション イベントを処理する (Visual C#)

更新 : 2007 年 11 月

次の手順では、Visual C# アドインを使用して、ウィンドウに関連するイベントを処理する方法について説明します。

ms165650.alert_note(ja-jp,VS.90).gifメモ :

使用している設定またはエディションによっては、表示されるダイアログ ボックスやメニュー コマンドがヘルプに記載されている内容と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

Visual C# を使用してウィンドウに関連するイベントを処理するには

  1. Visual C# を使用して、Visual Studio アドイン プロジェクトを作成します。

  2. OnConnection メソッドでは、変数を初期化してイベントを受け取ります。次の例では、この変数がイベントの名前になります。

    EnvDTE.Events events = _applicationObject.Events;
    
  3. OnConnection メソッドでは、オートメーション モデルからイベント オブジェクトを取得します。

    winEvents = (EnvDTE.WindowEvents)events.get_WindowEvents(null);
    

    この例では、変数は winEvents と呼ばれます。オートメーション モデル内の他のオブジェクトは、他の種類のイベントに関連しています。たとえば、FindEvents は、検索操作に関連するイベントに適用されます。また、TaskListEvents は、タスク一覧に関連するイベントに適用されます。使用できるイベントの一覧については、「オートメーション イベントへの応答」を参照してください。

  4. OnConnection メソッドでは、+= 演算子を使用して、手順 3. で取得したイベント オブジェクトから公開される各デリゲートに接続します。たとえば、WindowClosing イベントによって公開されたデリゲートに接続するには、次のプロシージャを使用します。

    winEvents.WindowClosing += new  
    _dispWindowEvents_WindowClosingEventHandler(this.WindowClosing);
    
  5. イベント オブジェクトに関連する各イベントにプロシージャを追加します。たとえば、ウィンドウを閉じたときに発生するイベント (WindowClosing) を処理するには、次のプロシージャを使用します。

    public void WindowClosing(EnvDTE.Window closingWindow)
    {
        outputWindowPane.OutputString ("WindowEvents::WindowClosing\n");
        outputWindowPane.OutputString("\tWindow: " + 
        closingWindow.Caption + "\n");
    }
    

    WindowEvents オブジェクトの場合、次に示すように、そのすべてのイベントにプロシージャが必要です。

  6. 最終的に、アドインの終了後もウィンドウに関連するイベントを引き続き監視して、Visual Studio によってシステムの処理速度が下がらないようにするには、イベント処理を無効にします。Visual C# では、-= 演算子を使用してイベント処理を無効にします。たとえば、WindowClosing のイベント処理を無効にするには、次のプロシージャを使用します。

    public void OnDisconnection(ext_DisconnectMode disconnectMode, 
    ref Array custom)
    {
        if (winEvents != null)
        {
            winEvents.WindowClosing -= new 
            _dispWindowEvents_WindowClosingEventHandler     
            (this.WindowClosing);
        }
    }
    

    これにより、アドインがシャットダウンされるか、アドインを実行したまま IDE がシャットダウンされるかに関係なく、イベント処理が無効になります。IDE がシャットダウンされると、最初に、実行中のすべてのアドインが自動的にシャットダウンされます。

使用例

基本的な Visual C# アドインの例を次に示します。この例では、Visual Studio でウィンドウに関連するイベントを受け取って処理する方法について説明します。ウィンドウに関連するイベントが発生した場合は、必ず通知メッセージが [出力] ウィンドウに送信されます。

namespace CSEventsAddin
{
    using System;
    using Microsoft.VisualStudio.CommandBars;
    using Extensibility;
    using EnvDTE;
    using EnvDTE80;

    public class Connect : Object, IDTExtensibility2
    {
        public Connect()
        {
        }

        public void OnConnection(object application, ext_ConnectMode 
        connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;

            // Retrieve the event objects from the automation model.
            EnvDTE.Events events = _applicationObject.Events;
            // Send event messages to the Output window.
            OutputWindow outputWindow = 
            (OutputWindow)_applicationObject.Windows.Item
            (Constants.vsWindowKindOutput).Object;
            outputWindowPane = outputWindow.OutputWindowPanes.Add("DTE 
Event Information");
            // Retrieve the event objects from the automation model.
            winEvents = 
            (EnvDTE.WindowEvents)events.get_WindowEvents(null);

            // Connect to each delegate exposed from each object 
            // retrieved above.
            winEvents.WindowActivated += new 
            _dispWindowEvents_WindowActivatedEventHandler
            (this.WindowActivated);
            winEvents.WindowClosing += new  
            _dispWindowEvents_WindowClosingEventHandler
            (this.WindowClosing);
            winEvents.WindowCreated += new  
            _dispWindowEvents_WindowCreatedEventHandler
            (this.WindowCreated);
            winEvents.WindowMoved += new 
            _dispWindowEvents_WindowMovedEventHandler
            (this.WindowMoved);
        }

        public void OnDisconnection(ext_DisconnectMode disconnectMode, 
        ref Array custom)
        {
            // If the delegate handlers have been connected, then 
            // disconnect them here. 
            // If this is not done, the handlers may still 
            // fire until the next garbage collection event.
            if (winEvents != null)
            {
                winEvents.WindowActivated -= new 
                _dispWindowEvents_WindowActivatedEventHandler
                (this.WindowActivated);
                winEvents.WindowClosing -= new 
                _dispWindowEvents_WindowClosingEventHandler
                (this.WindowClosing);
                winEvents.WindowCreated -= new 
                _dispWindowEvents_WindowCreatedEventHandler
                (this.WindowCreated);
                winEvents.WindowMoved -= new 
                _dispWindowEvents_WindowMovedEventHandler
                (this.WindowMoved);
            }
        }

        // Window-related events.
        public void WindowClosing(EnvDTE.Window closingWindow)
        {
            outputWindowPane.OutputString
("WindowEvents::WindowClosing\n");
            outputWindowPane.OutputString("\tWindow: " + 
closingWindow.Caption + "\n");
        }

        public void WindowActivated(EnvDTE.Window gotFocus,   
        EnvDTE.Window lostFocus)
        {
            outputWindowPane.OutputString
("WindowEvents::WindowActivated\n");
            outputWindowPane.OutputString("\tWindow receiving focus: " 
+ gotFocus.Caption + "\n");
            outputWindowPane.OutputString("\tWindow that lost focus: " 
+ lostFocus.Caption + "\n");
        }

        public void WindowCreated(EnvDTE.Window window)
        {
            outputWindowPane.OutputString
            ("WindowEvents::WindowCreated\n");
            outputWindowPane.OutputString("\tWindow: " + window.Caption 
+ "\n");
        }

        public void WindowMoved(EnvDTE.Window window, int top, int 
        left, int width, int height)
        {
            outputWindowPane.OutputString
            ("WindowEvents::WindowMoved\n");
            outputWindowPane.OutputString("\tWindow: " + window.Caption 
+ "\n");
            outputWindowPane.OutputString("\tLocation: (" + 
top.ToString() + " , " + left.ToString() + " , " + 
width.ToString() + " , " + height.ToString() + ")\n");
        }

        public void OnAddInsUpdate(ref Array custom)
        {
        }

        public void OnStartupComplete(ref Array custom)
        {
        }

        public void OnBeginShutdown(ref Array custom)
        {
        }

        private DTE2 _applicationObject;
        private AddIn _addInInstance;
        private EnvDTE.WindowEvents winEvents;
        private OutputWindowPane outputWindowPane;
    }
}

コードのコンパイル方法

このコードをコンパイルするには、新しい Visual C# アドイン プロジェクトを作成し、Connect クラスのコードを例のコードで置き換えます。

参照

処理手順

方法 : オートメーション イベントを処理する (Visual Basic)

参照

+= 演算子 (C# リファレンス)

その他の技術情報

オートメーション イベントへの応答

イベントへの応答 (Visual Basic および Visual C# プロジェクト)