Solved

Menu for SystemTray Icon

Posted on 1998-06-17
13
287 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
0
Comment
Question by:karau
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 1

Expert Comment

by:TAMC
Comment Utility
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
 

Author Comment

by:karau
Comment Utility
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
 

Expert Comment

by:Samir040498
Comment Utility
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
 

Author Comment

by:karau
Comment Utility
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
 
LVL 2

Expert Comment

by:igroove
Comment Utility
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
 
LVL 2

Expert Comment

by:igroove
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Expert Comment

by:Samir040498
Comment Utility
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
 

Author Comment

by:karau
Comment Utility
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
 

Expert Comment

by:Samir040498
Comment Utility
I am not able to reach your site.
0
 

Author Comment

by:karau
Comment Utility
I have also posted them on a more reliable server:

http://stack.ecn.purdue.edu/~karau/code
0
 
LVL 9

Expert Comment

by:Priyesh
Comment Utility
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
 

Accepted Solution

by:
vivi052098 earned 70 total points
Comment Utility
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
 

Author Comment

by:karau
Comment Utility
It worked great, Thanks

j. karau
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now