简体   繁体   English

如果未选中任何项,则禁用某些上下文菜单项

[英]Disable some context menu items if no items are checked

I have a tree view ( CTreeView ) that will show me a pop-up menu after I right-click my mouse on it. 我有一个树形视图( CTreeView ),在我右键单击鼠标后会显示一个弹出菜单。 In my context menu there are only 3 items (ie A, B, C) for selection and my tree view displays a long list of ordered foods designed with check-boxes. 在我的上下文菜单中,只有3个项目(即A,B,C)可供选择,我的树状视图显示了一系列带有复选框的订购食品。 I would like to disable menu items A and B if no ordered foods are checked and enable them when any is. 如果没有检查订购的食物,我想禁用菜单项A和B,并在任何时候启用它们。

I create CFoodView::OnUpdateItemA(CCmdUI* pCmdUI) //CFoodView inherits CTreeView and CFoodView::OnUpdateItemB(CCmdUI* pCmdUI) to handle their states like so 我创建CFoodView::OnUpdateItemA(CCmdUI* pCmdUI) //CFoodView inherits CTreeViewCFoodView::OnUpdateItemB(CCmdUI* pCmdUI)来处理它们的状态,如此

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

Method CFoodView::OnUpdateItemA(CCmdUI* pCmdUI) is the same. 方法CFoodView::OnUpdateItemA(CCmdUI* pCmdUI)是相同的。

I think this isn't a correct way to handle this GUI feature. 我认为这不是处理此GUI功能的正确方法。

Well, you did not submit all important information. 好吧,你没有提交所有重要信息。 How did you create menu item handlers? 你是如何创建菜单项处理程序的? Assuming you insert handlers the proper way, still did not provide any information how you are invoking popup menu. 假设您以正确的方式插入处理程序,仍未提供有关如何调用弹出菜单的任何信息。 If all you did was properly done it is the proper way of handling update menu. 如果您所做的一切都已正确完成,那么这是处理更新菜单的正确方法。 The most common mistake is designating view itself as the window that handles popup updates and commands. 最常见的错误是将视图本身指定为处理弹出更新和命令的窗口。 In order to use MFC menu update mechanism, you have to pass a pointer to the main window not to the tree view: 为了使用MFC菜单更新机制,您必须将指向主窗口的指针传递给树视图:

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

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

If this will not work reexamine the way you create handler and/or the place you invoke the TrackPopupMenu function. 如果这不起作用,请重新检查创建处理程序的方式和/或调用TrackPopupMenu函数的位置。

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

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