Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC, Cannot disable Menu option from Child View

MFC, Cannot disable Menu option from Child View

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11169029
    wrote on last edited by
    #1

    I have added a menu with submenus to my MFC frame program. I also have a child view window instantiated within a main frame. In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar. However, when i want to disable some submenu I cannot do it. I already have the following functions:

    bool CMViewOption1Enabled;
    .
    .
    .
    .
    .

    ON\_COMMAND(CM\_VIEW\_OPTION1, CmViewOption1)
    ON\_COMMAND(CM\_VIEW\_OPTION2, CmViewOption2)
    ON\_UPDATE\_COMMAND\_UI(CM\_VIEW\_OPTION1, CmEnableViewOption1)
    

    .
    .
    .
    .
    .
    .
    .
    /***************************************************************/
    /* */
    /* ViewOption1: */
    /* */
    /***************************************************************/

    void ChildView::CmViewOption1()
    {
    MessageBox("View Option1", "MENU", MB_OK | MB_ICONEXCLAMATION);
    CMViewOption1Enabled = false;

    }

    /***************************************************************/
    /* */
    /* CmEnableViewOption1: */
    /* */
    /***************************************************************/

    void ChildView::CmEnableViewOption1(CCmdUI *ptrenabler)
    {
    ptrenabler->Enable(CMViewOption1Enabled);
    //ptrenabler->DoUpdate(this, TRUE);

    }

    /***************************************************************/
    /* */
    /* ViewOption2: */
    /* */
    /***************************************************************/

    void ChildView::CmViewOption2()
    {
    MessageBox("View Option2", "MENU", MB_OK | MB_ICONEXCLAMATION);
    }

    So, i right click inside my child view i see Option1, Option2, and when i click on either i can see message window popping up, meaning that they work. However, if you noticed, when i click Option 1, it must disable this same option in menu (it must be grayed out) but that does not happen. What is the proper way to get it to work? to give additonal details, when i right click and select Option 1, i do see message about it, and as i mentioned above I do not see it grayed out, however when i click it again, no message appears, which means (i assume) that menu is already disabled, but not grayed out..so how to permanently disable/gray i

    L J 3 Replies Last reply
    0
    • U User 11169029

      I have added a menu with submenus to my MFC frame program. I also have a child view window instantiated within a main frame. In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar. However, when i want to disable some submenu I cannot do it. I already have the following functions:

      bool CMViewOption1Enabled;
      .
      .
      .
      .
      .

      ON\_COMMAND(CM\_VIEW\_OPTION1, CmViewOption1)
      ON\_COMMAND(CM\_VIEW\_OPTION2, CmViewOption2)
      ON\_UPDATE\_COMMAND\_UI(CM\_VIEW\_OPTION1, CmEnableViewOption1)
      

      .
      .
      .
      .
      .
      .
      .
      /***************************************************************/
      /* */
      /* ViewOption1: */
      /* */
      /***************************************************************/

      void ChildView::CmViewOption1()
      {
      MessageBox("View Option1", "MENU", MB_OK | MB_ICONEXCLAMATION);
      CMViewOption1Enabled = false;

      }

      /***************************************************************/
      /* */
      /* CmEnableViewOption1: */
      /* */
      /***************************************************************/

      void ChildView::CmEnableViewOption1(CCmdUI *ptrenabler)
      {
      ptrenabler->Enable(CMViewOption1Enabled);
      //ptrenabler->DoUpdate(this, TRUE);

      }

      /***************************************************************/
      /* */
      /* ViewOption2: */
      /* */
      /***************************************************************/

      void ChildView::CmViewOption2()
      {
      MessageBox("View Option2", "MENU", MB_OK | MB_ICONEXCLAMATION);
      }

      So, i right click inside my child view i see Option1, Option2, and when i click on either i can see message window popping up, meaning that they work. However, if you noticed, when i click Option 1, it must disable this same option in menu (it must be grayed out) but that does not happen. What is the proper way to get it to work? to give additonal details, when i right click and select Option 1, i do see message about it, and as i mentioned above I do not see it grayed out, however when i click it again, no message appears, which means (i assume) that menu is already disabled, but not grayed out..so how to permanently disable/gray i

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You have spelt the names of your variables differently:

      bool CMViewOption1Enabled;

      // ...

      ON_UPDATE_COMMAND_UI(CM_VIEW_OPTION1, CmEnableViewOption1)

      1 Reply Last reply
      0
      • U User 11169029

        I have added a menu with submenus to my MFC frame program. I also have a child view window instantiated within a main frame. In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar. However, when i want to disable some submenu I cannot do it. I already have the following functions:

        bool CMViewOption1Enabled;
        .
        .
        .
        .
        .

        ON\_COMMAND(CM\_VIEW\_OPTION1, CmViewOption1)
        ON\_COMMAND(CM\_VIEW\_OPTION2, CmViewOption2)
        ON\_UPDATE\_COMMAND\_UI(CM\_VIEW\_OPTION1, CmEnableViewOption1)
        

        .
        .
        .
        .
        .
        .
        .
        /***************************************************************/
        /* */
        /* ViewOption1: */
        /* */
        /***************************************************************/

        void ChildView::CmViewOption1()
        {
        MessageBox("View Option1", "MENU", MB_OK | MB_ICONEXCLAMATION);
        CMViewOption1Enabled = false;

        }

        /***************************************************************/
        /* */
        /* CmEnableViewOption1: */
        /* */
        /***************************************************************/

        void ChildView::CmEnableViewOption1(CCmdUI *ptrenabler)
        {
        ptrenabler->Enable(CMViewOption1Enabled);
        //ptrenabler->DoUpdate(this, TRUE);

        }

        /***************************************************************/
        /* */
        /* ViewOption2: */
        /* */
        /***************************************************************/

        void ChildView::CmViewOption2()
        {
        MessageBox("View Option2", "MENU", MB_OK | MB_ICONEXCLAMATION);
        }

        So, i right click inside my child view i see Option1, Option2, and when i click on either i can see message window popping up, meaning that they work. However, if you noticed, when i click Option 1, it must disable this same option in menu (it must be grayed out) but that does not happen. What is the proper way to get it to work? to give additonal details, when i right click and select Option 1, i do see message about it, and as i mentioned above I do not see it grayed out, however when i click it again, no message appears, which means (i assume) that menu is already disabled, but not grayed out..so how to permanently disable/gray i

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You should use your debugger to trap when the call to CmEnableViewOption1 occurs. Or move the message box there; that is the code you need to validate.

        U 1 Reply Last reply
        0
        • L Lost User

          You should use your debugger to trap when the call to CmEnableViewOption1 occurs. Or move the message box there; that is the code you need to validate.

          U Offline
          U Offline
          User 11169029
          wrote on last edited by
          #4

          ok so here is a thing, the CmEnableViewOption1 occurs only when i do View-> from top bar menu, there i see Option 1 and Option2 popping down. However, when i mouse right click from inside ChildView window i do see |Option1,2 menu popping up but inside debugger it does not enter the CmEnableViewOption1, meaning that this function does not run. In that case my question would be, why when i click directly inside top frame menu that function is called, but when i right click from child view window and properly getting that menu and can even run its commands, but CmEnableViewOption1 is not executed?

          L 1 Reply Last reply
          0
          • U User 11169029

            ok so here is a thing, the CmEnableViewOption1 occurs only when i do View-> from top bar menu, there i see Option 1 and Option2 popping down. However, when i mouse right click from inside ChildView window i do see |Option1,2 menu popping up but inside debugger it does not enter the CmEnableViewOption1, meaning that this function does not run. In that case my question would be, why when i click directly inside top frame menu that function is called, but when i right click from child view window and properly getting that menu and can even run its commands, but CmEnableViewOption1 is not executed?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Have you checked that your ON_UPDATE_COMMAND_UI in the FrameWnd class gets called? It's a long time since I used MFC but I do not recall ever seeing this problem. Take a look at the area where you create and display the popup menu.

            1 Reply Last reply
            0
            • U User 11169029

              I have added a menu with submenus to my MFC frame program. I also have a child view window instantiated within a main frame. In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar. However, when i want to disable some submenu I cannot do it. I already have the following functions:

              bool CMViewOption1Enabled;
              .
              .
              .
              .
              .

              ON\_COMMAND(CM\_VIEW\_OPTION1, CmViewOption1)
              ON\_COMMAND(CM\_VIEW\_OPTION2, CmViewOption2)
              ON\_UPDATE\_COMMAND\_UI(CM\_VIEW\_OPTION1, CmEnableViewOption1)
              

              .
              .
              .
              .
              .
              .
              .
              /***************************************************************/
              /* */
              /* ViewOption1: */
              /* */
              /***************************************************************/

              void ChildView::CmViewOption1()
              {
              MessageBox("View Option1", "MENU", MB_OK | MB_ICONEXCLAMATION);
              CMViewOption1Enabled = false;

              }

              /***************************************************************/
              /* */
              /* CmEnableViewOption1: */
              /* */
              /***************************************************************/

              void ChildView::CmEnableViewOption1(CCmdUI *ptrenabler)
              {
              ptrenabler->Enable(CMViewOption1Enabled);
              //ptrenabler->DoUpdate(this, TRUE);

              }

              /***************************************************************/
              /* */
              /* ViewOption2: */
              /* */
              /***************************************************************/

              void ChildView::CmViewOption2()
              {
              MessageBox("View Option2", "MENU", MB_OK | MB_ICONEXCLAMATION);
              }

              So, i right click inside my child view i see Option1, Option2, and when i click on either i can see message window popping up, meaning that they work. However, if you noticed, when i click Option 1, it must disable this same option in menu (it must be grayed out) but that does not happen. What is the proper way to get it to work? to give additonal details, when i right click and select Option 1, i do see message about it, and as i mentioned above I do not see it grayed out, however when i click it again, no message appears, which means (i assume) that menu is already disabled, but not grayed out..so how to permanently disable/gray i

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              Member 11203277 wrote:

              In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar.

              So you are using a popup (context) menu here and not the menu of the main frame (only the menu items / resources are re-used). Then you should handle enabling/disabling when opening the popup menu and pass AfxGetMainWnd() as the owner window (see code comments):

              CMenu *pMenu = new CMenu;
              pMenu->LoadMenu(IDR_OF_MENU);
              // Get the pop-up menu.
              CMenu *pPopupMenu = pMenu->GetSubMenu(0);
              // Enable disable an item by ID.
              pPopupMenu->EnableMenuItem(CM_VIEW_OPTION1, CMViewOption1Enabled ? 0 : MF_GRAYED);
              // Show context menu and wait for selection.
              // Advantages of passing AfxGetMainWnd():
              // - Calls provided OnUpdate() functions to determine state of items.
              // NOTE: Items can be only disabled using the OnUpdate handlers!
              // So there must be handlers for those items that may be disabled!
              // If no update handlers are present, items are enabled by default
              // (by existance of command handlers).
              // - Shows short info about items in status bar
              // - Processes F1 help
              pPopupMenu->TrackPopupMenuEx(flags, x, y, AfxGetMainWnd(), NULL);
              delete pMenu;

              A dynamic update is not necessary here when the popup menu closes with the first click. Otherwise make pPopupMenu a member of your child view so that it can be used to change items and/or their state dynamically.

              U 1 Reply Last reply
              0
              • J Jochen Arndt

                Member 11203277 wrote:

                In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar.

                So you are using a popup (context) menu here and not the menu of the main frame (only the menu items / resources are re-used). Then you should handle enabling/disabling when opening the popup menu and pass AfxGetMainWnd() as the owner window (see code comments):

                CMenu *pMenu = new CMenu;
                pMenu->LoadMenu(IDR_OF_MENU);
                // Get the pop-up menu.
                CMenu *pPopupMenu = pMenu->GetSubMenu(0);
                // Enable disable an item by ID.
                pPopupMenu->EnableMenuItem(CM_VIEW_OPTION1, CMViewOption1Enabled ? 0 : MF_GRAYED);
                // Show context menu and wait for selection.
                // Advantages of passing AfxGetMainWnd():
                // - Calls provided OnUpdate() functions to determine state of items.
                // NOTE: Items can be only disabled using the OnUpdate handlers!
                // So there must be handlers for those items that may be disabled!
                // If no update handlers are present, items are enabled by default
                // (by existance of command handlers).
                // - Shows short info about items in status bar
                // - Processes F1 help
                pPopupMenu->TrackPopupMenuEx(flags, x, y, AfxGetMainWnd(), NULL);
                delete pMenu;

                A dynamic update is not necessary here when the popup menu closes with the first click. Otherwise make pPopupMenu a member of your child view so that it can be used to change items and/or their state dynamically.

                U Offline
                U Offline
                User 11169029
                wrote on last edited by
                #7

                Perfect! I did almost exactly what you suggested with slight difference;

                void ChildView::OnRButtonDown(UINT, CPoint point) {

                CWnd\* pMain = AfxGetMainWnd(); // get to main window
                
                CMenu \*ptrmenu = pMain->GetMenu();             // point to main menu
                CMenu \*ptrpopup = ptrmenu->GetSubMenu(1);// point to edit popup
                ClientToScreen(&point);                  // convert coordinates
                
                ptrpopup->EnableMenuItem(CM\_VIEW\_OPTION1, CMViewOption1Enabled ? 0 : MF\_GRAYED);
                
                ptrpopup->TrackPopupMenu(0, point.x, point.y, AfxGetMainWnd(), 0);
                

                }

                Now it works great! thanks

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups