Table 14.6 itemizes the properties and methods available on each Window
object.
Beyond the basics (such as using the Height
and Width
properties to query or affect a window’s dimensions or setting focus to the window with the SetFocus
method), a few properties deserve special mention:
The Document
property gives you a way to programmatically interact with the document that the window is hosting (if any).
The Project
and ProjectItem
properties serve to bridge the Window
portion of the API with the Project/Solution
portion; in a similar vein to the Document
property, you can use these properties to interact with the project that is related to the window or the project item (such as the Visual Basic code file, text file, or resource file).
If you are dealing with a tool window, the SetTabPicture
method provides a way to set the tab icon that is displayed when the tool window is part of a group of tabbed windows. (For instance, the Toolbox window displays a wrench and hammer picture on its tab when part of a tabbed group.)
Again, specifically for tool windows only, the SetSelectionContainer
can be used to supply one or more objects for display within the Properties window. This capability is useful if you have a custom window where you need to control what is displayed in the Properties window when the window has focus. (All the standard VS windows already do this for you.)
Listing 14.2 contains an excerpt from a C# package; the method QueryWindows
illustrates the use of the Window
object. In this example, each window is queried to determine its type, and then a summary of each window is output in a simple message box.
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;
public class Connect : IDTExtensibility2, IDTCommandTarget
{
public void QueryWindows()
{
Windows windows = _applicationObject.DTE.Windows;
Window window;
int count = windows.Count;
string results =
count.ToString() + " windows open..." + "
";
//Iterate the collection of windows
for (int index = 1; index <= count; index++)
{
window = windows.Item(index);
string title = window.Caption;
//If the window is hosting a document, a valid Document
//object will be returned through Window.Document
if (window.Document != null)
{
//Write this out as a document window
string docName = window.Document.Name;
results =
results + "Window '" + title + "' is a document window" + "/r/n";
}
else
{
//If no document was present, this is a tool window
//(tool windows don't host documents)
results =
results + "Window '" + title + "' is a tool window" + "/r/n";
}
}
//Show the results
MessageBox.Show(results, "Window Documents",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Note
If you want to embed your own custom control inside a tool window, you have to write a package and use the Windows.CreateToolWindow
method. We cover this scenario in Chapter 15.