DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Menu items (http://www.daniweb.com/forums/thread36167.html)

atrusmre Dec 1st, 2005 5:12 am
Menu items
 
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?

WolfPack Dec 1st, 2005 6:34 am
Re: Menu items
 
CMenu* menu = GetMenu();
CMenu* submenu = menu->GetSubMenu(0);
submenu->EnableMenuItem(ID_MENUITEM_YOU_WANT_TO_ENABLE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

WolfPack Dec 1st, 2005 7:41 am
Re: Menu items
 
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.

atrusmre Dec 1st, 2005 4:16 pm
Re: Menu items
 
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);

WolfPack Dec 1st, 2005 8:16 pm
Re: Menu items
 
Quote:

Originally Posted by atrusmre
(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.
Quote:

Originally Posted by atrusmre
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.

atrusmre Dec 2nd, 2005 5:05 am
Re: Menu items
 
ahhhh ok, thanx :)


All times are GMT -4. The time now is 1:01 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC