Changing text of a menu item

Posted on 2006-04-25
Last Modified: 2010-04-01
In design time, i've created a popup menu. This menu has several menu items on it.  One of these menu items (PopupA) is marked as "Popup" in it's property pages, so it spawns a submenu.  When PopupA is selected, a submenu is launched (which is also created at design time).  I need to dynamically change the text of one of these submenu items.  What I've done so far is add the following message map: ON_UPDATE_COMMAND_UI(ID_MY_MENU_ITEM, OnUpdateMyMenuItem).  In this function (OnUpdateMyMenuItem), I change the menu item text.  

PROBLEM IS, my function (OnUpdateMyMenuItem) never gets called.  I've put a stop in there and ran in debug mode, and this function never gets called.  Any thoughts?


FYI, the window in which I launch this menu is an openGL window.
Question by:clintMon
    LVL 39

    Expert Comment

    >>>> I've created a popup menu

    What do you call a popup menu? Did you associate the menu to a CFrameWnd? Or how do you invoke the menu?

    >>>> What I've done so far is add the following message map

    Message maps only work within the MFC framework. If you have an active view class derived from CView notification messages like the ON_UPDATE_COMMAND_UI notification were sent for the active menu and it's menu items only. The active menu is determined by the current dialog or view or the current frame window associated to the view. Also the document and application class in a SDI or MDI architecure can be targets of menu messages.

    I don't know whether it is possible (or senseful) to adopt the ON_UPDATE_COMMAND_UI mechanism of the MFC framework to parts that were outside of the framework, e. g. the OpenGL window you were talking of. If you are creating a CMenu object from a resource, it might be easier to change the menu item names at creation time by calling ModifyMenu. Or you might call ModifyMenu in an event handler of your dialog (or view) class, where the menu should be stored as a member.

    Regards, Alex


    Author Comment

    I have the following message back declared in my openGL window's header file:

    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);

    So when I right click in my openGL window, the OnContextMenu function gets called, and my design time created popup menu, which we'll call ID_DESIGNTIME_POPUP_MENU, displays in the window.

    LVL 39

    Expert Comment

    >>>> and my design time created popup menu, which we'll call ID_DESIGNTIME_POPUP_MENU, displays in the window.

    Could you post the code that creates (invokes) the menu? Obviously the OnContextMenu function is just a handler of the right-click event but the menu functionality needs to be implemented seperately.

    Regards, Alex

    Author Comment

    In OnContextMenu(...), I've got the following code to create the popup menu:

    CMenu* pMenu = new CMenu;
    CMenu* pPopUpMenu = pMenu->GetSubMenu(0);

    ID_DESIGNTIME_POPUP_MENU has been created in design time, using the resource wizard.
    LVL 39

    Accepted Solution

    Why didn't you change the labels by calling

          pMenu->ModifyMenu(item, ..);

    If you create a menu in a handler function there is no way that UPDATE_UI notifications could be sent to your window class. There isn't any connection between both objects. The MFC framework works differently. When creating a frame window via dialog templates, the menu id was passed at creation time. Then, notifications can be setup and were periodically processed by the message loop at idle times.

    Regards, Alex

    Author Comment

    THANK YOU.  Yep, didn't even think about pMenu->ModifyMenu(item, ..);  Hadn't actually used that function before.  So, for anyone who wants to know, to simply change text menu item from a menu created in the resource editor, just do the following:

    OnContextMenu(CWnd* pWnd, CPoint point)

       CMenu* pMenu = new CMenu;
       pMenu->LoadMenu (ID_MY_POPUP_MENU); //associate a pointer to the design-time created menu
       CMenu* pSubMenu = pMenu->GetSubMenu(0); //point to the submenu

      //change the text of the menu item
       pSubMenu->ModifyMenu(ID_OF_MENU_ITEM, MF_STRING, ID_OF_MENU_ITEM, "Hi Mom");



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now