我正在使用 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 的问题()
是菜单项在您尝试单击它之前不会从视觉上将状态从灰色更改为启用,反之亦然。
最佳答案
我认为你必须在过程中包含这段代码
void check_user_permission();
当这个事件发生时你必须调用它:
- OnInitialUpdate()
- new user login (if your software permits user login/logout during the same session)
- new invoice opened
有帮助吗?
关于c++ - 实时启用/禁用 CMenu 项目的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8376442/