Simple Tool Window Sample

This small package demonstrates how to create a simple tool window with VSXtra. The package has the same functionality as the tool window package created with the VSPackage wizard.

Points of Interest:

  • Everything uses declarative approach
  • Command handling requires only a simple method decorated with attributes.
  • CommandExecMethod signs that the method is to be executed as a response for a command.
  • The CommandId attribute defines how the command is bound to the method.
  • The command has a ShowToolWindowAction attribute defines a menu action declaratively.
  • Creating a new tool window pane is easy with the ToolWindowPane<TPackage, TUIControl> base class.
  • InitialCaption and BitmapResourceId attributes set up the tool window properties declaratively.
  • You can refer to a resource string in the InitialCaption attribute.

Package source code:

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

namespace DeepDiver.SimpleToolWindow
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "SimpleToolWindow", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [XtraProvideToolWindow(typeof(MyToolWindow))]
  [Guid(GuidList.guidSimpleToolWindowPkgString)]
  public sealed class SimpleToolWindowPackage : PackageBase
  {
    [CommandExecMethod]
    [CommandId(GuidList.guidSimpleToolWindowCmdSetString, CmdIDs.cmdidShowToolWindow)]
    [ShowToolWindowAction(typeof(MyToolWindow))]
    private void ShowToolWindow()
    {
    }
  }
}
Tool window pane source code:

using System.Runtime.InteropServices;
using VSXtra;

namespace DeepDiver.SimpleToolWindow
{
  [Guid("37159cbf-a17d-4d3a-8277-5f847dc59c92")]
  [InitialCaption("$ToolWindowTitle")]
  [BitmapResourceId(301)]
  public class MyToolWindow : ToolWindowPane<SimpleToolWindowPackage, MyControl>
  {
  }
}
Tool window UI source code:

using System.Security.Permissions;
using System.Windows.Forms;

namespace DeepDiver.SimpleToolWindow
{
  public partial class MyControl : UserControl
  {
    public MyControl()
    {
      InitializeComponent();
    }

    [UIPermission(SecurityAction.LinkDemand, Window = UIPermissionWindow.AllWindows)]
    protected override bool ProcessDialogChar(char charCode)
    {
      if (charCode != ' ' && ProcessMnemonic(charCode))
      {
        return true;
      }
      return base.ProcessDialogChar(charCode);
    }

    protected override bool CanEnableIme
    {
      get { return true; }
    }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
    private void button1_Click(object sender, System.EventArgs e)
    {
      MessageBox.Show(this,
                      string.Format(System.Globalization.CultureInfo.CurrentUICulture, "We are inside {0}.button1_Click()", this.ToString()),
                      "VSXtra Simple Tool Window");
    }
  }
}
Screenshots:

ShowToolWindowResponse.png

Last edited Aug 26, 2008 at 5:28 AM by INovak, version 4

Comments

No comments yet.