How to show a ContextMenuStrip without displaying at taskbar?

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
 
R

RichardH

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
);
}
};
 
Joined
Jun 28, 2012
Messages
1
Reaction score
0
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:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top