2

Question: Is there a way to always let a click, that brings a form in to focus, through an have effect on the form?

Background: With my (C# win form) application out of focus I can hover the form and get shades and borders indicating where my mouse is.

Clicking for example a menu entry (File) the form gets focus but the file menu does not get click. This takes an additional click.

For an ordinary button on the form only one click is required.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Tomas
  • 21
  • 1
  • Since this is standard behavior for menus, you will find it like this in almost every application. For that reason, you can de-prioritize the importance of "fixing" this because users are accustomed to it already. The exception would be if this scenario has a special meaning in your application. – tenfour Feb 10 '11 at 15:33
  • Possible duplicate of [ToolStrip sometimes not responding to a mouse click](https://stackoverflow.com/questions/472301/toolstrip-sometimes-not-responding-to-a-mouse-click) – Doc Brown Dec 14 '17 at 13:43

2 Answers2

4

This can be fixed by setting focus before the click occurs. Se code:

class ToolStripEx : System.Windows.Forms.ToolStrip
{
    protected override void WndProc(ref Message m)
    {
        // WM_MOUSEACTIVATE = 0x21
        if (m.Msg == 0x21 && this.CanFocus && !this.Focused)
        {
            this.Focus();
        }
        base.WndProc(ref m);
    }
}

This approach also works on MenuStrip

ToPa
  • 363
  • 3
  • 7
1

I found a few helpful articles – especially this one by Rick Brewster. The solution lies in overriding the WndProc method for the ToolStrip (or MenuStrip):

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    if (this.clickThrough &&
        m.Msg == NativeConstants.WM_MOUSEACTIVATE &&
        m.Result == (IntPtr)NativeConstants.MA_ACTIVATEANDEAT)
    {
        m.Result = (IntPtr)NativeConstants.MA_ACTIVATE;
    }
}
Andrei Krasutski
  • 4,913
  • 1
  • 29
  • 35
  • Also ensure `myToolStrip.ClickThrough = true` in the designer code otherwise I found it doesn't work! – SharpC May 10 '19 at 15:25