Dynamic Tool Window Sample (in progress)

This sample demonstrates how to create a tool window that sets its controls' values as the visual status of the tool window changes. The status changes are also indicated in a custom Output Window pane.

Package source code:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.DynamicToolWindow
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "DynamicToolWindow", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [ProvideToolWindow(typeof(DynamicWindowPane))]
  [Guid(GuidList.guidDynamicToolWindowPkgString)]
  public sealed class DynamicToolWindowPackage : PackageBase
  {
  }
}
Dynamic Tool Window pane source code:

using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using VSXtra;

namespace DeepDiver.DynamicToolWindow
{
  [Guid("F0E1E9A1-9860-484d-AD5D-367D79AABF55")]
  [InitialCaption("Dynamic Tool Window")]
  class DynamicWindowPane : ToolWindowPane<DynamicToolWindowPackage, DynamicWindowControl>
  {
    private OutputWindowPane _OutputPane;

    public override void OnToolWindowCreated()
    {
      base.OnToolWindowCreated();
      _OutputPane = OutputWindow.GetPane<EventsPane>();
      VsDebug.Assert(_OutputPane != null, "Output pane creation failed.");
      Frame.OnShow += OnFrameShow;
      Frame.OnClose += OnFrameClose;
      Frame.OnResize += OnFrameResize;
      Frame.OnMove += OnFrameMove;
      Frame.OnDockChange += OnFrameDockChange;
      Frame.OnStatusChange += UIControl.RefreshValues;
    }

    void OnFrameDockChange(object sender, WindowFrameDockChangedEventArgs e)
    {
      _OutputPane.WriteLine("Dock state changed.");
      _OutputPane.WriteLine("  Docked: {0}", e.Docked);
      DisplayPosition(e.Position);
    }

    void OnFrameMove(object sender, WindowFramePositionChangedEventArgs e)
    {
      _OutputPane.WriteLine("Frame moved.");
      DisplayPosition(e.Position);
    }

    void OnFrameResize(object sender, WindowFramePositionChangedEventArgs e)
    {
      _OutputPane.WriteLine("Frame sized.");
      DisplayPosition(e.Position);
    }

    void OnFrameClose(object sender, WindowFrameCloseEventArgs e)
    {
      _OutputPane.WriteLine("Frame closed.");
      _OutputPane.WriteLine("  Reason: {0}", e.CloseOption);
    }

    void OnFrameShow(object sender, WindowFrameShowEventArgs e)
    {
      _OutputPane.WriteLine("Frame show state changed.");
      _OutputPane.WriteLine("  Reason: {0}", e.Reason);
    }

    void DisplayPosition(Rectangle rect)
    {
      _OutputPane.WriteLine("  New position: {0}", rect);
    }
  }

  [AutoActivate(true)]
  [DisplayName("Dynamic window events")]
  class EventsPane : OutputPaneDefinition
  {
  }
}
Dynamic Tool Window UI:

using System;
using System.Drawing;
using System.Windows.Forms;
using VSXtra;

namespace DeepDiver.DynamicToolWindow
{
  public partial class DynamicWindowControl : UserControl
  {
    public DynamicWindowControl()
    {
      InitializeComponent();
    }

    public void RefreshValues(object sender, EventArgs arguments)
    {
      var frame = sender as WindowFrame;
      if (frame == null) return;
      Rectangle rect;
      var framePos = frame.GetWindowPosition(out rect);
      xText.Text = rect.Left.ToString();
      yText.Text = rect.Top.ToString();
      widthText.Text = rect.Width.ToString();
      heightText.Text = rect.Height.ToString();
      dockedCheckBox.Checked = framePos == FramePosition.Docked;
      Invalidate();
    }
  }
}
Command handler source code

using System.Runtime.InteropServices;
using VSXtra;

namespace DeepDiver.DynamicToolWindow
{
  [Guid(GuidList.guidDynamicToolWindowCmdSetString)]
  public sealed class DynamicToolWindowCommandGroup: CommandGroup<DynamicToolWindowPackage>
  {
    [CommandId(CmdIDs.cmdidMyTool)]
    [ShowToolWindowAction(typeof(DynamicWindowPane))]
    public sealed class ShowToolCommand : MenuCommandHandler { }
  }
}
Screenshots:

DynamicToolWindow1.png

DynamicToolWindow2.png

Last edited Aug 5, 2008 at 6:52 AM by INovak, version 6

Comments

No comments yet.