如何:更改窗口特性
Visual Studio 中的窗口由 Window2 对象表示在自动化模型中。通过使用其成员可以操作窗口的特性(例如它的宽度、高度、可见性等)。通过使用 Window2 集合,可以创建一个链接窗口,该窗口由两个或多个停靠在一起的工具窗口组成。它们的成员还允许您将附加窗口停靠到框架,或从框架取消停靠附加窗口。
说明 |
---|
要链接的窗口必须是可见的。如果有任何一个窗口是隐藏的,都会引发异常。可以通过使用 Visible 属性来显示窗口。 |
Windows2 集合还使您可以创建自己的工具窗口。有关更多信息,请参见如何:创建和控制工具窗口。
说明 |
---|
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置。 |
示例
下面的示例演示如何引用和使用自动化模型的各个成员操作工具窗口。这些示例创建一个链接工具窗口,然后插入两个 Visual Studio 工具窗口,分别是**“解决方案资源管理器”和“输出”**窗口,并将其链接在一起。这些示例还显示如何调整工具窗口的大小和取消停靠工具窗口。在运行此代码以前,请确保 EnvDTE 程序集的“嵌入互操作类型”属性引用 False。有关如何在外接程序中运行示例代码的更多信息,请参见如何:编译和运行自动化对象模型代码示例。
警告 |
---|
运行该示例将更改当前 Visual Studio 工具窗口布局。 |
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)
chgWindow(_applicationObject)
End Sub
Public Sub chgWindow(ByVal dte As DTE2)
' Create variables for the various tool windows.
Dim winFrame As EnvDTE80.Window2
Dim win1 As Window = _
dte.Windows.Item(Constants.vsWindowKindSolutionExplorer)
Dim win2 As Window = dte.Windows. _
Item(Constants.vsWindowKindOutput)
Dim win3 As Window = dte.Windows. _
Item(Constants.vsWindowKindCommandWindow)
' Create a linked window frame and dock Solution
' Explorer and the Ouput window together inside it.
winFrame = CType(dte.Windows.CreateLinkedWindowFrame(win1, win2, _
vsLinkedWindowType.vsLinkedWindowTypeDocked), Window2)
MsgBox("Total number of windows in the linked window frame: " & _
winFrame.LinkedWindows.Count)
' Add another tool window, the Command window, to the frame
' with the other two.
winFrame.LinkedWindows.Add(win3)
MsgBox("Total number of windows in the linked window frame: " & _
winFrame.LinkedWindows.Count)
' Resize the entire linked window frame.
winFrame.Width = 500
winFrame.Height = 600
MsgBox("Frame height and width changed. Now changing Command _
window height.")
' Resize the height of the Command window.
winFrame.LinkedWindows.Item(3).Height = 800
MsgBox("Now undocking the Command window from the frame.")
' Undock the Command window from the frame.
winFrame.LinkedWindows.Remove(win3)
End Sub
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
chgWindow(_applicationObject);
}
public void chgWindow(DTE2 dte)
{
// Create variables for the various tool windows.
EnvDTE80.Window2 winFrame;
Window win1 =
dte.Windows.Item(Constants.vsWindowKindSolutionExplorer);
Window win2 = dte.Windows.Item(Constants.vsWindowKindOutput);
Window win3 =
dte.Windows.Item(Constants.vsWindowKindCommandWindow);
// Create a linked window frame and dock Solution
// Explorer and the Ouput window together inside it.
winFrame = (Window2)dte.Windows.CreateLinkedWindowFrame(win1, win2,
vsLinkedWindowType.vsLinkedWindowTypeDocked);
System.Windows.Forms.MessageBox.Show("Total number of windows in
the linked window frame: " + winFrame.LinkedWindows.Count);
// Add another tool window, the Command window, to the frame
// with the other two.
winFrame.LinkedWindows.Add(win3);
System.Windows.Forms.MessageBox.Show(
"Total number of windows in the linked window frame: " +
winFrame.LinkedWindows.Count);
// Resize the entire linked window frame.
winFrame.Width = 500;
winFrame.Height = 600;
System.Windows.Forms.MessageBox.Show(
"Frame height and width changed." +
"Now changing Command window height.");
// Resize the height of the Command window.
winFrame.LinkedWindows.Item(3).Height = 800;
System.Windows.Forms.MessageBox.Show(
"Now undocking the Command window from the frame.");
// Undock the Command window from the frame.
winFrame.LinkedWindows.Remove(win3);
}