Does EnableMenuItem() work at all?

What I need is when I select MenuItem1, MenuItem1 is grayed and MenuItem2, which is originally grayed, is enabled.
And my code is as follows.

CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem( ID_MenuItem1, MFS_GRAYED );
pMenu->EnableMenuItem( ID_MenuItem2, MFS_ENABLED);
SetMenu( pMenu );

But nothing changes!!! Please help!
P.S. I know how to make it work through ON_UPDATE_COMMAND_UI(). I'm just wondering why EnableMenuItem() doesn't work.
shibaiAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
migelConnect With a Mentor Commented:
Hi!
MFC implemet "smart" menu enabling
main frame wnd enable menu item if found handler for it(and disable it otherwise). Since you have handlers for these menu items MFC olways enabled it.
You can remove this check by setting CFrameWnd::m_bAutoMenuEnable  to the FALSE.
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

do the following

CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem( ID_MenuItem1, MF_BYCOMMAND | MF_GRAYED );
pMenu->EnableMenuItem( ID_MenuItem2, MF_BYCOMMAND | MF_ENABLED);
SetMenu( pMenu );

Try it out.
VinExpert
0
 
migelCommented:
Hi!
call DrawMenuBar() after menu changing.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
inprasCommented:
Hi
try
pMenu->EnableMenuItem(2, MF_GRAYED|MF_BYPOSITION) ;
Hope this helps
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

try this code
CMenu pMenu;
pMenu.LoadMenu (IDR_MAINFRAME);
pMenu->EnableMenuItem( ID_MenuItem1, MF_BYCOMMAND |
                                MF_GRAYED );
pMenu->EnableMenuItem( ID_MenuItem2, MF_BYCOMMAND |
                                MF_ENABLED);
AfxGetMainWnd()->SetMenu( &pMenu );
AfxGetMainWnd()->DrawMenuBar();
pMenu.Detach ();

VinExpert
0
 
shibaiAuthor Commented:
Hi, guys. Thanks for all your inputs.
But I've tried all these before I submitted my question. None worked. I increased points to 200. I'll see who can help me out.
0
 
migelCommented:
Hi!
Where you use this piece of code?
Can you show test project?
0
 
shibaiAuthor Commented:
Briefly, this is a MDI project. I added a submenu which has two items. I then added message handlers for them. In message handlers, I tried to enable one item and gray the other. That it!

If you need more info, let me know. Thanks!
0
 
abancroftCommented:
Do you mean when the menu item command is executed or when you scroll through the menu items?
0
 
Nicolay_ChCommented:
See ON_UPDATE_COMMAND_UI

When MFC want to know status of you menu string or button on any  XxxBar it call function, that handled in this macro...

One Macro for one string or control...

See MSDN for help...
0
 
mikeblasCommented:
EnableMenuItem() works if you call it correctly, as the other folks have shown.  (And you dont' need to call DrawMenu() unless you're doing this while the menu is actually dropped-down.)

But you're writing an MFC project, which means that you should use MFC's UPDATE_COMMAND_UI mechanism. You should add UPDATE_COMMAND_UI handlers for ID_MenuItem1 and ID_MenuItem2, and then edit the handler so that they do what you want:

void CQwerView::OnUpdateWindowMenuitem1(CCmdUI* pCmdUI)
{
   if (bCondition)
      pCmdUI->Enable(TRUE);
   else
      pCmdUI->Enable(FALSE);
}

This helps make your code more reusable, and is the MFC way. Any good learning book on MFC describes this mechanism.

..B ekiM
0
 
Nicolay_ChCommented:
Sorry for dummy answer... I not read question closely...

EnableMenuItem not work for this reason... MFC set status of menu strings with macro ON_UPDATE_COMMAND_UI
0
 
mikeblasCommented:
Which is what I said.

..B ekiM
0
 
shibaiAuthor Commented:
Sorry, Mike. I don't think I could give you the points. I said in my question I know how to make it work with macro ON_UPDATE_COMMAND_UI. And I still believe there got to be a way to make it work through EnableMenuItem(), though it may not be a good way. Otherwise, why do we need this function EnableMenuItem()?
0
 
SadhuCommented:
try following.
CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem( ID_MenuItem1, true);
pMenu->EnableMenuItem( ID_MenuItem2, true);

Sadhu
0
 
mikeblasCommented:
> I said in my question I

Oh! Sorry!  It's amazing what's escaping my eyes these days. I guess I'm getting old.

EnableMenuItem() doesn't work because, after you call EnableMenuItem(), MFC does its ON_UPDATE_COMMAND_UI work and undoes the effect of your direct EnableMenuItem() call.

It's that simple!

You don't "need" EnableMenuItem(). It's provided because there's an API of the same name, and MFC wants to provide that compatibility for you in a much more reusable and object oriented way.

If you want to use EnableMenuItem(), you'll have to get MFC to completely turn off it's ON_UPDATE_COMMAND_UI handling. You can
do that by setting the m_bAutoMenuEnable to FALSE in the constructor of your frame window. MFC will _still_ do most of this processing, though. IF you want to completely halt it, you can handle WM_INITMENUPOPUP and WM_INITMENU and do nothing in response.

..B ekiM
0
 
shibaiAuthor Commented:
Sorry, Mike. Migel gave the correct answer before you. So I'll have to give the points to Migel to be fair. Thanks, guys!
0
All Courses

From novice to tech pro — start learning today.