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

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

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 ;)

1 Solution
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

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
ChrisKerseyAuthor Commented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now