简体   繁体   中英

How to change menu item text?

I need to change menu item text on runtime. I've try to use GetMenuItemInfo() and SetMenuItemInfo():

case WM_NOTIFYICONMSG:
    switch (lParam)  {
    case WM_LBUTTONDBLCLK:
        someAction();
        break;
    case WM_RBUTTONDOWN:
    {
        POINT point;
        GetCursorPos(&point);

        HMENU hMenu;
        HMENU hMenuTrackPopup;

        hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU));
        if (hMenu) {
            MENUITEMINFOA menuitem = { sizeof(MENUITEMINFOA) };
            GetMenuItemInfoA(hMenu, IDM_EXIT, false, &menuitem);
            menuitem.dwTypeData = "New text here";
            SetMenuItemInfoA(hMenu, IDM_EXIT, false, &menuitem);
            hMenuTrackPopup = GetSubMenu(hMenu, 0);
            TrackPopupMenu(hMenuTrackPopup, 0, point.x, point.y, 0, hWnd, NULL);
            DestroyMenu(hMenu);
        }
    }
        break;
    default:
        break;
    }
    break;

But it doesn't work, text doesn't changed. What I am doing wrong? How to implement it?

As @HansPassant pointed out the solution is:

You are not using MENUITEMDATA correctly, you forgot to set the fMask member. Read the MSDN article for the struct for details

and then:

add menuitem.fMask = MIIM_TYPE | MIIM_DATA; menuitem.fMask = MIIM_TYPE | MIIM_DATA; and it works well

I can't take credit for this solution but am providing it here so that the next person that needs an answer to that question can easily find it without parsing the comments' section

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM