C ++ win32 dynamic menu - menu item selected

I have a win32 application (C ++) in which the context menu binds to the right button of the notification icon. Menu / submenu items are dynamically created and changed at runtime.

 InsertMenu(hSettings, 0, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR) hDevices, L"Setting 1");
 InsertMenu(hSettings, 1, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR) hChannels, L"Setting 2");

 InsertMenu(hMainMenu, 0, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR) hSettings, L"Settings");
 InsertMenu(hMainMenu, 1, MF_BYPOSITION | MF_STRING, IDM_EXIT, L"Exit"); 

In the code above, hDevices and hChannels are dynamically generated submenus. Dynamic menus are created as follows:

   InsertMenu(hDevices, i, style, IDM_DEVICE, L"Test 1");
   InsertMenu(hDevices, i, style, IDM_DEVICE, L"Test 2");
   InsertMenu(hDevices, i, style, IDM_DEVICE, L"Test 3");

Is there a way to find out which item was pressed without giving each item a submenu its own identifier (IDM_DEVICE in the code above)? In would like to find that the user clicked on the IDM_DEVICE submenu and that he clicked on the first item (test 1) in this submenu.

I would like to get something like this:

  case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
        case IDM_DEVICE: // user clicked on Test 1 or Test 2 or Test 3 
            UINT index = getClickedMenuItem(); // get the index number of the clicked item (if you clicked on Test 1 it would be 0,..) 
                            // change the style of the menu item with that index 
            break;          
    }
+5
2

:

MENUINFO mi;
memset(&mi, 0, sizeof(mi));
mi.cbSize = sizeof(mi);
mi.fMask = MIM_STYLE;
mi.dwStyle = MNS_NOTIFYBYPOS;
SetMenuInfo(hDevices, &mi);

WM_MENUCOMMAND WM_COMMAND. wParam lParam. , , - DefWindowProc. :

case WM_MENUCOMMAND:
    HMENU menu = (HMENU)lParam;
    int idx = wParam;
    if (menu == hDevices)
    {
       //Do useful things with device #idx
    }
    else
        break; //Ensure that after that there is a DefWindowProc call
+6

TrackPopupMenuEx() TPM_RETURNCMD | TPM_NONOTIFY id WM_MENUCOMMAND.

0

All Articles