Solved

Menu for SystemTray Icon

Posted on 1998-06-17
13
290 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

14 Experts available now in Live!

Get 1:1 Help Now