Question

I need to show the context menu for my notification icon context menu for both left and right clicks after performing some actions rather than right after the click.

It works perfectly for the left-click as:

sysTrayIcon.MouseClick += OnSysTrayClicked;

private static void OnSysTrayClicked(Object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        PerformActions();
        MethodInfo oMethodInfo = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
        oMethodInfo.Invoke(sysTrayIcon, null);
    }
}

But the right-click is still handled automatically and the associated ContextMenuStrip is shown at the right-click. Is there some way to override that with my event handler?

Était-ce utile?

La solution

There are two options.

Option A: Do not assign the context menu to this.ContextMenuStrip. Open the context menu manually whenever you want to. Be aware, that strip.Top and strip.Left must be absolute screen coordinates.

Option B: De-assign this.ContextMenuStrip OnMouseDown if right-mouse-button and re-assign OnMouseUp. See this example.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top