How Taskbar Right Click Popup Context Menu Win32 shell_NotifyIcon (not MFC, not VB)

Posted on 2003-03-08
Medium Priority
Last Modified: 2012-08-13
500 pts for some good, simple, comprehensive sample code demonstrating how to get a context menu (popup menu) to appear by right clicking a taskbar icon using Visual C++ 6.0 (not MFC!).  I would also appreciate it if you don't link me to a url that will expire.. I've been frustrated to see the answer to this very question posted in another forum as a link to a url whose domain has expired... ergo, a tease... Grrrrr.

What I have so far:

I can get the icon in the task bar, and I can respond to mouse events (i.e., I can do a MessageBox) just fine, but my attempts to get the popup menu to appear anywhere outside of the application window has failed.  Seems the x/y coordinates I am sending are inside of the application workspace and not the desktop.

I'm tired of scraping the internet for a sample so if someone would be so kind as to help this newbie out, it's worth 500 pts.  I'd throw in box of chocolates too, but I'm broke ;)

Question by:ChrisKersey
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

Accepted Solution

teleplayr earned 1500 total points
ID: 8096310
OK, here's the code from a tray program I wrote.  You just create the menu when you receive the WM_RBUTTONDOWN message from your tray icon:

   if (lParam == WM_RBUTTONDOWN)
      // Load up the popup menu resource
      hMenu = LoadMenu(hInst, IDM_POPUP);
      // Get the popup we're interested in
      hMenuPopup = GetSubMenu(hMenu, 0);
      POINT p;
      // Find out where the user clicked

      // A little Windows quirk.  You need to do this
      // so the menu disappears if the user clicks
      // off it

      // Now popup the window at the cursor
      TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_BOTTOMALIGN, p.x, p.y, 0, hDlg, 0);
      // Destroy the first handle since we're not using it

LVL 49

Expert Comment

ID: 8097093
teleplayr's code looks great -- right on the money.  I'd also add a double-click handler that will execute the command on the top of the menu.  In MFC, it is like this:

else if ( nMsg == WM_LBUTTONDBLCLK ) {
     SendMessage( WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);

In general, a double-click of a 'tray icon' is a request to dispay the U/I for the process (i.e., set options and whatnot).

-- Dan

Author Comment

ID: 8101311
This did the trick, thanks alot.  I might add that what is not shown in the above code (which is one of the reasons my routine wasn't working I found out) is that the NOTIFYICON structure that gets passed into the Shell_NotifyIcon(NIM_ADD,pnid); to display the taskbar icon didn't have the PNOTIFYICON->uCallbackMessage member set.  In the code supplied here, PNOTIFYICON->uCallBackMessage would be assigned MYWM_NOTIFYICON.  Also, I didn't have hDlg defined anywhere so I set it to hwnd (first param to WinProc) and this code worked fine.

Thank you!  Don't spend your 500 points all in one place...

Featured Post

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.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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