c# - 使用 Control+Plus 的快捷方式创建 MenuItem – 使用反射修改 MenuItem 的私有(private)字段是最好的方法吗?

标签 c# .net winforms reflection menuitem

我正在使用旧版 MainMenu control (with MenuItem s) control in an application, and would like to implement zoom in and zoom out menu items (with Control++Control+- 键盘快捷键)。 (请注意,我使用的是 MainMenu 而不是 MenuStrip )。 MenuItem 确实有一个 Shortcut属性,类型 Shortcut , 但它没有 CtrlPlus 选项。

我决定看看如何Shortcut was implemented in the referencesource ,看起来每个枚举值只是几个 Keys 的组合枚举值(例如 CtrlA 只是 Keys.Control + Keys.A)。所以我尝试创建一个应该等于 Control+Plus 的自定义快捷键值:

const Shortcut CONTROL_PLUS = (Shortcut)(Keys.Control | Keys.Oemplus);

zoomInMenuItem.Shortcut = CONTROL_PLUS;

但是,当我尝试分配 Shortcut 属性时,这会引发 InvalidEnumArgumentException

所以我决定使用反射,并修改(非公开)MenuItemDatashortcut 属性,然后调用(非公开)UpdateMenuItem方法。这实际上有效(具有在菜单项中显示为 Control+Oemplus 的副作用):

const Shortcut CONTROL_PLUS = (Shortcut)(Keys.Control | Keys.Oemplus);

var dataField = typeof(MenuItem).GetField("data", BindingFlags.NonPublic | BindingFlags.Instance);
var updateMenuItemMethod = typeof(MenuItem).GetMethod("UpdateMenuItem", BindingFlags.NonPublic | BindingFlags.Instance);
var menuItemDataShortcutField = typeof(MenuItem).GetNestedType("MenuItemData", BindingFlags.NonPublic)
    .GetField("shortcut", BindingFlags.NonPublic | BindingFlags.Instance);

var zoomInData = dataField.GetValue(zoomInMenuItem);
menuItemDataShortcutField.SetValue(zoomInData, CONTROL_PLUS);
updateMenuItemMethod.Invoke(zoomInMenuItem, new object[] { true });

虽然该方法有效,但它使用了反射,我不确定它是否面向 future 。

我正在使用 MenuItem而不是更新的ToolStripMenuItem因为我需要拥有 RadioCheck 属性(以及其他原因);放弃它不是一种选择。

A form that has 2 menu items:
* Zoom in (Ctrl+Oemplus)
* Zoom out (Ctrl+OemMinus)

下面是创建上述对话框的一些完整代码,显示了我要完成的工作(最相关的代码在 OnLoad 方法中):

缩放窗体.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ZoomMenuItemMCVE
{
    public partial class ZoomForm : Form
    {
        private double zoom = 1.0;
        public double Zoom {
            get { return zoom; }
            set {
                zoom = value;
                zoomTextBox.Text = "Zoom: " + zoom;
            }
        }

        public ZoomForm() {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e) {
            const Shortcut CONTROL_PLUS = (Shortcut)((int)Keys.Control + (int)Keys.Oemplus);
            const Shortcut CONTROL_MINUS = (Shortcut)((int)Keys.Control + (int)Keys.OemMinus);

            base.OnLoad(e);

            //We set menu later as otherwise the designer goes insane (http://stackoverflow.com/q/28461091/3991344)
            this.Menu = mainMenu;

            var dataField = typeof(MenuItem).GetField("data", BindingFlags.NonPublic | BindingFlags.Instance);
            var updateMenuItemMethod = typeof(MenuItem).GetMethod("UpdateMenuItem", BindingFlags.NonPublic | BindingFlags.Instance);
            var menuItemDataShortcutField = typeof(MenuItem).GetNestedType("MenuItemData", BindingFlags.NonPublic)
                .GetField("shortcut", BindingFlags.NonPublic | BindingFlags.Instance);

            var zoomInData = dataField.GetValue(zoomInMenuItem);
            menuItemDataShortcutField.SetValue(zoomInData, CONTROL_PLUS);
            updateMenuItemMethod.Invoke(zoomInMenuItem, new object[] { true });

            var zoomOutData = dataField.GetValue(zoomOutMenuItem);
            menuItemDataShortcutField.SetValue(zoomOutData, CONTROL_MINUS);
            updateMenuItemMethod.Invoke(zoomOutMenuItem, new object[] { true });
        }

        private void zoomInMenuItem_Click(object sender, EventArgs e) {
            Zoom *= 2;
        }

        private void zoomOutMenuItem_Click(object sender, EventArgs e) {
            Zoom /= 2;
        }
    }
}

ZoomForm.Designer.cs

namespace ZoomMenuItemMCVE
{
    partial class ZoomForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.MenuItem viewMenuItem;
            this.zoomTextBox = new System.Windows.Forms.TextBox();
            this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
            this.zoomInMenuItem = new System.Windows.Forms.MenuItem();
            this.zoomOutMenuItem = new System.Windows.Forms.MenuItem();
            viewMenuItem = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // zoomTextBox
            // 
            this.zoomTextBox.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.zoomTextBox.Location = new System.Drawing.Point(0, 81);
            this.zoomTextBox.Name = "zoomTextBox";
            this.zoomTextBox.ReadOnly = true;
            this.zoomTextBox.Size = new System.Drawing.Size(292, 20);
            this.zoomTextBox.TabIndex = 0;
            this.zoomTextBox.Text = "Zoom: 1.0";
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            viewMenuItem});
            // 
            // viewMenuItem
            // 
            viewMenuItem.Index = 0;
            viewMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.zoomInMenuItem,
            this.zoomOutMenuItem});
            viewMenuItem.Text = "View";
            // 
            // zoomInMenuItem
            // 
            this.zoomInMenuItem.Index = 0;
            this.zoomInMenuItem.Text = "Zoom in";
            this.zoomInMenuItem.Click += new System.EventHandler(this.zoomInMenuItem_Click);
            // 
            // zoomOutMenuItem
            // 
            this.zoomOutMenuItem.Index = 1;
            this.zoomOutMenuItem.Text = "Zoom out";
            this.zoomOutMenuItem.Click += new System.EventHandler(this.zoomOutMenuItem_Click);
            // 
            // ZoomForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 101);
            this.Controls.Add(this.zoomTextBox);
            this.Name = "ZoomForm";
            this.Text = "ZoomForm";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MainMenu mainMenu;
        private System.Windows.Forms.TextBox zoomTextBox;
        private System.Windows.Forms.MenuItem zoomInMenuItem;
        private System.Windows.Forms.MenuItem zoomOutMenuItem;
    }
}

上面的代码可以正常工作,并且做了我想要的,但我不确定这样做是否正确(使用反射修改私有(private)变量通常看起来是不正确的方法)。我的问题是:

  • 是否有更好的方法将 MenuItem 的快捷方式设置为 Control++
  • 这种基于反射的方法会导致问题吗?

最佳答案

it uses reflection, and I'm not sure if it's future-proof

你会侥幸逃脱的,在引擎盖下没有发生特别危险的事情。 MainMenu/MenuItem 类是一成不变的,永远不会再改变。您是面向 future 的 Windows 版本,此快捷方式实际上并未由 Windows 实现,但已添加到 MenuItem。实际上是 Form.ProcessCmdKey() 方法使它起作用。这是你在不修改菜单项的情况下做的提示。将此代码粘贴到您的表单类中:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if (keyData == (Keys.Control | Keys.Oemplus)) {
        zoomInMenuItem.PerformClick();
        return true;
    }
    if (keyData == (Keys.Control | Keys.OemMinus)) {
        zoomOutMenuItem.PerformClick();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

你得到的快捷键描述很糟糕,没有正常人知道“Oemplus”可能意味着什么。不要使用自动生成的,写你自己的。你不能用设计器做到这一点,它不会让你在项目文本和快捷键描述之间输入制表符。但是代码没有问题:

public ZoomForm() {
    InitializeComponent();
    zoomInMenuItem.Text = "Zoom in\tCtrl +";
    zoomOutMenuItem.Text = "Zoom out\tCtrl -";
}

I'm using MenuItem and not the newer ToolStripMenuItem because...

这不是一个很好的理由。 ToolStripMenuItem 确实没有为单选按钮行为提供开箱即用的实现,但您自己添加它非常容易。 Winforms 使创建您自己的 ToolStrip 项目类变得简单,这些项目类在设计时可用,并且可以按照您在运行时选择的方式运行。向您的项目添加一个新类并粘贴如下所示的代码。编译。在设计时使用 Insert > RadioItem 上下文菜单项插入一个,Edit DropdownItems... 上下文菜单项可以轻松添加多个。您可以设置 Group 属性来指示哪些项目属于一起并且应该作为一个单选组。

using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
public class ToolStripRadioItem : ToolStripMenuItem {
    public int Group { get; set; }

    protected override void OnClick(EventArgs e) {
        if (!this.DesignMode) {
            this.Checked = true;
            var parent = this.Owner as ToolStripDropDownMenu;
            if (parent != null) {
                foreach (var item in parent.Items) {
                    var sibling = item as ToolStripRadioItem;
                    if (sibling != null && sibling != this and sibling.Group == this.Group) sibling.Checked = false;
                }
            }
        }
        base.OnClick(e);
    }
}

关于c# - 使用 Control+Plus 的快捷方式创建 MenuItem – 使用反射修改 MenuItem 的私有(private)字段是最好的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30197697/

相关文章:

c# - UIColor 子类中的工厂方法导致崩溃

c# - 如何从 MS Word 访问表格中具有不同单元格宽度的列

.net - 如何比较两个 .exe

c# - 客户端 - 服务器身份验证 - 使用 SSPI?

c# - 测试复杂实体

c# - 如何在 C# 打印文档中设置和打印自定义纸张尺寸?

c# - 如何在 ASP.NET MVC 中创建两列蛇形布局?

c# - 获取/设置私有(private)静态集合?

c# - dotTrace:是否有 TeamCity 插件?

c# - 为什么 KeyPress 的消息框显示在 KeyDown 之前?