I have a MFC application that uses a menu, I want the menu disabled by default (easy enough), but what command do I use to reenable it? I tried

GetDlgItem(IDM_FILE_TEST)->EnableWindow(TRUE);

The program runs up until it executes that statement, then it crashes. Any ideas?

CMenu* menu = GetMenu();
CMenu* submenu = menu->GetSubMenu(0);
submenu->EnableMenuItem(ID_MENUITEM_YOU_WANT_TO_ENABLE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

Just for your information, GetDlgItem is used only for the child windows of the mother dialog. The menu is a separate entity. There for

GetDlgItem(IDM_FILE_TEST)

will return NULL and

GetDlgItem(IDM_FILE_TEST)->EnableWindow()

will fail.

I realized before I posted that that was odviously the wrong thing to use, but it was the closest I could think of. (As a side note, my question was about enabling, and you aswnered how to disable, but I figured it out :P And it works) Just a couple of questions. Why the whole CMenu stuff. Why can't you just do?

EnableMenuItem(ID_MENUITEM_YOU_WANT_TO_ENABLE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

(As a side note, my question was about enabling, and you aswnered how to disable, but I figured it out :P And it works)

Okay. My Bad. I pasted some code I had somewhere so forgot to change it.

Why the whole CMenu stuff. Why can't you just do?

EnableMenuItem(ID_MENUITEM_YOU_WANT_TO_ENABLE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

Well it is like this. You can call CWnd::GetDlgItem directly because it is a member function of the CWnd class. So the appropriate function of your dialog window will be called even if you didnt specify the Object you are calling for.

In Contrast, the CMenu::EnableMenuItem function is not a member of the CWnd Class. It is a member of the CMenu Class. To call this inside the Dialog window, you will have to first get the handle for the object of the desired menu item using CWnd::GetMenu(). Actually what you get here is the pointer for the Menubar. Then use the CMenu::GetSubMenu to get the Menu you want and call the CMenu::EnableMenuItem() function.

ahhhh ok, thanx :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.