none
Hosting a WinForm User Control in a Tool Window for VS2010 RRS feed

  • Question

  • I'm migrating the VS add-in to the VSPackage in the VS2010.

    How can I host the WinForm user control in the my Tool Window?

    Monday, July 5, 2010 11:19 AM

Answers

  • First, here is the code I use to make a tool window. I think it came from someones blog, or a sample but its been a while so I dont remember.

     

    class MyToolWindow : ToolWindowPane
    {
    	// Control that will be hosted in the tool window
    	private FrmMain control = null;
    	// Caching our output window pane
    	private IVsOutputWindowPane outputWindowPane = null;
    
    	/// <summary>
    	/// Constructor for ToolWindowPane.
    	/// Initialization that depends on the package or that requires access
    	/// to VS services should be done in OnToolWindowCreated.
    	/// </summary>
    	public MyToolWindow()
    		: base(null)
    	{
    		control = new FrmMain();
    	}
    
    	/// <summary>
    	/// This property returns the handle to the user control that should
    	/// be hosted in the Tool Window.
    	/// </summary>
    	public override IWin32Window Window
    	{
    		get { return (IWin32Window)control; }
    	}
    
    	/// <summary>
    	/// This is called after our control has been created and sited.
    	/// This is a good place to initialize the control with data gathered
    	/// from Visual Studio services.
    	/// </summary>
    	public override void OnToolWindowCreated()
    	{
    		base.OnToolWindowCreated();
    
    		this.Caption = "My super cool tool window";
    	}
    
    	/// <summary>
    	/// Retrieve the pane that should be used to output information.
    	/// </summary>
    	private IVsOutputWindowPane OutputWindowPane
    	{
    		get
    		{
    			if (outputWindowPane == null)
    			{
    				// First make sure the output window is visible
    				IVsUIShell uiShell = (IVsUIShell)this.GetService(typeof(SVsUIShell));
    				// Get the frame of the output window
    				Guid outputWindowGuid = new Guid("{AAB0CA48-F44D-41B4-94F1-B332AA4C9ED2}");
    				IVsWindowFrame outputWindowFrame = null;
    				ErrorHandler.ThrowOnFailure(uiShell.FindToolWindow((uint)__VSCREATETOOLWIN.CTW_fForceCreate, ref outputWindowGuid, out outputWindowFrame));
    				// Show the output window
    				if (outputWindowFrame != null)
    					outputWindowFrame.Show();
    
    				// Get the output window service
    				IVsOutputWindow outputWindow = (IVsOutputWindow)this.GetService(typeof(SVsOutputWindow));
    				// The following GUID is a randomly generated one. This is to uniquely identify our output pane.
    				// It is best to change it to something else to avoid sharing it with someone else.
    				// If the goal is to share, then the same guid should be used, and the pane should only
    				// be created if it does not already exist.
    				Guid paneGuid = new Guid("{AAB0CA48-F44D-41B4-94F1-B332AA4C9ED3}");
    				// Create the pane
    				string paneName = "MyPaneName";
    				ErrorHandler.ThrowOnFailure(outputWindow.CreatePane(ref paneGuid, paneName, 1 /*visible=true*/, 0 /*clearWithSolution=false*/));
    				// Retrieve the pane
    				ErrorHandler.ThrowOnFailure(outputWindow.GetPane(ref paneGuid, out outputWindowPane));
    			}
    
    			return outputWindowPane;
    		}
    	}
    
    }

     

    and here is the activation part, FindToolWindow is a method that comes from your Package, in my case, my activation handler is in my Package implementation so its pretty simple.

     

    ToolWindowPane pane = FindToolWindow(typeof(CodeConverter), 0, true);
    ((IVsWindowFrame)pane.Frame).Show();

    Tuesday, July 6, 2010 5:40 AM