Trouble with Menu event and TrackPopupMenuEx()

  • Thread starter Ignacio X. Domínguez
  • Start date
I

Ignacio X. Domínguez

Hi everyone. Hope you can help me out on this one.

I'm developing a Component which has a property ContextMenu. This component,
when placed on a form with a working ContextMenu (events assigned to
elements and proven to work with labels) displays an icon in the
notification area by calling ShellNotifyIcon() (don't want to use NotifyIcon
class). The form's ContextMenu is assigned to the property ContextMenu of
the Component. I'm able to receive every event (mouse moves, mouse clicks)
from the tray icon using a NativeWindow inside the component (let's call it
Window), and when the user right clicks the icon, I display the menu using
the following code:

<snippet>
//Make sure the menu hides when I click anywhere else
SetForegroundWindow(
new HandleRef(this.Window, this.Window.Handle));
//Show menu
TrackPopupMenuEx(
new HandleRef(this.ContextMenu, this.ContextMenu.Handle),
0x40,
System.Windows.Forms.Cursor.Position.X,
System.Windows.Forms.Cursor.Position.Y,
new HandleRef(this.Window, this.Window.Handle),
null);
//Get focus so the menu won't reload if clicked again.
PostMessage(
new HandleRef(this.Window, this.Window.Handle), 0, 0, 0);
</snippet>

The menu gets displayed correctly in the right position, but when a menu
item is clicked it just closes and nothing happens. It should execute the
code associated to that menu item like it does when it is displayed from a
label in the same form that has my Component. What is going on? I'm pretty
sure that NotifyIcon does the same thing to display its ContextMenu.

Any ideas?

thanks
 
M

Mattias Sjögren

The menu gets displayed correctly in the right position, but when a menu
item is clicked it just closes and nothing happens. It should execute the
code associated to that menu item like it does when it is displayed from a
label in the same form that has my Component. What is going on?

When the regular NotifyIcon recieves a WM_COMMAND message it
dispatches it to the right object using the internal class
System.Windows.Forms.Command. Since the class is non-public, you can't
easily do the same thing.



Mattias
 

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