下面的过程演示了如何使用 Visual Studio 外接程序处理与窗口相关的事件。
提示
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。 这些过程是在“常规开发设置”处于活动状态时开发的。 若要更改设置,请在“工具”菜单上选择“导入和导出设置”。 有关更多信息,请参见 使用设置。
使用 Visual Basic 处理与窗口相关的事件
- 使用 Visual Basic 创建 Visual Studio 外接程序项目。 
- 在 Connect 类中,初始化一个变量以处理 WindowEvents 对象,初始化另一个变量以存储 OutputWindowPane 对象。 - Public WithEvents winEvents As EnvDTE.WindowEvents Private outputWinPane As OutputWindowPane- 在此示例中,变量被命名为 winEvents。 此自动化模型中的其他对象与其他类型的事件相关。 例如,FindEvents 应用于与查找操作相关的事件,而 TaskListEvents 应用于与**“任务列表”**相关的事件。 有关可用事件的完整列表,请参见响应自动化事件。 
- 在 OnConnection 方法中,初始化一个变量以截获事件。 在以下示例中,此变量被命名为 events。 - Dim events As EnvDTE.Events events = _applicationObject.Events
- 从自动化模型中检索事件对象。 - winEvents = CType(events.WindowEvents(Nothing), EnvDTE.WindowEvents)- 因为该对象变量声明使用 WithEvents (Visual Basic) 处理程序,所以 Visual Studio 会自动连接此方法处理程序。 
- 为与事件对象相关的每个事件添加过程。 例如,若要处理关闭窗口时发生的事件,请使用: - 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 对象,则必须为 WindowActivated、WindowClosing、WindowCreated 和 WindowMoved 提供处理过程。 下面的示例中列出了完整代码。 
- 最后,为了防止在关闭外接程序后 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 类的代码。 有关如何运行外接程序的信息,请参见如何:使用外接程序管理器控制外接程序。