Solved

Enable / disabled & graying menu item problem !

Posted on 2000-03-14
13
1,370 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 94
Generic progress indicator 6 117
ShiftLeft challenge 21 86
NotAlone Challenge 20 85
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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