The best way to enable / disable CMenu elements in real time

I am working on a project using Visual C ++ 6.0, and I need to be able to enable or disable certain menu items depending on the permissions assigned to the current user. This is the code I'm using:

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

He is doing exactly what I want to do, but I am trying to find a better place to create it. If I put it in OnInitialUpdate() , I get the results that I want, but only for opening the first account. If you open a second invoice without closing and reopening the dialog, the code will not be executed again. OnUpdate() not called when another invoice is opened, and the only other place I found is OnDraw() working, the problem with OnDraw() is that the menu item does not visually change the state from displayed to gray. Enabled or vice versa, until you try to click it.

+4
source share
2 answers

In the end, I decided to disable the Edit Invoice menu item instead of the Edit menu itself. This turned out to be much simpler and cleaner, as it determines the resolution and enables or disables the item each time the 'Edit main menu opens.

 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(); } } 
0
source

I think you should include this code in the procedure.

void check_user_permission ();

what you should call him when these events occur:

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

Can this help?

0
source

Source: https://habr.com/ru/post/1384587/


All Articles