如何使用复选框创建命令菜单项? [英] How to create command menu item with checkbox?

查看:22
本文介绍了如何使用复选框创建命令菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 VSPackage,我需要有带复选框的菜单项,就像下面的示例图片一样:

I'm writing a VSPackage and I need to have menu item with checkbox, just like on this sample image below:

我浏览了关于此 msdn 参考.vsct 文件,bud 没有提供任何解释如何操作的信息.我现在拥有的是带有图标和文本的标准菜单项(来自 MyPackage.vsct 文件的代码示例):

I went through this msdn reference regarding .vsct files, bud didn't fine any information explaining how to do it. What I have now is standard menu item with icon and text (code sample from MyPackage.vsct file):

<Buttons>     
  <Button guid="guidMyPackageCmdSet" id="cmdidMyPackage" type="Button">
    <Icon guid="guidImages" id="myPackageBitmap" />
    <CommandFlag>TextChanges</CommandFlag>
    <CommandFlag>DontCache</CommandFlag>
    <CommandFlag>FixMenuController</CommandFlag>
    <Strings>
      <ButtonText>MyPackage</ButtonText>
    </Strings>
  </Button>      
</Buttons>

我需要这个额外的复选框.怎么做?

I need this additional checkbox. How to do it?

推荐答案

CheckedVisibleEnabled 等属性无法通过 VSCT 文件定义支持的.您需要一个控制命令状态的命令处理程序.我创建了一个基类,它封装了 OleMenuCommand 实例的创建并处理命令的 BeforeQueryStatus 事件.这是我的实现的精简版,但它会给你一个如何解决它的想法......

The properties like Checked, Visible, Enabled or Supported can´t be defined via the VSCT file. You need a command handler that controls the command´s state. I´ve created a base class that wraps the creation of the OleMenuCommand instance and handles the command´s BeforeQueryStatus event. This is a slimmed version of my implementation, but it will give you an idea how to solve it...

internal abstract class CommandHandler : IDisposable
{
    private readonly OleMenuCommand command;

    protected CommandHandler(Guid group, int id)
    {
        var commandid = CommandID(group, id);
        this.command = new OleMenuCommand(this.Invoke, commandId);
        this.command.BeforeQueryStatus += this.OnBeforeQueryStatus;
    }

    protected virtual void OnExecute() { } 

    protected virtual void OnQueryStatus(QueryStatusEventArgs e) { }

    private void Invoke(object sender, EventArgs e)
    {
        this.OnExecute();
    }

    private void OnBeforeQueryStatus(object sender, EventArgs e)
    {
        OleMenuCommand command;
        if ((command = sender as OleMenuCommand) != null)
        {
            var e = new QueryCommandEventArgs
            {
                Checked = command.Checked,
            }

            this.OnQueryStatus(e);

            command.Checked = e.Checked;
        }
    }

    public void Dispose()
    {
        this.command.BeforeQueryStatus -= this.OnBeforeQueryStatus;
    }
}

public class QueryCommandEventArgs : EventArgs
{
    public bool Checked { get; set; }
}

CommandHandler 类允许控制任何菜单命令的状态.只需从中派生出新的处理程序实现并覆盖 OnExecuteOnQueryStatus 方法,例如...

The CommandHandler class allows to control the state of any menu command. Just derive new handler implementations from it and override the OnExecute and OnQueryStatus methods, like...

internal sealed class MyCommand : CommandHandler
{
    private bool checked;

    public MyCommand() : base(GuidCmdSet, MyCommandId) { }

    protected override void OnExecute()
    {
        this.checked = !this.checked; // toggle checked state
    }

    protected override void OnQueryStatus(QueryStatusEventArgs e)
    {
        e.Checked = this.checked;
    }
}

这篇关于如何使用复选框创建命令菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆