次の方法で共有


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

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

注意

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

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

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

  2. Connect クラスで、WindowEvents オブジェクトを処理する変数および OutputWindowPane オブジェクトを格納する別の変数を初期化します。

    Public WithEvents winEvents As EnvDTE.WindowEvents
    Private outputWinPane As OutputWindowPane
    

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

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

    Dim events As EnvDTE.Events
    events = _applicationObject.Events
    
  4. オートメーション モデルからイベント オブジェクトを取得します。

    winEvents = CType(events.WindowEvents(Nothing), EnvDTE.WindowEvents)
    

    オブジェクト変数の宣言で WithEvents (Visual Basic) ハンドラーが使用されているため、Visual Studio は自動的にメソッド ハンドラーに接続されます。

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

    Private Sub windowsEvents_WindowClosing(ByVal Window As _
    EnvDTE.Window) Handles winEvents.WindowClosing
        outputWinPane.OutputString("WindowEvents.WindowClosing" & _
        ControlChars.Lf)
        outputWinPane.OutputString(ControlChars.Tab & "Window: " & _
        Window.Caption & ControlChars.Lf)
    End Sub
    

    WindowEvents オブジェクトの場合、WindowActivatedWindowClosingWindowCreated、および WindowMoved にプロシージャを追加する必要があります。 完全なコード例を次に示します。

  6. 最終的に、アドインの終了後もウィンドウに関連するイベントを引き続き監視して、Visual Studio によってシステムの処理速度が下がらないようにするには、イベント処理を無効にします。 Visual Basic でイベント処理を無効にするには、イベント ハンドラーを Nothing に設定します。

    Public Sub OnDisconnection(ByVal disconnectMode As _
    ext_DisconnectMode, ByRef custom As Array) Implements _
    IDTExtensibility2.OnDisconnection
        winEvents = Nothing
    End Sub
    

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

使用例

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

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Public Class Connect
    Implements IDTExtensibility2
    ' Handle window events.
    Public WithEvents winEvents As EnvDTE.WindowEvents
    Private outputWinPane As OutputWindowPane

    Dim _applicationObject As DTE2
    Dim _addInInstance As AddIn

    Public Sub OnConnection(ByVal application As Object, ByVal _
    connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
    custom As Array) Implements IDTExtensibility2.OnConnection
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)

        Dim events As EnvDTE.Events
        events = _applicationObject.Events
        ' Send event messages to the Output window.
        Dim outputWindow As OutputWindow
        outputWindow = CType(_applicationObject.Windows.Item _
        (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow)
        outputWinPane = outputWindow.OutputWindowPanes.Add( _
           "DTE Event Information")

        ' Retrieve the event objects from the automation model.
        ' Visual Basic automatically connects the method handler 
        ' because the object variable declaration uses the 'WithEvents'
        ' handler.
        winEvents = CType(events.WindowEvents(Nothing), _
        EnvDTE.WindowEvents)
    End Sub

    ' Handle all window-related events.
    Private Sub windowsEvents_WindowActivated(ByVal GotFocus As _
    EnvDTE.Window, ByVal LostFocus As EnvDTE.Window) Handles _
    winEvents.WindowActivated
        outputWinPane.OutputString("WindowEvents.WindowActivated" & _
        ControlChars.Lf)
        outputWinPane.OutputString(ControlChars.Tab  _
        & "Window receiving focus: " & GotFocus.Caption  _
        & ControlChars.Lf)
        outputWinPane.OutputString _
        (ControlChars.Tab & "Window that lost focus: " _
        & LostFocus.Caption & ControlChars.Lf)
    End Sub

    Private Sub windowsEvents_WindowClosing(ByVal Window As _
    EnvDTE.Window) Handles winEvents.WindowClosing
        outputWinPane.OutputString("WindowEvents.WindowClosing" & _
        ControlChars.Lf)
        outputWinPane.OutputString(ControlChars.Tab & "Window: " & _
        Window.Caption & ControlChars.Lf)
    End Sub

    Private Sub windowsEvents_WindowCreated(ByVal Window As _
    EnvDTE.Window) Handles winEvents.WindowCreated
        outputWinPane.OutputString("WindowEvents.WindowCreated" & _
        ControlChars.Lf)
        outputWinPane.OutputString(ControlChars.Tab & "Window: " & _
        Window.Caption & ControlChars.Lf)
    End Sub

    Private Sub windowsEvents_WindowMoved(ByVal Window As _
    EnvDTE.Window, ByVal Top As Integer, ByVal Left As Integer, ByVal _
    [Width] As Integer, ByVal Height As Integer) Handles _
    winEvents.WindowMoved
        outputWinPane.OutputString("WindowEvents.WindowMoved" & _
        ControlChars.Lf)
        outputWinPane.OutputString(ControlChars.Tab & "Window: " & _
        Window.Caption & ControlChars.Lf)
        outputWinPane.OutputString(ControlChars.Tab & "Location: (" & _
        Top.ToString() & " , " & Left.ToString() & " , " & _
        Width.ToString() & " , " & Height.ToString() & ")" & _
        ControlChars.Lf)
    End Sub

    Public Sub OnDisconnection(ByVal disconnectMode As _
    ext_DisconnectMode, ByRef custom As Array) Implements _
    IDTExtensibility2.OnDisconnection
        ' Turns off window event handling when the add-in shuts down.
        winEvents = Nothing
    End Sub

    Public Sub OnAddInsUpdate(ByRef custom As Array) Implements _
    IDTExtensibility2.OnAddInsUpdate
    End Sub

    Public Sub OnStartupComplete(ByRef custom As Array) Implements _
    IDTExtensibility2.OnStartupComplete
    End Sub

    Public Sub OnBeginShutdown(ByRef custom As Array) Implements _
    IDTExtensibility2.OnBeginShutdown
    End Sub
End Class

コードのコンパイル

このコードをコンパイルするには、Visual Basic で新しい Visual Studio アドイン プロジェクトを作成し、Connect クラスのコードを例のコードに置き換えます。 アドインの実行方法については、「方法: アドイン マネージャーを使用してアドインを制御する」を参照してください。

参照

処理手順

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

その他の技術情報

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