c# - .NET 4.0/4.5 WinForms MenuStrip 窃取焦点的奇怪错误

标签 c# .net winforms

我们最近升级到 VS 2012,同时升级到 .NET Framework 4.5。这引入了一个与 WinForms MenuStrip 控件相关的奇怪且讨厌的错误。同样的错误也发生在以 .NET Framework 4.0 为目标的应用程序中,因为 4.5 的安装程序显然也会更新 4.0 的部分内容。因此,一个完美工作的代码模式现在仅仅因为框架升级就被打破了。

问题描述:
我有一个带有 MenuStrip 的 Form1。对于其中一个下拉项,事件处理程序打开另一个 Form2(不一定是子级,只是另一个 Form)。如果用户右键单击新的 Form2 或其子控件之一,这会触发 ContextMenuStrip 的 Show(),则原始 Form1 会再次弹出到前台。
这独立于 Form2 中所有先前的其他 UI 操作。可以调整大小、移动、最小化、最大化 Form2、在控件之间切换、输入文本等。不知何故,Form1 的 MenuStrip 似乎记得它导致了 Form2 的打开,并在第一次右键单击时捕获了焦点。

我正在尝试不同的方法,但到目前为止无法找到解决方法。该星座并不少见,可能会影响周围的许多 WinForms 应用程序。因此,我决定将其张贴在这里,因为可行的解决方案可能会受到普遍关注。如果有人知道解决方法或至少对我有一些线索,我将非常高兴。

我能够在以下代码中提炼出它的本质。它应该可以在任何安装了 .NET 4.5 的机器上重现,并且在分别针对 4.0 和 4.5 时发生 - 但不是在 3.5 或更低版本上。

using System;
using System.Windows.Forms;

namespace RightClickProblem {
    static class Program {
        [STAThread]
        static void Main() {
            // Construct a MenuStrip with one item "Menu" with one dropdown-item "Popup"
            var mainMenu = new MenuStrip();
            var mainItem = new ToolStripMenuItem("Menu");
            mainItem.DropDownItems.Add(new ToolStripMenuItem("Popup", null, Popup));
            mainMenu.Items.Add(mainItem);

            // Create form with MenuStrip and Show
            var form1 = new Form();
            form1.Controls.Add(mainMenu);
            Application.Run(form1);
        }

        private static void Popup(object sender, EventArgs e) {
            // Create a form with a right click handler and show
            var form2 = new Form();
            var contextMenu = new ContextMenuStrip();
            contextMenu.Items.Add("Just an item...");
            form2.ContextMenuStrip = contextMenu;
            form2.Show();
            // Problem: contextMenu.Show() will give focus to form1
        }
    }
}

编辑: 我花了一些时间单步调试 .NET Framework 源代码,发现根本原因很可能在 System.Windows.Forms.ToolStripManager 中。在那里,微软正在使用消息过滤器来跟踪窗口激活,这在某种程度上是错误地为 MenuStrip 实现的。
与此同时,我还发现 Microsoft 已经在修补程序中解决了这个问题(请参阅 http://support.microsoft.com/kb/2769674),希望这会在 .NET Framework 4.5 的 future 更新中找到解决方法。

不幸的是,很难在所有客户端计算机上强制应用此修补程序。因此,仍然会非常感谢可行的解决方法。到目前为止,我自己一直无法找到切实可行的解决方案......

EDIT#2: 最初的 KB 编号是针对 Win8 的,但在 KB 2756203 下有适用于 Win7 和 Vista 的类似修补程序。如果有人可以使用它,请在此处找到可供下载的修补程序:http://thehotfixshare.net/board/index.php?autocom=downloads&showfile=15569 .我们对其进行了测试,它确实解决了问题。如果我们很快发现肯定没有解决方法,我们将使用修补程序。

编辑#3:对 spajce 提出的已接受解决方案的评论
显然,在任何 ContextMenu 上调用 Show() 将说服原始 MenuStrip 忘记其对焦点的声明。这可以通过某种方式完成,这样虚拟的 ContextMenu 甚至不会显示在屏幕上。我找到了在任何 Form 的构造函数中插入以下代码片段的最短且最容易实现的方法:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();

        using (var dummyMenu = new ContextMenuStrip()) {
            dummyMenu.Items.Add(new ToolStripMenuItem());
            dummyMenu.Show(Point.Empty);
        }
    }
}

因此,每个打开的窗体都会清除 ToolStripMenu 系统的损坏状态。不妨将此代码放在 FormHelper.FixToolStripState() 之类的静态方法中,或者将其放在模板 Form 的 OnCreateControl(...) 中并从中继承所有 Forms(无论如何我们幸运地做了什么)。

最佳答案

这是我的解决方案:)

 static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Construct a MenuStrip with one item "Menu" with one dropdown-item "Popup"
            var mainMenu = new MenuStrip();
            var mainItem = new ToolStripMenuItem("Menu");
            mainItem.DropDownItems.Add(new ToolStripMenuItem("Popup", null, Popup));
            mainMenu.Items.Add(mainItem);

            // Create form with MenuStrip and Show
            var form1 = new Form();
            form1.Controls.Add(mainMenu);
            Application.Run(form1);
        }

        private static void Popup(object sender, EventArgs e)
        {
            // Create a form with a right click handler and show
            var form2 = new Form();
            var contextMenu = new ContextMenuStrip();
            contextMenu.Items.Add("Just an item...");
            var loc = form2.Location; //<---- get the location
            var p2 = new Point(loc.X, loc.Y); //<---- get the point of form
            form2.ContextMenuStrip = contextMenu;
            form2.ContextMenuStrip.Show(form2, p2); //<---- just add this code.
            form2.Show();
            // Problem: contextMenu.Show() will give focus to form1
        }
    }

关于c# - .NET 4.0/4.5 WinForms MenuStrip 窃取焦点的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14058222/

相关文章:

c# - 下拉箭头的宽度

c# - 创建包含 GUI 组件 Windows Phone 8 的 .dll 库

c# - 为什么我可以 .Add() 到一个只有 get 访问器的属性列表?

c# - 以编程方式分离调试器

.net - 覆盖 App.xaml 中的标准主题

c# - 新线程()和线程池?

c# - 显示前检查空 XML 数据验证

c# - 如何压缩两个不同大小的列表以创建一个与原始列表中最长的列表相同的新列表?

c# - 如何填充WebBrowser TextBox的?

c# - OLEDB 参数化查询