Disable some context menu items if items are unchecked

I have a tree view ( CTreeView

) that will show me a popup menu after I right click on it. My context menu only has 3 items (like A, B, C) to select, and my tree view displays a long list of ordered products intended for checkboxes. I would like to disable menu items A and B if no ordered products have been ordered, and enable them if available.

I create CFoodView::OnUpdateItemA(CCmdUI* pCmdUI)

//CFoodView inherits CTreeView

and CFoodView::OnUpdateItemB(CCmdUI* pCmdUI)

to handle my states like this:

CFoodView::OnUpdateItemB(CCmdUI* pCmdUI)
{
    if TreeView has no items
    {
        pCmdUI->Enable(FALSE);
    }
    else
    {
        *Search* the tree to get selected items
        if None is checked
        {
            pCmdUI->Enable(FALSE);
        }
        else there are checked items
            pCmdUI->Enable(TRUE);
    }
}

      

The method is CFoodView::OnUpdateItemA(CCmdUI* pCmdUI)

the same.

I think this is not the correct way to handle this GUI feature.

+3


source to share


1 answer


Well, you haven't provided all the important information. How did you create the menu item handlers? Assuming you are inserting the handlers correctly, you still haven't provided any information about how you invoke the popup menu. If everything you did was done correctly, this is the correct way to handle the update menu. The most common mistake is defining the view itself as a window that handles pop-up updates and commands. To use the MFC menu update mechanism, you must pass the pointer to the main window, not to the tree view:

    CWnd *pMainWnd = AfxGetMainWnd();
    ASSERT(pMainWnd != nullptr);

    pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, pMainWnd);

      



If that doesn't work, try again to create the handler and / or the location where you call the TrackPopupMenu function.

+1


source







All Articles