Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enable / disabled & graying menu item problem !

Posted on 2000-03-14
13
Medium Priority
?
1,396 Views
Last Modified: 2013-11-20
Hi to all,

I have a problem to enable (or disable + graying) a menu item.

I can check the menu item, ...

but I can not enable it (if disabled) or
 I can not disable + graying it (if enabled)

The menu looks like so

&File     &Coucou
.                   Test 1 (IDM_TEST1) ENABLED AT STARTUP
.                   Test 2 (IDM_TEST2) DISABLED + GRAYED AT STARTUP
.

I'm doing so :

void CChildFrame::MenuActions()
 {
  // Get the active document
  CMenuTesterDoc *pDoc = (CMenuTesterDoc *)GetActiveDocument();
  ASSERT_KINDOF(CMenuTesterDoc, pDoc);

  // Locate the <Test 1> submenu
  CMenu *pCoucouMenu = NULL;
  CMenu *pTopMenu = AfxGetMainWnd()->GetMenu();

  for (int iPos = pTopMenu->GetMenuItemCount()-1; iPos >= 0; iPos--)
   {
    CMenu *pMenu = pTopMenu->GetSubMenu(iPos);

    if (pMenu && pMenu->GetMenuItemID(0) == IDM_TEST1)
     {
      pCoucouMenu = pMenu;
      break;
     }
   }
  ASSERT(pCoucouMenu != NULL);

  pCoucouMenu->CheckMenuItem(IDM_TEST1, MF_CHECKED); // WORKS

  pCoucouMenu->EnableMenuItem(IDM_TEST1, MF_DISABLED | MF_GRAYED); // DON'T WORK. WHY ?

  pCoucouMenu->EnableMenuItem(IDM_TEST1, MF_ENABLED); // DON'T WORK. WHY ?
 }
0
Comment
Question by:mike_marquet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 2615921
Hi mike_marquet,

I think your enableing/disabling is overridden by default COMMAND_UPDATE_UI message handling ... Each time a menu item is displayed it send a sends a notification to the framework to allow the framework to set state of the menu item.

The default implementation of this message handler looks if there's a message handler for the menu item. If so, it enables the item, if not it disables the item.

Recommended way to enable/disable frame window menu items is using a ON_COMMAND_UPDATE_UI handler function.

hope that helps,

ZOPPO
0
 

Author Comment

by:mike_marquet
ID: 2615944
I don't want to use ON_COMMAND_UPDATE_UI
because there are a lot of checks to perform in the hole application for one menu item.

I want to use the CMenu method.

An idea to disabled default processing ?
0
 

Author Comment

by:mike_marquet
ID: 2615956
Other informations :

When I check the grayed box for a menu item in the resource, on running, this item is not grayed.
But when I check the checked box, it works.

I sink the 2 problems are bound.
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 31

Expert Comment

by:Zoppo
ID: 2616069
Hi mike_marquet,

So, I found a way, but this disables ON_COMMAND_UPDATE_UI for all menus of the window ... ok, just give it a try:

override CMainFrame::OnInitMenuPopup like this:

void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
 CMDIFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, TRUE); // set bSysMenu to TRUE disables ON_COMMAND_UPDATE_UI messages
}

hope that helps,

ZOPPO
0
 

Author Comment

by:mike_marquet
ID: 2616160
Don't work.

When a child window make a change to a menu item, it works, but when I close the child, the changes are toggled to the preview state.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2616204
Not sure if I understand this correct...

Take care: ChildWindow and MainFrame have different menus.

From where do you call this MenuActions?
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 2616785
Do you have a handler for the menu item? MFC will automatically gray out items when therte is no handler.
0
 
LVL 8

Expert Comment

by:gelbert
ID: 2616887
Tyr to overwrite CWnd::OnInitMenuPopup() and enable/disable menu items from there
0
 
LVL 4

Expert Comment

by:abancroft
ID: 2617632
As others have said, the MFC command update mechanism is overriding your code.

Why don't you just calculate a BOOL that indicates the state of the menu and then apply this BOOL in an ON_COMMAND_UPDATE_UI handler?
0
 

Author Comment

by:mike_marquet
ID: 2618681
The MenuActions function is a membre function of a derived CMDIChildWnd class (child frame).

I have a handler for each menu items.

gelbert you said, overwrite OnInitMenuPopup and make the modifcation there. This is not what I want (see above), I can the make the changes in the ON_COMMAND_UPDATE_UI handler which is much easier to use.

For me, the changes must be made in the code (for example on button click). And for this, I must use CMenu - EnableMenuItem.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2618991
Now, mike_marquet, I think you only have two (MFC-supported) possibilities:

1. Override default OnInitMenuPopup() and avoid calling the ON_COMMAND_UPDATE_UI handler for all items.

2. Use ON_COMMAND_UPDATE_UI for all menu items.

To mix these two you'll have to copy the code from the default implementation of OnInitMenuPopup() and insert there code to filter out the items you don't wanna handle via ON_COMMAND_UPDATE. But I'm not sure if this is a good way, because MFC may change in future and using code from MFC function may cause errors then.

ZOPPO
0
 

Author Comment

by:mike_marquet
ID: 2619131
Thanks to all.
I will than use ON_COMMAND_UPDATE_UI if I can not block default implementation.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 150 total points
ID: 2619165
That's the best I think ... perhaps it would be useful for you to somewhere create something like a map holding a menu item ID as key and flags for 'dis-/enabled' and 'checked' state. You could implement i.e. a class which holds this map, fill this map at startup and create one single ON_UDPATE_COMMAND_UI handler function called for all items. In this function you can then get the values for enable/disable and check states from the map. In the class holding the map you can then implement some functions like EnableItem(UINT cmdId, BOOL enable) and CheckItem(UINT cmdId, BOOL enable) and there just set the values in the map. Now you can call i.e.  EnableItem(...) of the class anywhere in the code which will then affect the values used in the ON_UPDATE_COMMAND_UI handler.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question