【问题标题】:Best way to Enable/Disable CMenu items in real time实时启用/禁用菜单项的最佳方法
【发布时间】:2011-12-04 15:47:14
【问题描述】:

我正在使用 Visual C++ 6.0 开发一个项目,我需要能够根据分配给当前登录用户的权限启用或禁用某些菜单项。这是我正在使用的代码:

// If the currently logged in user doesn't have permission to edit invoices
if (!((CMyApp *)AfxGetApp())->UserHasPermission(PERMISSION_EditInvoice))
{
    // Disable the Edit Menu
    pMain->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
}
else
{
    // Enable the Edit Menu
    pMain->EnableMenuItem(1, MF_BYPOSITION | MF_ENABLED);
}

它完全符合我的要求,但我正在努力寻找放置它的最佳位置。如果我把它放在OnInitialUpdate() 中,我会得到我想要的结果,但仅限于打开的第一张发票。如果您打开第二张发票而不关闭并重新打开对话框,则不会再次执行代码。 OnUpdate() 在打开不同的发票时不会被调用,我发现唯一可以工作的地方是OnDraw()OnDraw() 的问题是菜单项在视觉上没有从灰色变为灰色启用,反之亦然,直到您尝试单击它。

【问题讨论】:

    标签: c++ visual-c++ mfc


    【解决方案1】:

    我认为您必须在过程中包含此代码

    void check_user_permission();

    当这个事件发生时你必须调用它:

    - OnInitialUpdate()
    - new user login (if your software permits user login/logout during the same session)
    - new invoice opened
    

    有帮助吗?

    【讨论】:

    • 有点。问题不在于每次更改发票时都没有调用它,而是我无法让 GUI 自行更新以显示菜单的新状态,而无需用户先尝试单击它。
    【解决方案2】:

    我最终决定禁用Edit Invoice 菜单项,而不是Edit 菜单本身。事实证明,这更简单、更简洁,因为它会在每次打开 'Edit 主菜单时确定权限并启用或禁用项目。

    void CViewInvoiceView::OnUpdateEditEditinvoice(CCmdUI* pCmdUI) 
    {
        // If the currently logged in user doesn't have permission to edit invoices
        if (!((CJ3App *)AfxGetApp())->UserHasPermission(PERMISSION_EditInvoice))
        {
            // Disable the Edit Menu
            pCmdUI->Enable(false);
        }
        else
        {
            // Enable the edit menu
            pCmdUI->Enable();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 2011-08-07
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多