How can I hide a menu item under certain conditions in MFC?
I'm not interested in just graying it out.
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.
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 ...
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);
}
Or if you are removing a single menu item use CMenu::RemoveMenu
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With