Windows requires a click to activate a window before a second click will select a button. How can I change this?

后端 未结 4 1679
一向
一向 2021-02-13 03:26

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

4条回答
  •  没有蜡笔的小新
    2021-02-13 04:26

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

    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().

    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. :-)

提交回复
热议问题