CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    TrackPopupMenu and default taskbar menu

    Hi everyone:

    I'm not sure why this happens, so maybe someone can clear this up for me.

    Say, I want to display a pop-up menu from my program's tray icon on the task bar. I do the following:
    Code:
    		int nRes = TrackPopupMenu(hMenu, TPM_RIGHTALIGN | TPM_TOPALIGN |
    			TPM_LEFTBUTTON | TPM_VERPOSANIMATION | TPM_HORNEGANIMATION | TPM_RETURNCMD,
    			pnt.x, pnt.y, 0, hMainWnd, NULL);
    Because of some bug in the Explorer shell the menu isn't dismissed when someone clicks outside of it, so I had to add the following code to accommodate for that (which can be found throughout Win32 forums online):
    Code:
    		::SetForegroundWindow(hMainWnd);
    
    		int nRes = TrackPopupMenu(hMenu, TPM_RIGHTALIGN | TPM_TOPALIGN |
    			TPM_LEFTBUTTON | TPM_VERPOSANIMATION | TPM_HORNEGANIMATION | TPM_RETURNCMD,
    			pnt.x, pnt.y, 0, hMainWnd, NULL);
    
    		PostMessage(WM_NULL, 0, 0);
    That takes away that bug, but what happens is that at times when my tray icon is first displayed on the task bar's tray and I try to right-click it, I get my pop-up menu and also get the default task bar menu (picture below) displayed over it, as if I right-clicked the task bar itself. This happens only the first time I right-click my tray icon.

    Can someone tell me why this happens and how to remedy that?


    PS. I'm running these tests on a Windows 7 with a default theme (and glass effect enabled).
    Attached Images Attached Images  

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: TrackPopupMenu and default taskbar menu

    You need to show the code that creates the taskbar icon. Are you intercepting WM_RBUTTONDOWN or WM_CONTEXTMENU to display the popup menu?

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: TrackPopupMenu and default taskbar menu

    I process both like this:
    Code:
    		case WM_RBUTTONDOWN:
    		case WM_CONTEXTMENU:
    			{
    				ShowTrayPopupMenu();
    			}
    			break;
    Is it supposed to be done differently?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: TrackPopupMenu and default taskbar menu

    Remove the redundant WM_RBUTTONDOWN case.

    Code:
    // case WM_RBUTTONDOWN:
    case WM_CONTEXTMENU:
        {
          ShowTrayPopupMenu();
        }
        break;

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: TrackPopupMenu and default taskbar menu

    As a general rule, never display a popup menu with WM_RBUTTONDOWN. Instead, use WM_CONTEXTMENU.

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: TrackPopupMenu and default taskbar menu

    For some reason if I remove WM_RBUTTONDOWN it's not called at all.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: TrackPopupMenu and default taskbar menu

    My bad. For task bar icon popups, use WM_RBUTTONDOWN and remove WM_CONTEXTMENU.

  8. #8
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: TrackPopupMenu and default taskbar menu

    Thanks, but I still get the same bug of two overlapping menus. I played a little with it, and to reproduce this bug you can add Sleep(500); after SetForegroundWindow call to make it come up for sure. I also believe that this only happens when some other window has keyboard focus (like any other Explorer window.)

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: TrackPopupMenu and default taskbar menu

    Can someone tell me why this happens and how to remedy that?
    It seems the effect has nothing to do with your code. I had the effect of the kind in regular Explorer window while right clicking an element without selecting it previously. So it's rather Win7 Explorer behavior.
    Last edited by Igor Vartanov; September 29th, 2011 at 02:56 AM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured