父窗体正在给前面的子窗体菜单条被点击时 [英] Parent form is bringing to front when the menu strip of a child form is clicked

查看:174
本文介绍了父窗体正在给前面的子窗体菜单条被点击时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty的奇怪,但reproductible问题。

我有一个的MenuStrip ,可打开与 Form.Show()方法,一种新的模式窗体。

子窗体也有一个菜单条。

当你开始通过单击子窗体的菜单条奇怪的事情发生。然后父窗体是回来前景和打招呼。这是一个真正的痛苦。

如何prevent这个问题?

一个史科瑟思电影通过以下链接 zippyshare.com 说明我的问题(3MO)

正如你可以看到在视频,父窗体没有考虑重点,它只是bringed前由somehing人。

需要注意的是repacing的的MenuStrip 的ToolStrip 解决这个问题。

有些code重现该问题:

 公共类DemoLostfocus:表
{
    私人无效的Ini​​tializeComponent()
    {
        this.menuStrip1 =新的MenuStrip();
        this.fileToolStripMenuItem =新ToolStripMenuItem();
        this.openModelessFormToolStripMenuItem =新ToolStripMenuItem();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();

        this.menuStrip1.Items.AddRange(新的ToolStripItem [] {
        this.fileToolStripMenuItem});
        this.menuStrip1.Location =新System.Drawing.Point(0,0);
        this.menuStrip1.Name =menuStrip1;
        this.menuStrip1.Size =新System.Drawing.Size(284,24);
        this.menuStrip1.TabIndex = 0;
        this.menuStrip1.Text =menuStrip1;

        this.fileToolStripMenuItem.DropDownItems.AddRange(新的ToolStripItem [] {
        this.openModelessFormToolStripMenuItem});
        this.fileToolStripMenuItem.Name =fileToolStripMenuItem;
        this.fileToolStripMenuItem.Size =新System.Drawing.Size(37,20);
        this.fileToolStripMenuItem.Text =文件;

        this.openModelessFormToolStripMenuItem.Name =openModelessFormToolStripMenuItem;
        this.openModelessFormToolStripMenuItem.Size =新System.Drawing.Size(187,22);
        this.openModelessFormToolStripMenuItem.Text =打开无模式表;
        this.openModelessFormToolStripMenuItem.Click + =新System.EventHandler(this.openModelessFormToolStripMenuItem_Click);

        this.AutoScaleDimensions =新System.Drawing.SizeF(6F,13F);
        this.AutoScaleMode = AutoScaleMode.Font;
        this.ClientSize =新System.Drawing.Size(284,262);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Name =DemoLostfocus;
        this.Text =DemoLostfocus;
        this.menuStrip1.ResumeLayout(假);
        this.menuStrip1.PerformLayout();
        this.ResumeLayout(假);
        this.PerformLayout();
    }

    私人的MenuStrip menuStrip1;
    私人ToolStripMenuItem fileToolStripMenuItem;
    私人ToolStripMenuItem openModelessFormToolStripMenuItem;

    公共DemoLostfocus()
    {
        的InitializeComponent();
    }

    私人无效openModelessFormToolStripMenuItem_Click(对象发件人,EventArgs的)
    {
        (新DemoLostfocus())显示()。
    }
}
 

解决方案

这是被引入.NET 4.5 pretty的讨厌的错误。知识库文章是可在这里。解决方法是目前仅可作为一个修补程序,希望这将使它成为一个服务的更新很快。我只是复制/粘贴说明:

  

假设你有一个.NET框架4.5的Windows窗体应用程序。当您单击菜单项来打开应用程序中的一个子窗口,通过菜单和子窗口交互出现错误的行为。

     

例如,您可能会遇到以下情况:

     

在打开的子窗口的上下文菜单,主窗口中把焦点。
     不能使用助记符来访问菜单项。

     

出现此问题的原因IMessageFilter接口脱钩太积极。因此,在.NET Framework 4.5不滤镜菜单中相关的窗口消息。


更新:这个问题是固定在发布了1月8日在.NET 4.5的更新,2013年知识库文章在这里

I have a pretty strange but reproductible problem.

I have a MenuStrip which can open a new Modeless form with the Form.Show() method.

The child form also have a Menu Strip.

The strange stuff happens when you start by clicking the menu strip of the child form. Then the parent form is coming back foreground and saying hello. That's a real pain.

How to prevent this problem?

A Scorcese movie to illustrate my problem by following this link zippyshare.com (3Mo)

As you can see in the video, parent form doesn't take focus, it is just bringed front by somehing else.

Note that repacing the MenuStrip by a ToolStrip correct the problem.

Some Code to reproduce the problem:

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();
    }
}

解决方案

This is a pretty nasty bug that was introduced in .NET 4.5. The KB article is available here. The fix is right now only available as a hotfix, hopefully it will make it into a service update soon. I'll just copy/paste the description:

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.


Update: this issue was fixed in a .NET 4.5 update released on Jan 8th, 2013. The KB article is here.

这篇关于父窗体正在给前面的子窗体菜单条被点击时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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