public static void CreateToolWindow(IVsUIShell vsUiShell, ToolWindowPane toolWindow, int instanceId) { var clsId = toolWindow.ToolClsid; var typeId = toolWindow.GetType().GUID; var guidAutoActivate = Guid.Empty; var caption = toolWindow.Caption; IVsWindowFrame frame; toolWindow.Package = RPackage.Current; ErrorHandler.ThrowOnFailure( vsUiShell.CreateToolWindow( (uint)(__VSCREATETOOLWIN.CTW_fInitNew | __VSCREATETOOLWIN.CTW_fToolbarHost | __VSCREATETOOLWIN.CTW_fForceCreate), (uint)instanceId, toolWindow, ref clsId, ref typeId, ref guidAutoActivate, null, caption, null, out frame ) ); toolWindow.Frame = frame; SetToolbarToHost(frame, toolWindow); }
private IVsWindowFrame CreateToolWindow(string caption, string guid, System.Windows.Forms.UserControl userControl) { const int TOOL_WINDOW_INSTANCE_ID = 0; // Single-instance toolwindow IVsUIShell uiShell = (IVsUIShell)ServiceProvider.GetService(typeof(SVsUIShell)); Guid toolWindowPersistenceGuid = new Guid(guid); Guid guidNull = Guid.Empty; int[] position = new int[1]; IVsWindowFrame windowFrame = null; int result = uiShell.CreateToolWindow((uint)__VSCREATETOOLWIN.CTW_fInitNew, TOOL_WINDOW_INSTANCE_ID, userControl, ref guidNull, ref toolWindowPersistenceGuid, ref guidNull, null, caption, position, out windowFrame); Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(result); return(windowFrame); }
/// <include file='doc\Package.uex' path='docs/doc[@for="Package.CreateToolWindow"]/*' /> /// <devdoc> /// Create a tool window of the specified type with the specified ID. /// </devdoc> /// <param name="toolWindowType">Type of the window to be created</param> /// <param name="id">Instance ID</param> /// <returns>An instance of a class derived from ToolWindowPane</returns> /// <param name="transient">Should creation of this window be forced ?</param> /// <param name="multiInstances">Is this window multiinstanced ?</param> protected ToolWindowPane CreateToolWindow(Type toolWindowType, int id, bool transient, bool multiInstances) { if (toolWindowType == null) { throw new ArgumentNullException("toolWindowType"); } if (id < 0) { throw new ArgumentOutOfRangeException("id"); } if (!toolWindowType.IsSubclassOf(typeof(ToolWindowPane))) { throw new ArgumentException("toolWindowType"); } /////////////////////////////////////////////////////////////////////////////////////// // // this is the only method that should be calling IVsUiShell.CreateToolWindow() // First create an instance of the ToolWindowPane ToolWindowPane window = (ToolWindowPane)Activator.CreateInstance(toolWindowType); // Check if this window has a ToolBar bool hasToolBar = (window.ToolBar != null); uint flags = (uint)__VSCREATETOOLWIN.CTW_fInitNew; if (!transient) { flags |= (uint)__VSCREATETOOLWIN.CTW_fForceCreate; } if (hasToolBar) { flags |= (uint)__VSCREATETOOLWIN.CTW_fToolbarHost; } if (multiInstances) { flags |= (uint)__VSCREATETOOLWIN.CTW_fMultiInstance; } Guid emptyGuid = Guid.Empty; Guid toolClsid = window.ToolClsid; IVsWindowPane windowPane = null; if (toolClsid.CompareTo(Guid.Empty) == 0) { // If a tool CLSID is not specified, then host the IVsWindowPane windowPane = window.GetIVsWindowPane() as IVsWindowPane; } Guid persistenceGuid = toolWindowType.GUID; IVsWindowFrame windowFrame; // Use IVsUIShell to create frame. IVsUIShell vsUiShell = (IVsUIShell)serviceProvider.GetService(typeof(SVsUIShell)); if (vsUiShell == null) { throw new Exception(string.Format("Missing service: {0}", typeof(SVsUIShell).FullName)); } int hr = vsUiShell.CreateToolWindow(flags, // flags (uint)id, // instance ID windowPane, // IVsWindowPane to host in the toolwindow (null if toolClsid is specified) ref toolClsid, // toolClsid to host in the toolwindow (Guid.Empty if windowPane is not null) ref persistenceGuid, // persistence Guid ref emptyGuid, // auto activate Guid null, // service provider window.Caption, // Window title null, out windowFrame); ErrorHandler.ThrowOnFailure(hr); window.Package = null; // this; // If the toolwindow is a component, site it. IComponent component = null; if (window.Window is IComponent) { component = (IComponent)window.Window; } else if (windowPane is IComponent) { component = (IComponent)windowPane; } if (component != null) { if (componentToolWindows == null) { componentToolWindows = new PackageContainer(window); } componentToolWindows.Add(component); } // This generates the OnToolWindowCreated event on the ToolWindowPane window.Frame = windowFrame; if (hasToolBar && windowFrame != null) { // Set the toolbar IVsToolWindowToolbarHost toolBarHost; object obj; ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_ToolbarHost, out obj)); toolBarHost = (IVsToolWindowToolbarHost)obj; if (toolBarHost != null) { Guid toolBarCommandSet = window.ToolBar.Guid; ErrorHandler.ThrowOnFailure( toolBarHost.AddToolbar((VSTWT_LOCATION)window.ToolBarLocation, ref toolBarCommandSet, (uint)window.ToolBar.ID)); } } window.OnToolBarAdded(); // keep track of created window: if (toolWindows == null) { toolWindows = new Hashtable(); } toolWindows.Add(GetHash(toolWindowType.GUID, id), window); return(window); }