tags:

views:

33

answers:

1

I have a menu that contains submenus.

eg:

  • Item1
  • Item2
  • Item3
    • item A
    • Item B

Item3 has items under it.

At any given time 1, 2, or the items under 3 should be checked. Since I don't have an ID for Item3 I have to use the MF_BYPOSITION indicator when I try to set a check on Item3 to indicate one of its children has a checkmark. Item3 should have a checkmark if A or B are checked. I am able to check items 1 and 2 and A and B - but can't figure out item3.

I have not been able to successfully use either ::CheckMenuItem() or ModifyMenu() to set the check mark.

Can someone point me to an example that does this successfully? The docs seem to indicate it can be done, but I have been unable to do it.

EDIT

This is for a menu that is set as the menu for a dlg box. The menu bar has three items - one of which drops down to what is shown above.

Note also, it is used as a popup for a right click, but I will take any suggestions to work in either case.

+1  A: 

I've done this before for popup menus. You will need to access the submenu by position, instead of ID. Using your example above, Item 3 would be at position 2:

CMenu popupMenu;
popupMenu.LoadMenu(IDR_MYMENU);
popupMenu.GetSubMenu(0)->CheckMenuItem(2,MF_BYPOSITION|MF_CHECKED);
.
.
.
popupMenu.GetSubMenu(0)->TrackPopupMenu(...);

However, I haven't done this with items in the menu bar.

EDIT by Tim the OP:

For completeness

To get it to work with the menu item you have to get the hmenu

// MENU_POSITION is the zero based location of the menu you want to use. (file, edit, view, help... etc)
HMENU mainMenu = ::GetMenu(m_hWnd);
HMENU subMenu = GetSubMenu( mainMenu, MENU_POSITION);
SetMenuState(subMenu);
flashk
Well, it works for my popup/context menu but not for the menu bar one. What a hassle. There's got to be a way to do it that I have not tried.
Tim
I got it to work. The problem was that I had been passing in HMENUs to a common method. Depending on where it was called from it was either a right-click popup hmenu or the frame's hmenu. THose can't be treated the same when using by position. All other settings were done by id which works.
Tim