Sign in to follow this  
CDProp

Silly MFC problem (enabling/disabling menu items)

Recommended Posts

Hello. I wish to enable and disable items in my dialog's main menu (File, Edit, View, etc.) depending on the circumstance. I get the File Menu's object like this:
CMenu *MainMenu		= AfxGetApp()->m_pMainWnd->GetMenu();
CMenu *pFileMenu	= MainMenu->GetSubMenu( 0 );
Since File is the first menu on the list, this should get the object for me. I set a breakpoint here to verify that pFileMenu is not NULL after this funciton is called, and indeed it is not. So, to disable a menu item, I do the following:
UINT result	= pFileMenu->EnableMenuItem( ID_FILE_MENUITEM, MF_BYCOMMAND | MF_GRAYED);
I set a break point here, and the result IS valid. However, when I hit F5 to resume execution of the program, and then look at the menu item, it is not grayed out like it is supposed to be. Does anyone know what I'm doing wrong?

Share this post


Link to post
Share on other sites
Where are you attempting to modify the menu?

Look into processing this message -> ON_WM_INITMENUPOPUP

void YourWindow::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
//Modify the menu before it is shown.
}

Share this post


Link to post
Share on other sites
I was attempting to do it from the view class (derived from CFormView). Could that be the problem? The reason I did that is that I need to access information in the document in order to determine whether certain options should be enabled or disabled. The only way I know how to access the document, outside of the document itself, is to called GetDocument() from the view that is associated with it.

Share this post


Link to post
Share on other sites
Hmm, Can you processes that message in a CView, I dont remember if views get the menu messages. Try doing it from your mainframe and query the current view for details from there.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this