We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Menu for SystemTray Icon

karau
karau asked
on
Medium Priority
308 Views
Last Modified: 2013-11-20
I have created a dialog-based app that has an icon in the system tray, however, the menu for the icon doesn't function properly.  It looks as though it should, the resource file has all of the menu items enabled and I am pretty sure I loaded the menu correctly:

CMenu menu;
if (!menu.LoadMenu(m_NotifyIconData.uID))
  return 0;

CMenu* pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
  return 0;

Then later, after receiving a WM_RBUTTONUP message I do the tracking of the menu:

::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
CPoint p;
GetCursorPos(&p);
::SetForegroundWindow(m_Icon.hWnd);  
::TrackPopupMenu(pSubMenu->m_hMenu, 0, p.x, p.y, 0, m_Icon.hWnd, NULL);

The problem is that when the menu pops up, all of the menu items are grayed and disabled.  I think it is probably something small that I am just missing, any help would be appreciated.

Thanks,
j. karau
Comment
Watch Question

Commented:
The only time I have noticed that items are grayed out in any menu is when you don't have a event handling functions assigned to each of them.  You know the thing in classwizard where you choose a menu item and then you choose an event then you create a function.  Have you done that for any of your menu items, if you haven't, try it, and if it works, I'll thank you for the points later.

Author

Commented:
I am not using VC++, but yes I have all of the message handling functions written as well as the MESSAGE_MAP to go along with them.  At first I thought this was my problem, but it is still happening and I don't know why.

j. karau
From what I understand from your code extract...

You are loading the menu in a different place and then using track popup menu in a different place. Which is wrong!!!

The pointer returned by GetSubmenu cannot be stored.

So load your menu in the handler of RBUTTONUP just before
::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);

and it should work fine.

Regards
Samir

Author

Commented:
All of the code given is in the same function.  The tracking of the menu isn't after the message WM_RBUTTONUP is received, I was wrong originally, it is after an IF statement that determines that WM_RBUTTONUP was the message.

Commented:
Um, you have a reference to the submenu in pSubMenu.
You should (since it looks like you're using MFC) call:

pSubMenu->TrackPopupMenu(
  TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
  p.x, p.y);

Also, if you are using MFC, if the menu is NOT handled in a message map in the
window whose handle in NOTIFYICONDATA you fed the ::ShellNotifyIcon(), then of course the items will be all
greyed.

You can create a popup menu in that window's menu whose
topmost item is NOT visible.  Load the window menu like you did,
and then get the submenu whose index is the "invisible"
popup:

CMenu m;

if (m.LoadMenu(ID_OFWINDOWMENU))  {
  CMenu* pMenu = m.GetSubMenu(nIndexOfInvisible);

  if (pMenu)  {
    POINT p;

    GetCursorPos(&p);
    pSubMenu->TrackPopupMenu(
      TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
      p.x, p.y, this);
  }
}

Commented:
Also, if you can go to the menu, if its not handled anywhere, and invoke the Class Wizard.  It'll ask you about associating it with a class.  If your window is MFC derived (and visible to the Class Wizard) then you can get it to "ungrey" by associating it with that window's class.  Then you don't have to muck about
with the main window menu.
Incase igroove's answer isn't solving your problem it would be helpful if you could give the exact code that you have used. I have done somthing similar a couple of wks. ago and I had no trouble, getting it to work.

So maybe I can suggest somthing after comparing your code with mine

Samir

Author

Commented:
I was still having problems and was getting tired of it so I switched to using the CTrayIcon class that I found on the internet a while back.  But guess what the items are still grayed out.  If someone can help me I am posting all of the CTrayIcon code as well as my InitInstance function where I call the Create function in CTrayIcon.  They are located at:

http://shay.ecn.purdue.edu/~karau/code

j. karau
I am not able to reach your site.

Author

Commented:
I have also posted them on a more reliable server:

http://stack.ecn.purdue.edu/~karau/code

Commented:
Karau..
        Created an app with u'r code and it all works fine. I don't see any problem in that. Please consider changing the message map of the popup menu to the main app window. just for the sake of it.. no other suggestion..
        The problem with u is that the message maps are not considered now.. ... So please consider changing that..
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
It worked great, Thanks

j. karau
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.