How do I fire a mouse click on a C# MenuStrip when a form become activated/focused?

1.4k Views Asked by At

Possible Duplicate:
Click lost on focusing form

If a form with a MenuStrip is not the window that has focus and you click on the MenuStrip, it uses that first click to make the window active, and then you have to click the MenuStrip again to get the menu to drop down. Buttons work differently. If a form with a button is not active/does not have focus and you click that button, it will register as a button click on that first click, as well as simultaneously making that form active/focused. I really need that menu on the MenuStrip to drop down on that first click, even if the form isn't active.

I tried using the OnMouseClick(...) method to fire a simulated mouse click when the form Enter and/or Activate events are triggered but that doesn't work. The Enter and Activate events are triggered on Mouse Down, so by putting an OnMouseClick(...) call in the Enter or Activate event handler, it's trying to fire a SECOND mouse click before the first mouse click has been released.

I somehow need the OnMouseClick(...) to occur after the Activate event occurs and then after MouseUp occurs.

1

There are 1 best solutions below

0
On

Use this MenuStrip derivate as replacement:

public class ActivatingMenuStrip : MenuStrip
{
    public ActivatingMenuStrip()
    {
        InitializeComponent();
    }


    int WM_MOUSEACTIVATE = 0x21;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_MOUSEACTIVATE)
        {
            this.Parent.Focus();
        }
        base.WndProc(ref m);
    }

    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
    }

}