Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Win32 Creating a Popup Menu From Resource

Here is what it is doing

I am trying to load a popup "right click" menu, and use the resource file to define the menu items. The picture shows what is happening when I right click, it displays room for 2 items, which is correct, but doesnt show any text.

In the .cpp:

POINT pt;
pt.x = LOWORD (lParam);
pt.y = HIWORD (lParam);
ClientToScreen (hwnd, &pt);
HMENU hMenu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_POPUPMENU));
TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL);

and the resource:

IDR_POPUPMENU MENU DISCARDABLE 
BEGIN
MENUITEM "test",              IDM_TEST
MENUITEM "Close",               IDM_CLOSE
END

any idea on what I am donig wrong?

Thanks.

EDIT: I just tested, and clicking in the "no text displayed" areas, and it sends the correct message. What could be causing it to not display the text?

like image 233
Evan Carslake Avatar asked Sep 04 '13 00:09

Evan Carslake


People also ask

How do you create a pop up menu?

Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.

How do I create a Windows program menu?

To assign a menu to a window, use the SetMenu function or specify the menu's handle in the hMenu parameter of the CreateWindowEx function when creating a window.

What is Hmenu?

HMENU is a handle to a menu, e.g. as created by LoadMenu (which creates a menu from a specification in a resource). But, the CreateWindow function re-uses the same argument for two different purposes.

What display a pop up menu?

Pop-up menus appear over the application in a vertical orientation while the user is clicking an item, and then they disappear from the screen. You can programmatically invoke a pop-up menu, or you can arrange for a right-mouse click to automatically invoke a pop-up menu tied to a window or a control.


2 Answers

Found the solution:

HMENU hMenu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_POPUPMENU));
hMenu = GetSubMenu(hMenu, 0);

and resource:

IDR_POPUPMENU MENU DISCARDABLE 
BEGIN 
   POPUP "TEST" 
      BEGIN     
        MENUITEM "Test",                IDM_TEST
        MENUITEM "Close",               IDM_CLOSE
      END
END

Just had to start the resource entry with a beginning sub menu, TEST does not display, only its menu items do.

like image 148
Evan Carslake Avatar answered Sep 28 '22 07:09

Evan Carslake


Your menu resource is incorrect. It must be a popupmenu. eg:

IDR_MENU_TRAY MENU
BEGIN
    POPUP "ContextMenu"
    BEGIN
        MENUITEM "ShowWindow",                  ID_POPUP_SHOWWINDOW
        MENUITEM "Exit",                        ID_POPUP_EXIT
    END
END

TrackPopupMenu first parameter is a handle to a submenu associated with an existing menu item. You can see the examples here: http://msdn.microsoft.com/EN-US/library/ms647558(v=VS.85,d=hv.2).aspx

like image 35
HwangBae Avatar answered Sep 28 '22 05:09

HwangBae