Display menu on notifyIcon with left mouse click?

M

mdb

My app has a notify icon in the systray. I want the left mouse click to
initiate a menu, and a right-mouse click to do something else. Normally,
for a button, I would listen to MouseDown and if I need to display the
menu, I would contextMenu.Show(buttonCtrl, new Point(0)). But I can't do
this for a notifyIcon because notifyIcons aren't controls. How can I
display a contextMenu where the cursor is by command only?

-mdb
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

Unfortunately you can not do this. But you can handle click events for
notify icon and show your own contextr menu.. But it could be hard to
control position of the menu and when to hide menu....
 
J

John B

mdb said:
My app has a notify icon in the systray. I want the left mouse click to
initiate a menu, and a right-mouse click to do something else. Normally,
for a button, I would listen to MouseDown and if I need to display the
menu, I would contextMenu.Show(buttonCtrl, new Point(0)). But I can't do
this for a notifyIcon because notifyIcons aren't controls. How can I
display a contextMenu where the cursor is by command only?

-mdb
Hi,
The notify Icon has a context menu property.
Create your context menu and then associate that menu with your notify
icon at design time (or run time) and it will automagically pop up.

HTH
JB
 
M

mdb

Hi,
The notify Icon has a context menu property.
Create your context menu and then associate that menu with your notify
icon at design time (or run time) and it will automagically pop up.

You misunderstand (or I didn't make it clear enough). I already have a
ContextMenu associated with the notifyIcon. The problem is that I want to
swap the meaning of the left/right mouse button, so I want the ContextMenu
to appear when I click the LEFT mouse button (it normally appears with the
right button.) And I want to perform a custom action with the RIGHT mouse
button.

Now, I can catch the left and right mouse clicks without any problem. The
problem occurs when, on a left mouse click, how to get the ContextMenu to
appear, because the Show(...) method of ContextMenu requires a
System.Windows.Forms.Control, which NotifyIcon is not.

-mdb
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

Show your menu manually. Call its Show method.. Assign this as first
parameter; assign a Point that u calculate for position. For instance;

new Point(Screen.PrimaryScreen.Bounds.Width-Left),
Screen.PrimaryScreen.Bounds.Height-Top)

This shows your menu @right bottom corner.But you have to decide when to
clsoe context menu...
 
W

Wayne

I'm still wondering why you would want to change the default standard
behavior. Depending on why I would find using your app very annoying and
probably uninstall it.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
M

mdb

I'm still wondering why you would want to change the default standard
behavior. Depending on why I would find using your app very annoying and
probably uninstall it.

Well first of all, its a design consideration that shouldn't concern anyone
but me and my customers. Many of them have expressed that they want the
menu to appear on the left mouse click, because this is the "primary
function" of the program. They want a window to appear when a right-click
occurs because this is a "secondary function" of the program.

Second of all, I'm not planning to force this on anyone - it will be an
option. If you want it the way you want it, fine. But the problem still
exists that I can't do what they are asking me to do, which is why I'm
looking for some help here.

-mdb
 
M

mdb

Show your menu manually. Call its Show method.. Assign this as first
parameter; assign a Point that u calculate for position. For instance;

new Point(Screen.PrimaryScreen.Bounds.Width-Left),
Screen.PrimaryScreen.Bounds.Height-Top)

This shows your menu @right bottom corner.But you have to decide when to
clsoe context menu...

Ummmm.... the Show(...) method for a ContextMenu is

[C#]
public void Show(
Control control,
Point pos
);

What would I use for the 'Control'? If I set it to 'null', I get:

An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: 'null' is not a valid value for 'control'.

-mdb
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

this

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

mdb said:
Show your menu manually. Call its Show method.. Assign this as first
parameter; assign a Point that u calculate for position. For instance;

new Point(Screen.PrimaryScreen.Bounds.Width-Left),
Screen.PrimaryScreen.Bounds.Height-Top)

This shows your menu @right bottom corner.But you have to decide when to
clsoe context menu...

Ummmm.... the Show(...) method for a ContextMenu is

[C#]
public void Show(
Control control,
Point pos
);

What would I use for the 'Control'? If I set it to 'null', I get:

An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: 'null' is not a valid value for 'control'.

-mdb
 
M

mdb


OK... This helped me get much closer to solving the problem, but simply
setting 'this' isn't the full answer, although it did help, and I'm still
not quite where I need to be. Read on for details...

If I call

notifyIcon1.Show(this, new Point(0,0))

then the menu appears in the upper left-hand corner of the screen instead
of where my notifyIcon is, because 'this' is a windows form that is
minimized. For a while, it seemed like no matter what I put for the point
(even Cursor.Position), it always appeared in the upper left-hand corner.
But then I realized that the value of Form.Location for a minimized form is
(-32000, -32000), and these extreme values were just overriding the small
values I was using. So instead of Point(0) or Cursor.Position, I did this:

notifyIcon1.Show(this, this.PointToClient(Cursor.Position))

which works quite well. (I don't mind if the Menu shows where the cursor
is instead of exactly where the notifyIcon is.)

BUT... now I can't exit my menu???? That is, if I choose NOT to click
any of my items, I can't get the menu to go away. I press ESC, I click
outside of the menu window... the ContextMenu still stays up. This is NOT
a problem when the ContextMenu is used in the normal manner with the
notifyIcon.ContextMenu property.

So now my question is... How can I get the ContextMenu to go away when I
click outside of it or press ESC if I am displaying it using the Show(...)
method? I could just put a dummy "Exit Menu" item that does nothing, but
that is kind of cheesy.

Getting closer - thanks!

-mdb
 
M

mdb

BUT... now I can't exit my menu???? That is, if I choose NOT to
click any of my items, I can't get the menu to go away. I press ESC,
I click outside of the menu window... the ContextMenu still stays up.
This is NOT a problem when the ContextMenu is used in the normal
manner with the notifyIcon.ContextMenu property.

So now my question is... How can I get the ContextMenu to go away
when I click outside of it or press ESC if I am displaying it using
the Show(...) method? I could just put a dummy "Exit Menu" item that
does nothing, but that is kind of cheesy.

Interesting thing I found... if I click my MenuItems, then it works the
way I would want, but if I simply mouse over them, and then click off of
the menu, it does not exit. So this seems to be more of a mouse capture
issue than anything else.

-mdb
 
J

John B

mdb said:
You misunderstand (or I didn't make it clear enough). I already have a
ContextMenu associated with the notifyIcon. The problem is that I want to
swap the meaning of the left/right mouse button, so I want the ContextMenu
to appear when I click the LEFT mouse button (it normally appears with the
right button.) And I want to perform a custom action with the RIGHT mouse
button.

Now, I can catch the left and right mouse clicks without any problem. The
problem occurs when, on a left mouse click, how to get the ContextMenu to
appear, because the Show(...) method of ContextMenu requires a
System.Windows.Forms.Control, which NotifyIcon is not.

-mdb

Doh,
Sorry, I really should read the post twice before answering.

JB
:)
 
W

Wayne

Wasn't trying to offend you by saying that. I've just seen time and time
again where new developers have tried to change the default, normal,
expected behavior of their app so that it was different than everything else
with out thinking fully of the effects on their potential customers of doing
it. So questioning that usually makes them think about it a bit more. It is
obvious now that you have thought about that.

Wayne
 
M

Mick Doherty

Instead of hiding the form, move it off screen and then in the contextmenus
popup event call this.Activate()

Alternatively ou could pInvoke SetForegroundWindow.
 
M

mdb

Instead of hiding the form, move it off screen and then in the
contextmenus popup event call this.Activate()

Alternatively ou could pInvoke SetForegroundWindow.

Yeah I thought about moving it off screen, and it seemed to work pretty
well, but I went back (never tried the Activate method). Thanks for the
tip - I'll try this to see if it works any better.

-mdb
 
W

Wayne

If you are moving it off screen be sure to check the Screen object and
position the form so it will truly be off screen. I have two monitors where
my secondary is to the left, so the cords on it are all negative. Not sure
if your customers would have this issue or not, just something else to be
careful of.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 

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