Compartilhar via


How to: Handle Automation Events (Visual Basic)

The procedure below demonstrates how to handle window-related events by using a Visual Studio add-in.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

  1. Create a Visual Studio Add-in project by using Visual Basic.

  2. In the Connect class, initialize a variable to handle the WindowEvents object and another variable to store an OutputWindowPane object.

    Public WithEvents winEvents As EnvDTE.WindowEvents
    Private outputWinPane As OutputWindowPane
    

    In this example, the variable is named winEvents. Other objects in the automation model relate to other types of events. For example, FindEvents applies to events related to find operations, and TaskListEvents applies to events related to the Task List. For a complete list of available events, see Responding to Automation Events.

  3. In the OnConnection method, initialize a variable to intercept events. In the example below, this variable is named events.

    Dim events As EnvDTE.Events
    events = _applicationObject.Events
    
  4. Retrieve the event objects from the automation model.

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

    Visual Studio automatically connects the method handler because the object variable declaration uses the WithEvents (Visual Basic) handler.

  5. Add procedures for each event related to the event object. For example, to handle the event that occurs when a window is closed, you would use:

    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
    

    In the case of the WindowEvents object, you must have procedures for WindowActivated, WindowClosing, WindowCreated, and WindowMoved. The complete code is listed in the example below.

  6. Finally, to prevent Visual Studio from slowing your system by continuing to monitor window-related events after you close the add-in, disable event handling. In Visual Basic, this is done by setting the event handler to Nothing.

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

    This turns off event handling whether the add-in is shut down, or the IDE is shut down while the add-in is still running. When the IDE is shut down, all running add-ins are automatically shut down first.

Example

The following example is a basic Visual Studio add-in that demonstrates how to intercept and handle window-related events in Visual Studio. Whenever window-related events occur, a notification message is sent to the Output window.

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

Compiling the Code

To compile this code, create a new Visual Studio add-in project in Visual Basic and replace the code of the Connect class with the code in the example. For information about how to run the add-in, see How to: Control Add-Ins By Using the Add-In Manager.

See Also

Tasks

How to: Handle Automation Events (Visual C#)

Other Resources

Responding to Automation Events