EnableMenuItem() - Need Help
I am trying to do something as simple as changing a submenu item on the IDR_MAINFRAME menu from Enabled to grayed. I contuiue to have problems of stupid proportions and have just about given up. Can someone give me an idea as to how to do this? If I were to create a MFC app, from the app wizard, no frills or thrills, can you tell me where and what code I need to add to enable/disable (gray it out) any command of your choosing on the FILE menu? If you could post the segment of code, I would be very greatful.
Thanks!
John Hagen
Re: EnableMenuItem() - Need Help
Check out ON_UPDATE_COMMAND_UI in the message map section. Class wizard gives you the option of including this message in the message map. The implementation is usually this (assume ID_FILE_TESTING is my menu ID):
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_UPDATE_COMMAND_UI(ID_FILE_TESTING, OnUpdateFileTesting)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
...
BOOL MyEnableValue; // Used in my app to determine if menu item should be enabled
...
void CMainFrame::OnUpdateFileTesting(CCmdUI* pCmdUI)
{
// Set Menu to Enabled /Disabled depending on MyEnableValue setting
pCmdUI->Enable(MyEnableValue);
}
Regards,
Paul McKenzie
Re: EnableMenuItem() - Need Help
Hi ,
1) In MainFrame.cpp set bAutoMenuEnable to false as below.
CMainFrame::CMainFrame()
{
m_bAutoMenuEnable = FALSE ;
...
}
2) IN ur View class on UpadateCommand enabel or disabel as required :
void CMyView::OnUpdateUtilitiesStatistics(CCmdUI* pCmdUI)
{
if ( pCmdUI->m_pMenu != NULL )
{
CMenu * pm = pCmdUI->m_pMenu ;
if ( !m_Pause )
pm->EnableMenuItem( 0 , MF_BYPOSITION|MF_GRAYED) ;
else
pm->EnableMenuItem( 0 , MF_BYPOSITION|MF_ENABLED) ;
}
}
Hope this helps you.
Good Luck.
Yash.
Re: EnableMenuItem() - Need Help
Thanks for your help, Yash. It's now working.
Re: EnableMenuItem() - Need Help
Thanks for your help, Paul. It's now working.