NotifyIcon and ContextMenu

G

Glen

Can anyone tell me if there is a workable method to get the mouse cursor
position on the screen or the NotifyIcon position? I need to display a
context menu for the NotifyIcon when clicked and I'd like it to display
based on the relative position of the tray icon or mouse pointer if
possible (whichever method works).

Any help would be much appreciated.

- Glen
 
G

Greg Merideth

For the NotifyIcon I believe you need to write your own event
handler/message parsing loop and catch the mouse move's when the
NotifyIcon window appears.
 
N

Nicholas Paldino [.NET/C# MVP]

Glen,

What's wrong with using the ContextMenu property on the NotifyIcon
class? Or do you mean you need the context menu to come up when it is
left-clicked (as opposed to right-clicked)?

If that is the case, then I don't know if you can do that in .NET 1.1
(unless you do all of the interop code yourself and create your own tray
icon, forgoing the NotifyIcon class).

In .NET 2.0, the NotifyIcon class has new events to handle the mouse
down and mouse up events over the NotifyIcon.

Hope this helps.
 
G

Glen

Hi Greg,

Actually, I found an article in Google Groups that helped by creating a
form control object to reference:

private void ShowMenu()
{
Control control = new Control();
control.CreateControl();
Point menuPos = new Point(Cursor.Position.X, Cursor.Position.Y);
notifyIcon1.ContextMenu.Show(control, menuPos);
}

I still have to run some tests to see what kind of overhead this
produces and how to compensate, but it seems to work. Now the only
problem I have is when the icon is right-clicked, the event is fired
twice (once when right-clicking the icon and once when clicking the menu
option). I just need to figure out how to call a single event
regardless of right or left click.

Thanks for your help.

- Glen
 

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