Solved

Menu for SystemTray Icon

Posted on 1998-06-17
13
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 1

Expert Comment

by:TAMC
ID: 1318137
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
ID: 1318138
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
ID: 1318139
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:karau
ID: 1318140
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
ID: 1318141
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
ID: 1318142
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
 

Expert Comment

by:Samir040498
ID: 1318143
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
ID: 1318144
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
ID: 1318145
I am not able to reach your site.
0
 

Author Comment

by:karau
ID: 1318146
I have also posted them on a more reliable server:

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

Expert Comment

by:Priyesh
ID: 1318147
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
ID: 1318148
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
ID: 1318149
It worked great, Thanks

j. karau
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
no14 challenge 14 74
tripleUp challenge 7 93
building a service to accessed via telnet client, want colors ? 4 99
Excel file not created as expected 7 94
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: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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