Windows需要单击才能激活窗口,然后再次单击将选择一个按钮.我该如何更改? [英] Windows requires a click to activate a window before a second click will select a button. How can I change this?

查看:66
本文介绍了Windows需要单击才能激活窗口,然后再次单击将选择一个按钮.我该如何更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是C#Windows窗体应用程序,.Net使我的主菜单栏和工具条按钮的默认行为是必须首先单击应用程序的窗口(任何位置),然后才能单击菜单或工具条按钮.我该如何更改?

My application is a C# Windows Forms Application and .Net somehow makes the default behavior of my main menu bar and tool strip buttons to be that you have to first click on my app's window (anywhere), before it will let you click the menu or tool strip button. How can I change this?

诸如记事本和写字板之类的旧应用程序不会以这种方式运行.但是所有Microsoft Office应用程序都可以.但这真烦人!

Old apps like Notepad and WordPad don't behave this way. But all the Microsoft Office apps do. But this is so annoying!

情况变得更糟,因为我的应用程序会弹出辅助窗口,并且每次更改窗口时,我必须先激活爆炸的窗口,然后才能执行任何操作.gh!

It gets worse because my app pops up secondary windows and each time I change windows, I have to first activate the blasted window before I can do anything. Ugh!

我希望有一些全局方法可以更改此行为,因此我不必单独重写很多控件(我没有尝试过).有人知道吗?

I am hoping there is some global way to change this behavior, so I don't have to override lots of controls individually (I haven't tried that). Does anybody know?

请注意,我的应用程序对话框上的控件没有表现出这种愚蠢的行为.他们为什么不同?

Note that controls on my app's dialogs don't exhibit this stupid behavior. Why are they different?

推荐答案

好吧,我最终找到了自己的解决方案,它非常简单.我派生自己的ToolStrip类(和MenuStrip类),如下所示:

Well, I ended up finding my own solution that it pretty simple. I derive my own ToolStrip class (and MenuStrip class) as follows:

// This version of ToolStrip enables mouse click's even when the main form is NOT active.
private class MyToolStrip : ToolStrip
{
    const uint WM_LBUTTONDOWN = 0x201;
    const uint WM_LBUTTONUP   = 0x202;

    static private bool down = false;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg==WM_LBUTTONUP && !down) {
            m.Msg=(int)WM_LBUTTONDOWN; base.WndProc(ref m);
            m.Msg=(int)WM_LBUTTONUP;
            }

        if (m.Msg==WM_LBUTTONDOWN) down=true;
        if (m.Msg==WM_LBUTTONUP)   down=false;

        base.WndProc(ref m);
    }
}

看来,就像Mac OS X一样,Windows做出了GUI样式的决定,要求首先激活一个窗口,然后才能选择任何控件.但是,Windows仅对诸如ToolStrip和MenuStrip之类的特定控件执行此操作.当您的窗口处于非活动状态时,不会向这些控件发送鼠标DOWN事件.我不确定WinForms如何执行此GUI准则,但也许它使用通过Application.AddMessageFilter()使用消息过滤器.

It appears that, just like the Mac OS X, Windows has made a GUI style decision that requires one to first activate a window BEFORE it will allow any controls to be selected. However, Windows only does this for specific controls, like ToolStrip and MenuStrip. When your window is NOT active, these controls are not sent the mouse DOWN event. I'm not sure how WinForms enforces this GUI guideline, but perhaps it uses a message filter using Application.AddMessageFilter().

无论如何,MenuStrip控件仍会获取鼠标UP事件.这启发了我的解决方案.我只是在寻找缺少其对应的DOWN事件的任何鼠标UP事件.当我看到这种特殊情况时,我将生成自己的鼠标DOWN事件,对于ToolStrip控件,整个世界都很高兴.:-)

Regardless, the MenuStrip controls STILL get the mouse UP event. And that inspired my solution. I simply look for any mouse UP event that is missing it's corresponding DOWN event. When I see this peculiar case, I generate my own mouse DOWN event, and for the ToolStrip controls, the world is all happy. :-)

这篇关于Windows需要单击才能激活窗口,然后再次单击将选择一个按钮.我该如何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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