简体   繁体   English

如何更改菜单项文本?

[英]How to change menu item text?

I need to change menu item text on runtime. 我需要在运行时更改菜单项文本。 I've try to use GetMenuItemInfo() and SetMenuItemInfo(): 我尝试使用GetMenuItemInfo()和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: 正如@HansPassant指出的解决方案是:

You are not using MENUITEMDATA correctly, you forgot to set the fMask member. 您没有正确使用MENUITEMDATA,却忘记了设置fMask成员。 Read the MSDN article for the struct for details 阅读有关该结构的MSDN文章以了解详细信息

and then: 接着:

add menuitem.fMask = MIIM_TYPE | MIIM_DATA; 添加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 我无法赞扬此解决方案,但在此处提供了该解决方案,以便需要该问题答案的下一个人可以轻松找到它而无需解析评论部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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