Solved

Enable / disabled & graying menu item problem !

Posted on 2000-03-14
13
1,342 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
13 Comments
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
Not sure if I understand this correct...

Take care: ChildWindow and MainFrame have different menus.

From where do you call this MenuActions?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Expert Comment

by:danny_pav
Comment Utility
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
Comment Utility
Tyr to overwrite CWnd::OnInitMenuPopup() and enable/disable menu items from there
0
 
LVL 4

Expert Comment

by:abancroft
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
Thanks to all.
I will than use ON_COMMAND_UPDATE_UI if I can not block default implementation.
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 50 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
configuration management 2 99
iSeries DB2 Query 2 83
mapAB Challlenge 35 84
Making an alias 7 51
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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now