Skip to main content
 首页 » 编程设计

c#之单击子窗体的菜单条时,父窗体将置于最前面

2024年02月20日14虾米哥

我有一个很奇怪但可以重现的问题。

我有一个 MenuStrip,它可以使用 Form.Show() 方法打开一个新的无模式窗体。

子窗体也有一个菜单条。

当您通过单击子窗体的菜单条开始时,会发生奇怪的事情。然后父表单回到前台并打招呼。这是一个真正的痛苦。

如何防止这个问题?

一部 Scorcese 电影通过点击此链接来说明我的问题 zippyshare.com (3个月)

正如您在视频中看到的,父表单没有获得焦点,它只是被其他东西带到了前面。

请注意,将 MenuStrip 替换为 ToolStrip 可以解决问题。

重现问题的一些代码:

public class DemoLostfocus : Form 
{ 
    private void InitializeComponent() 
    { 
        this.menuStrip1 = new MenuStrip(); 
        this.fileToolStripMenuItem = new ToolStripMenuItem(); 
        this.openModelessFormToolStripMenuItem = new ToolStripMenuItem(); 
        this.menuStrip1.SuspendLayout(); 
        this.SuspendLayout(); 
 
        this.menuStrip1.Items.AddRange(new ToolStripItem[] { 
        this.fileToolStripMenuItem}); 
        this.menuStrip1.Location = new System.Drawing.Point(0, 0); 
        this.menuStrip1.Name = "menuStrip1"; 
        this.menuStrip1.Size = new System.Drawing.Size(284, 24); 
        this.menuStrip1.TabIndex = 0; 
        this.menuStrip1.Text = "menuStrip1"; 
 
        this.fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { 
        this.openModelessFormToolStripMenuItem}); 
        this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; 
        this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); 
        this.fileToolStripMenuItem.Text = "File"; 
 
        this.openModelessFormToolStripMenuItem.Name = "openModelessFormToolStripMenuItem"; 
        this.openModelessFormToolStripMenuItem.Size = new System.Drawing.Size(187, 22); 
        this.openModelessFormToolStripMenuItem.Text = "Open Modeless Form"; 
        this.openModelessFormToolStripMenuItem.Click += new System.EventHandler(this.openModelessFormToolStripMenuItem_Click); 
 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
        this.AutoScaleMode = AutoScaleMode.Font; 
        this.ClientSize = new System.Drawing.Size(284, 262); 
        this.Controls.Add(this.menuStrip1); 
        this.MainMenuStrip = this.menuStrip1; 
        this.Name = "DemoLostfocus"; 
        this.Text = "DemoLostfocus"; 
        this.menuStrip1.ResumeLayout(false); 
        this.menuStrip1.PerformLayout(); 
        this.ResumeLayout(false); 
        this.PerformLayout(); 
    } 
 
    private MenuStrip menuStrip1; 
    private ToolStripMenuItem fileToolStripMenuItem; 
    private ToolStripMenuItem openModelessFormToolStripMenuItem; 
 
    public DemoLostfocus() 
    { 
        InitializeComponent(); 
    } 
 
    private void openModelessFormToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
        (new DemoLostfocus()).Show(); 
    } 
} 

请您参考如下方法:

这是 .NET 4.5 中引入的一个非常讨厌的错误。知识库文章是 available here .该修复程序目前仅作为修补程序提供,希望它能尽快成为服务更新。我将复制/粘贴描述:

Assume that you have a .NET Framework 4.5-based Windows Form application. When you click a menu item to open a child window in the application, interactions with the menu and child window behave incorrectly.

For example, you may experience the following:

When you open a context menu in the child window, the main window takes the focus.
You cannot use mnemonics to access a menu item.

This issue occurs because the IMessageFilter interface is unhooked too aggressively. Therefore, the .NET Framework 4.5 does not filter menu-related window messages.


更新:此问题已在 2013 年 1 月 8 日发布的 .NET 4.5 更新中得到修复。知识库文章 is here .