Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a menu item in MFC

How can I hide a menu item under certain conditions in MFC?
I'm not interested in just graying it out.

like image 325
Dana Avatar asked Jan 05 '09 15:01

Dana


People also ask

How do I hide menu items in MFC?

It's not possible to hide a menu in Win32, MFC is just a wrapper over this framework. You've got to remove this menu item when not needed and then insert it back at the same location using InsertMenu, have a look at MF_BYPOSITION and MF_BYCOMMAND.

How do I hide static text in MFC?

RC file the static text is immediately before the control it labels, you can do something like CWnd* pControl = GetDlgItem(IDC_SOMECONTROL); CWnd* pStatic = pControl->GetWindow(GW_HWNDPREV); pStatic->ShowWindow(SW_HIDE); I'd probably be extra careful and wrap in if statements so that you don't accidentally dereference ...


2 Answers

Add an Update Handler for your menu item (using ON_UPDATE_COMMAND_UI).

This line should appear in your message map:

  ON_UPDATE_COMMAND_UI(ID_MYMENUITEM, OnUpdateMyMenuItem)

In the handler, use this code:

void CMainFrame::OnUpdateMyMenuItem(CCmdUI *pCmdUI)
{
  if (pCmdUI->m_pMenu!=NULL)
    pCmdUI->m_pMenu->DeleteMenu(pCmdUI->m_nID, MF_BYCOMMAND);
}
like image 78
Serge Wautier Avatar answered Sep 28 '22 04:09

Serge Wautier


Or if you are removing a single menu item use CMenu::RemoveMenu

like image 37
SmacL Avatar answered Sep 28 '22 03:09

SmacL