Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 218
BArtWell Avatar asked Jul 24 '14 19:07

BArtWell


People also ask

How to change menu item text in Android studio?

This example demonstrates how do I change the text color of the menu item in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which method is used to change the name of a menu item?

Use MenuItem. setTitle(). If this isn't what you needed, you have to be more specific.

How do I change the menu color?

Open the colors. xml file by navigating to the app -> res -> values -> colors. xml. Create a color tag inside the resources tag with a name and set a color with its hex code.

How do you inflate a menu?

Stay organized with collections Save and categorize content based on your preferences. This class is used to instantiate menu XML files into Menu objects. For performance reasons, menu inflation relies heavily on pre-processing of XML files that is done at build time.


1 Answers

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; 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

like image 198
YePhIcK Avatar answered Sep 24 '22 00:09

YePhIcK