How to show a ContextMenuStrip without displaying at taskbar?

  • Thread starter Thread starter Seraph Jiang
  • Start date Start date
S

Seraph Jiang

I am writing a NotifyIcon app.
I want to use Left mouse click to pop up a ContextMenuStrip.

Currently, I use
ContextMenuScrip.Show(x,y) to display it.
But it always show a windows at taskbar like I popup a window form.

Did I use the wrong method?

(e-mail address removed)

27th,Dec
 
I had the same problem. I could not find a way to achieve this without
using Reflection. This won't be officially supported, since it uses a
private method on the NotifyIcon class, but here's what I did (using an
anonymous method):

niMain.MouseClick += delegate( object sender, MouseEventArgs e )
{
if ( e.Button != MouseButtons.Right )
{
niMain.GetType().InvokeMember(
"ShowContextMenu",

BindingFlags.InvokeMethod|BindingFlags.Instance|BindingFlags.NonPublic,
null,
niMain,
null
);
}
};
 
The best and right way, without Reflection is:


UnsafeNativeMethods.SetForegroundWindow(new HandleRef(notifyIcon.ContextMenuStrip, notifyIcon.ContextMenuStrip.Handle));
notifyIcon.ContextMenuStrip.Show(Cursor.Position);


where UnsafeNativeMethods.SetForegroundWindow is:

public static class UnsafeNativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetForegroundWindow(HandleRef hWnd);
}
 
Last edited:
Back
Top