1

I currently am attempting to do something simple:

CMenu menu;
menu.LoadMenu(IDR_MENU_IMAGE);
CPoint pt;
GetCursorPos(&pt); 
menu.TrackPopupMenu(TPM_RIGHTBUTTON, pt.x, pt.y, this);

When I right-click, I get the below image. However, the menu is loaded; as I move my cursor down the menu, you can see it populate sub-menus just fine. It does this with any menu I load dynamically like this. I attempted to create a pointer (CMenu*) and still continue having this problem.

The points don't matter (I input arbitrary points).

The "this" in question is a derived CView* class. I am clicking on a HWND object but I tried to also take the CWnd::FromHandle() of this particular object I am clicking on and having the object handle it, but I still have the same problem.

My top menu structure and all other menus work - it is only in this particular case.

I don't really want to derive a C++ CMenu class just to override the MeasureItem function when the original menus should actually be working, and do work fine in other versions..

Help?

enter image description here

RootAtShell
  • 115
  • 1
  • 7
  • The image you posted doesn't really match the content of your question. – Jabberwocky Mar 16 '16 at 07:57
  • Unsure what you mean besides me failing to mention an almost zero-width menu. However, I figured out the problem was simply that I forgot to include a no-name option as the very first menu as per https://msdn.microsoft.com/en-us/library/k5az22fk.aspx . – RootAtShell Mar 16 '16 at 16:01
  • I mean the image contains mainly junk and maybe 1/3 of a menu. – Jabberwocky Mar 16 '16 at 16:02
  • The junk in the background, sure, but the silver bar on the left is actually the menu that I'm complaining about and I included the menu on the right that is actually from the left zero-width menu to show that it is actually there, actually populating submenus, etc. – RootAtShell Mar 17 '16 at 15:56
  • OK; Now I understand, but you must admit that someone who is not aware of your problem seeing this won't understand. – Jabberwocky Mar 17 '16 at 15:57
  • 1
    Yeah, 100% agree with you. It was the only way I could think of showing this issue but it wasn't anything close to good. I should've probably annotated it. – RootAtShell Mar 17 '16 at 16:00

1 Answers1

2

Use GetSubMenu(0) to obtain a popup handle:

CMenu menu;
menu.LoadMenu(IDR_MENU_IMAGE);
CMenu *submenu = menu.GetSubMenu(0);
if (submenu)
    submenu->TrackPopupMenu(TPM_RIGHTBUTTON, pt.x, pt.y, this);

Where IDR_MENU_IMAGE is created in resource editor similar to the following:

IDR_MENU_IMAGE MENU
BEGIN
    POPUP "File"
    BEGIN
        MENUITEM "New",                         ID_FILE_NEW
        MENUITEM "Open",                        ID_FILE_OPEN
        MENUITEM "Save",                        ID_FILE_SAVE
        MENUITEM "Save As ...",                 ID_FILE_SAVEAS
    END
END

Result:

enter image description here

Note, this won't work if there is only a "menu bar", and no popup. The menu below cannot be created as popup:

IDR_MENU_IMAGE MENU //no popup menu!
BEGIN
    MENUITEM "A", IDA
    MENUITEM "B", IDB
    MENUITEM "C", IDC
END


You can also create the popup menu as follows:
CMenu menu;
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING, ID_FILE_NEW, "New");
menu.TrackPopupMenu(TPM_RIGHTBUTTON, p.x, p.y, this);
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Actually, after doing this, I realized the actual issue wasn't anything related to handles, etc. - it was merely that I needed to have a no-name option in the menu as per https://msdn.microsoft.com/en-us/library/k5az22fk.aspx to be a pop-up. Doing this, I was able to fix the issue. – RootAtShell Mar 16 '16 at 16:00