Solved

Enable / disabled & graying menu item problem !

Posted on 2000-03-14
13
1,363 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 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 50 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prevent this page from creating additional dialogs. 3 532
conditional code and condition difference 9 82
java ^ examples 8 66
firstChar challenge 13 115
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

778 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