• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Menu for SystemTray Icon

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
0
karau
Asked:
karau
  • 5
  • 3
  • 2
  • +3
1 Solution
 
TAMCCommented:
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.
0
 
karauAuthor 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
0
 
Samir040498Commented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
karauAuthor 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.
0
 
igrooveCommented:
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);
  }
}

0
 
igrooveCommented:
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.
0
 
Samir040498Commented:
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

0
 
karauAuthor 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
0
 
Samir040498Commented:
I am not able to reach your site.
0
 
karauAuthor Commented:
I have also posted them on a more reliable server:

http://stack.ecn.purdue.edu/~karau/code
0
 
PriyeshCommented:
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..
0
 
vivi052098Commented:
Karau...
   I have used the tray icon with the popup menu without any problem . But i have added the menu items dynamically instead of loading from the resource. try out the following code it works....

in the dialog class header add member varible
CMenu   m_oMenu;

afx_msg LRESULT OnNotifyMessage(WPARAM wParam ,                       LPARAM lParam);
afx_msg void  OnItem1();
afx_msg void OnItem2();

Now in the cpp file add the following....
In the Create() override  add the menu items

      m_oMenu.CreatePopupMenu();
      m_oMenu.AppendMenu(MF_ENABLED|MF_STRING,                WM_USER+10, "Item1");
      m_oMenu.AppendMenu(MF_ENABLED|MF_STRING,                WM_USER+11, "Item2");


ON_MESSAGE(WM_USER+1 , OnNotifyMessage)

ON_COMMAND(WM_USER+10 , OnItem1)
ON_COMMAND(WM_USER+11 , OnItem2)

LRESULT CTestDlg::OnNotifyMessage(WPARAM wParam , LPARAM lParam)
{
     switch (lParam)
     {
      case WM_LBUTTONDBLCLK:
              ......................................
                  break;
      case WM_RBUTTONDOWN:
               CPoint oPoint;
                GetCursorPos(&oPoint);
      m_oMenu.TrackPopupMenu( TPM_RIGHTALIGN,                               oPoint.x ,oPoint.y ,this, NULL );
                   break;
}
           
void CTestDlg::OnItem1()
{

}
void CTestDlg::OnItem2()
{

}

try this code ..........all the best.....


0
 
karauAuthor Commented:
It worked great, Thanks

j. karau
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now