HOW TO:處理 Automation 事件 (Visual Basic)
下列程序示範如何使用 Visual Studio 增益集處理與視窗相關的事件。
注意事項 |
---|
根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。 |
若要使用 Visual Basic 處理與視窗相關的事件
使用 Visual Basic 建立 Visual Studio 增益集專案。
在 Connect 類別中,初始化一個處理 WindowEvents 物件的變數以及另一個儲存 OutputWindowPane 物件的變數。
Public WithEvents winEvents As EnvDTE.WindowEvents Private outputWinPane As OutputWindowPane
在這個範例中,變數的名稱為 winEvents。Automation 模型中的其他物件則是與其他類型的事件相關。例如,FindEvents 適用於與尋找作業相關的事件,而 TaskListEvents 則適用於與 [工作清單] 相關的事件。如需可使用之事件的完整清單,請參閱回應 Automation 事件。
在 OnConnection 方法中,初始化變數以攔截事件。在下面的範例中,這個變數的名稱為 events。
Dim events As EnvDTE.Events events = _applicationObject.Events
從 Automation 模型中擷取事件物件 (Event Object)。
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 類別的程式碼。如需如何執行增益集的詳細資訊,請參閱 HOW TO:使用增益集管理員來控制增益集。
請參閱
工作
HOW TO:處理 Automation 事件 (Visual C#)