VSXtra-like implementation of the VS SDK's MenusAndCommands Sample

Package source:

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

namespace DeepDiver.DynamicCommands
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "DynamicComands", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [Guid(GuidList.guidDynamicCommandsPkgString)]
  public sealed class DynamicCommandsPackage : PackageBase
  {
  }
}

Command handlers source code:

// --- Command handlers
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.DynamicCommands
{
  [Guid(GuidList.guidDynamicCommandsCmdSetString)]
  public sealed class DynamicCommandGroup : CommandGroup<DynamicCommandsPackage>
  {
    [CommandId(CmdIDs.cmdidMyCommand)]
    [WriteMessageAction("Sample Command executed.")]
    public sealed class MyCommand : MenuCommandHandler { }

    [CommandId(CmdIDs.cmdidMyGraph)]
    [ShowMessageAction("Graph Command executed.")]
    public sealed class MyGraph : MenuCommandHandler { }

    [CommandId(CmdIDs.cmdidMyZoom)]
    [WriteMessageAction("Zoom Command executed.")]
    public sealed class MyZoom : MenuCommandHandler { }

    public abstract class DynamicVisibility : MenuCommandHandler
    {
      protected override void OnExecute(OleMenuCommand command)
      {
        var dynCommand1 = GetHandler<DynamicVisibility1>();
        var dynCommand2 = GetHandler<DynamicVisibility2>();
        if (dynCommand1 == null || dynCommand2 == null) return;
        dynCommand1.MenuCommand.Visible = command.CommandID.ID == CmdIDs.cmdidDynVisibility2;
        dynCommand2.MenuCommand.Visible = !dynCommand1.MenuCommand.Visible;
      }
    }

    [CommandId(CmdIDs.cmdidDynVisibility1)]
    [CommandVisible(true)]
    public sealed class DynamicVisibility1 : DynamicVisibility { }

    [CommandId(CmdIDs.cmdidDynVisibility2)]
    [CommandVisible(false)]
    public sealed class DynamicVisibility2 : DynamicVisibility { }

    [CommandId(CmdIDs.cmdidDynamicTxt)]
    public sealed class DynamicText : MenuCommandHandler
    {
      private int _ClickCount;
      protected override void OnExecute(OleMenuCommand command)
      {
        command.Text = "VSXtra Text Changed: " + ++_ClickCount;
      }
    }
  }
}

Screenshot:

ToolsMenu1.png

Last edited Aug 5, 2008 at 7:51 AM by INovak, version 4

Comments

No comments yet.