2

I am trying to disable main menu items. in MDI application, this works:

CWnd *pW=AfxGetMainWnd();
CMenu * pMenu=pW->GetMenu();
pMenu->EnableMenuItem(5, MF_BYPOSITION | MF_GRAYED | MF_DISABLED);  

Not in SDI. Most likely, I am putting it into the wrong place. CMainframe? The view? Which speific subroutine? I tried the constructors, but no change in UI.

Any help is appreciated, I am banging my head and searched numerous web places (and here) but didn't find the right direction.

many thanks

Vinícius
  • 15,498
  • 3
  • 29
  • 53
opto
  • 57
  • 6

2 Answers2

1

You don't want to directly enable/disable menu items under MFC, whether it's SDI or MDI.

Instead, when you add the item to the menu, you add two event handlers for it. One will be for "COMMAND", the other for "UPDATE_COMMAND_UI".

enter image description here

the COMMAND handler actually carries out the command for that menu entry.

The UPDATE_COMMAND_UI handler (indirectly) enables/disables the menu entry by returning true/false to indicate whether it should be enabled.

As to why this is preferable: first and foremost, because you can have (for example) both a menu entry and a toolbar that invoke the same action. This automatically enables/disables both as appropriate.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

you can do that by using class wizard to add function that handles Enabling/disabling menu items via UPDATE_COMMAND_UI:

void CMyAdoMfcView::OnUpdateAddnew(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->Enable(true);
}

in my code I enabled the menu item AddNew.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • I think both answers apply to when I want to disable a subentry. If I want to disable files->New, then I use UPDATE_COMMAND_UI. But I want to disable the full files menu item. I think I got my solution from Paetzold's book. Only problem: if I do it in the wrong place, the menu may be reenabled later – opto Jan 05 '17 at 09:00