Objective
Add-in most need a certain user interface, the main way in the previous essay is menu and custom form. For menus, it can be the main menu of VS, or it can be in a particular context menu, such as an editor; for a custom form, it's easy to use, just like adding a form to a Windows application. This article will cover additional content about the user interface, including:
1 access to each window in VS, such as Output window, command window, etc.;
2 Create a custom tool window
about Windows in VS
We encounter various kinds of windows in VS, such as solution Explorer, Toolbox, Output window, or Open text editor, although they look different, but they still have something in common in the AOM model.
1 access to Windows in VS
First of all think of a question, how to get the reference to the above window? The DTE2 interface has a Windows collection property that allows access to a particular window.
The interface implements IEnumerable, so we can iterate over it using the foreach statement:
C # Code-Traverse vs Window Collection
foreach (Window win in _applicationObject.Windows)
{
_applicationObject.ToolWindows.OutputWindow.ActivePane.OutputString (win.Caption + Environment.NewLine);
}
In addition, with the Item method of the Windows collection, we can get a specific window using a digital index or the title of the window, such as:
C # Code-Get a window reference using a digital index
Window win = _applicationObject.Windows.Item(1);
win.SetFocus();
The problem with this approach is that the digital index of the window is indeterminate, and so is the title of the window. For Solution Explorer, when we open a solution, its title is "Solution ' Nenhancer ' (6 projects)" format.
Fortunately, for the tool windows in VS (Solution Explorer, Toolbox, Output window, and so on), they all have a unique index, which is the GUID, which makes it easy to get the desired window:
C # Code-Access window with GUID
Window slnWin = _applicationObject.Windows.Item (EnvDTE.Constants.vsWindowKindSolutionExplorer);
slnWin.SetFocus();
GUIDs are obviously hard to remember, so use the constants defined in EnvDTE.Constants to make it easier to get a window and perform better than using a number or title index (you can try to get server Explorer).
There is now a way to get a reference to a window, but the Windows interface is, after all, a common interface for all windows, and if you want to do something specific to a window, you need to consider other ways.
2) The object property of the window interface
Use the object property to get an object specific to a window, as shown in the following example:
C # Code-Gets the objects hosted by the tool window
Window taskListWin = _applicationObject.Windows.Item (EnvDTE.Constants.vsWindowKindTaskList);
TaskList taskList = taskListWin.Object as TaskList;
MessageBox.Show(string.Format("You have {0} tasks.", taskList.TaskItems.Count));
As for this object attribute, perhaps it is easy to see the following Custom tool window. This accesses the Task List window through the Tasklist interface. Other windows such as command window, Error list, and so on are similar. For these most important tool windows, AOM provides a more convenient API.