HOW TO:處理 Automation 事件 (Visual C#)
下列程序示範如何使用 Visual C# 增益集處理與視窗相關的事件。
注意事項 |
---|
根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。 |
若要使用 Visual C# 處理與視窗相關的事件
使用 Visual C# 建立 Visual Studio 增益集專案。
在 OnConnection 方法中,初始化變數以攔截事件。在下面的範例中,這個變數的名稱為 events。
EnvDTE.Events events = _applicationObject.Events;
在 OnConnection 方法中,從 Automation 模型中擷取事件物件 (Event Object)。
winEvents = (EnvDTE.WindowEvents)events.get_WindowEvents(null);
在這個範例中,變數的名稱為 winEvents。Automation 模型中的其他物件則是與其他類型的事件相關。例如,FindEvents 適用於與尋找作業相關的事件,而 TaskListEvents 則適用於與 [工作清單] 相關的事件。如需可使用之事件的完整清單,請參閱回應 Automation 事件。
在 OnConnection 方法中,使用 += 運算子連接至由步驟 3 中所擷取之事件物件公開 (Expose) 的各個委派 (Delegate)。例如,若要連接由 WindowClosing 事件公開的委派,請使用下列程式碼:
winEvents.WindowClosing += new _dispWindowEvents_WindowClosingEventHandler(this.WindowClosing);
為事件物件的每個相關事件加入程序。例如,若要處理在視窗關閉時發生的事件 (WindowClosing),請使用下列程式碼:
public void WindowClosing(EnvDTE.Window closingWindow) { outputWindowPane.OutputString ("WindowEvents::WindowClosing\n"); outputWindowPane.OutputString("\tWindow: " + closingWindow.Caption + "\n"); }
以 WindowEvents 物件為例,您必須要有其所有事件的程序,即:
最後,為了不讓 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 類別的程式碼。
請參閱
工作
HOW TO:處理 Automation 事件 (Visual Basic)