VSIP 2005: Tracking the active VS window
In a VSPackage using the Managed Package Framework (MPF), how do you track the active editor pane in VS?
To start with, I create a new VSPackage project (New project -> Other project types -> Extensibility -> Visual Studio Integration Package) and have the wizard create a C# based project with a tool window for me. From the standard button click (to keep things under control easily), I run the following initialization code:
Two different things are being done here:
- An output pane is installed into the VS output window, titled Active window tracking, that I can use throughout my package to output debug information. The output pane is stored in a field of type
OutputWindowPane, to be used later. - The last two lines query the
IVsMonitorSelectionservice from the VS shell, and install my class as an event receiver for event pertaining to selections. A selection in VS can be for any type of object that the IDE deals with, so windows are one such type. The cookie that’s returned by the call toAdviseSelectionEventsisn’t important for us, it’s stored away in a field ofuinttype.
Now the class has to implement the interface IVsSelectionEvents to be able to receive notifications about selections. The three methods of the interface are implemented here:
As you can see, the only method of interest to me is the OnElementValueChanged method, in which I evaluate the elementid to make sure that the given element is indeed a WindowFrame and I extract the window caption for demonstration purposes, which is then shown in the output window I installed in the beginning. Here are the missing helper functions:
Have fun!





