Page 1 of 1

systray icon popup menu

Posted: Fri Aug 07, 2009 1:26 pm
by ConvertFromOldNGs
by John Munro >> Thu, 26 Mar 2009 22:01:46 GMT

I have a systray icon, and when you right-click it I want a popup menu to appear - as usual for systray icons.

Here is a simplified version of the code I'm using:

trayIconClicked(clickType: Integer) updating;

vars
ptPos : Point;
begin

if clickType = TrayIcon_RightClick then
call getCursorPos(ptPos);

popupMenu(mnuSystray, ptPos.x, ptPos.y);
endif;

end;

The getCursorPos external function is GetCursorPos in user32.

The problem is that the popup menu isn't acting like other popup menus from systray icons - it is very low on the screen and depending where you click the bottom most menu item can be off the bottom of the screen.

I couldn't find an indication in the help file what the x and y parameters popupMenu are relative to - I'm assuming they're just relative to the screen, but maybe that's not right.

Has anyone done this sort of thing before?

thanks,

John

Re: systray icon popup menu

Posted: Fri Aug 07, 2009 1:26 pm
by ConvertFromOldNGs
by Murray Symon >> Fri, 27 Mar 2009 0:03:55 GMT

Have you looked into the 'contextMenu' event? It provides the x,y position of the mouse click. I have made used of it, but never for the MS Windows systray. Translating coordinates can be done by Window::windowToScreen() and Window::screenToWindow().

Murray.

Re: systray icon popup menu

Posted: Fri Aug 07, 2009 1:26 pm
by ConvertFromOldNGs
by John Munro >> Fri, 27 Mar 2009 14:12:45 GMT

Thanks for your reply but unfortunately the contextMenu event doesn't get triggered when the systray icon is clicked

John

Re: systray icon popup menu

Posted: Fri Aug 07, 2009 1:26 pm
by ConvertFromOldNGs
by Torrie Moore >> Sat, 28 Mar 2009 6:51:18 GMT

From the example given in the Jade help (see help for Form::popupMenu) it appears that the coordinates are relative to the form. Perhaps someone from the plant could confirm this.

As Murray's suggested you should be able to use the Window::windowToScreen() and Window::screenToWindow() method to find the cursor position relative to the form. You may also find that you need to take the height of the popup menu into account when displaying the menu. Unfortunately it doesn't look like Jade provides the number of items in a sub menu via a method call so you may have to hard code it. I think that the height of a single menu item can be obtained with a call to the Window:::getSystemMetrics method passing in SM_CYMENU.

Regards

Torrie

Re: systray icon popup menu

Posted: Fri Aug 07, 2009 1:26 pm
by ConvertFromOldNGs
by John Munro >> Mon, 30 Mar 2009 14:39:58 GMT

For future reference here is the code that seems to pop the menu at the correct coordinates. I don't understand why the height of the menu needs to be added but I'm not going to argue with success:

trayIconClicked(clickType: Integer) updating;

constants
SM_CYMENU : Integer = 15;
vars
ptPos : Point;
rX, rY : Real;begin

if clickType = TrayIcon_RightClick then
call getCursorPos(ptPos);
rX := ptPos.x;
rY := ptPos.y;

screenToWindow(rX, rY);

rY := rY + getSystemMetrics(SM_CYMENU);

popupMenu(mnuSystray, rX.Integer, rY.Integer);
endif;

end;

Thanks to Torrie and Murray

John

Re: systray icon popup menu

Posted: Fri Aug 07, 2009 1:26 pm
by ConvertFromOldNGs
by Torrie Moore >> Tue, 31 Mar 2009 3:22:57 GMT

I'd suggested adding (I actually should have said subtract!) the menu height as the position passed to popupMenu would be the top left coordinates of the popup menu. Given that the icon is at the bottom of the screen, you would want the top left corner of the popup menu to be above the mouse, not at the same position. If the menu had more items, then I would expect that you would need to subtract a multiple of the item heights.

Regards

Torrie

Re: systray icon popup menu

Posted: Fri Aug 07, 2009 1:26 pm
by ConvertFromOldNGs
by John Munro >> Tue, 31 Mar 2009 14:06:20 GMT

That's strange, because with just getCursorPos and screenToWindow the popup menu appears exactly one SM_CYMENU above the mouse position, so adding SM_CYMENU made it line up perfectly.

John