Activate a form and process button click at the same time?

前端 未结 2 1452
予麋鹿
予麋鹿 2021-02-15 17:52

I\'m using Windows Forms in C#.

I have a main form with a couple of toolbars that contain toolStripButtons. After working with another form that contains data, the main

2条回答
  •  深忆病人
    2021-02-15 18:22

    What you need to do is create a class that inherits ToolStrip and handles the WndProc. This is one way to do it. There are others.

    private class MyToolStrip : ToolStrip
    {
        private const uint WM_LBUTTONDOWN = 0x201;
        private const uint WM_LBUTTONUP   = 0x202;
    
        private static 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);
        }
    }
    

    I've also seen this solution:

    protected override void WndProc(ref Message m)
    {
        // WM_MOUSEACTIVATE = 0x21
        if (m.Msg == WM_MOUSEACTIVATE && this.CanFocus && !this.Focused)
            this.Focus();
        base.WndProc(ref m);
    }
    

    I ran into this at the last place I worked, I think the solution I came up with worked more like the latter, but I don't have access to the exact code I used.

提交回复
热议问题