NotifyIcon - showing context menu

J

Jeff Gaines

I have written an app that spends most of its time in the System
Tray. The main form opens on double clicking the icon and the
context menu shows on right clicking the icon.

I would also like the context menu to show on left clicking the
icon. I have trapped the MouseDwon event and can show the
context menu but it appears at screen location 0,0. I can't use
this.PointToClient because the form is invisible when the icon
is clicked.

Any hints on working out what the location of the Context Menu
should be so it appears in the same place whether I right click
or left click?

Many thanks.
 
H

Herfried K. Wagner [MVP]

* Jeff Gaines said:
I have written an app that spends most of its time in the System
Tray. The main form opens on double clicking the icon and the
context menu shows on right clicking the icon.

I would also like the context menu to show on left clicking the
icon. I have trapped the MouseDwon event and can show the
context menu but it appears at screen location 0,0. I can't use
this.PointToClient because the form is invisible when the icon
is clicked.

<URL:http://dotnet.mvps.org/dotnet/samples/windowsandforms/downloads/SimpleNotifyIcon.zip>
 
H

Herfried K. Wagner [MVP]

* Jeff Gaines said:
Herfried

Danke schön, aber... (there's always an aber isn't there!)

The context menu only appears when the icon is clicked with the
right mouse button. Any thoughts on showing it in the correct
position with a left click?

I feel sorry, but that's non-standard behavior. Maybe you can use
'Cursor.Position' to get the current cursor position and then show the
context menu at this position.
 
J

Jeff Gaines

I feel sorry, but that's non-standard behavior. Maybe you can use
'Cursor.Position' to get the current cursor position and then show the
context menu at this position.

It might be non-standard but half the icons in my system tray
produce a menu when left clicked which is very useful when using
a laptop with a touch pad!

I can get the menu to appear, that's no problem, it's just the
location - particularly when the form is hidden.

MousePosition and Cursor.Position seem to produce 0,0 - I need
to use ClientToScreen I think but I don't have a visible control
to base it on.
 
H

Herfried K. Wagner [MVP]

* Jeff Gaines said:
It might be non-standard but half the icons in my system tray
produce a menu when left clicked which is very useful when using
a laptop with a touch pad!

Mine only show when right-clicked (I am using a right-hand mouse).
I can get the menu to appear, that's no problem, it's just the
location - particularly when the form is hidden.

MousePosition and Cursor.Position seem to produce 0,0 - I need
to use ClientToScreen I think but I don't have a visible control
to base it on.

Is the mouse cursor at the location of the icon?
 
J

Jeff Gaines

Mine only show when right-clicked (I am using a right-hand mouse).


Is the mouse cursor at the location of the icon?

Herfried

Norton Internet Security is a good example of getting the menu
with a left click - interestingly the menu doesn't show up in
quite the same place with a left click as with a right click.

The mouse pointer is on the Notify Icon when I click it, I
suspect that in the MouseUp event e.x and e.y are very small
numbers (I'll log them and check), it's a very small icon! I
know that you usually need to apply PointToClient or
PointToScreen to these co-ordinates but this property does not
exist in a NotifyIcon and cannot be used when the form is
invisible.

I guess I'll have to do some calculations and hard code it for
now.
 

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