Avatar of UdiRaz
UdiRaz

asked on 

How to disable a menuItem?

hi,

I have  a menu. One of the main menu Items has sub menus. On of the sub menus has some more sub menus.
The sub menu does not has a id since it has submenu, and I don't know what its position to disable it by position.

I tried in a loop 100 ids and couldn't make it disabled.

Please advice, how do I get its position?

Thanks

m_menu.EnableMenuItem( ID_LANGUAGES_LANG1, MF_DISABLED|MF_GRAYED|MF_BYPOSITION  );

Open in new window

System ProgrammingC++

Avatar of undefined
Last Comment
Zoppo
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi UdiRaz,

first, if you want to use an ID as in your sample you need to do it with 'MF_BYCOMMAND' instead of 'MF_BYPOSITION'.

But, as you told, a sub menu doesn't have an ID, so you'll have to iterate through the menu items of the parent menu until you find the sub menu you want to disable. This way you can examine it's position (index) in the parent menu.

Hope that helps,

ZOPPO
Avatar of UdiRaz
UdiRaz

ASKER

1. How do I iterate through the menu items?
2. How can I examine its position in the parent menu?

Thanks, Udi
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi,

somehow like this:

// I assume 'hMenu' is the menu-handle to the 'parent' menu
int iPos = -1;

for ( int i = 0; i < GetMenuItemCount( hMenu ); i++ )
{
 MENUITEMINFO inf = { 0 };
 inf.cbSize = sizeof( inf );
 inf.fMask = MIIM_SUBMENU;

 GetMenuItemInfo( hMenu, i, TRUE, &inf );

 if ( NULL == inf.hSubMenu )
 {
  // no sub menu
  continue;
 }

 // here you have to check if the menu item is the sub menu you search for.
 // here we search the item which is a submenu containing ID_LANGUAGES_LANG1
 for ( int j = 0; j < GetMenuItemCount( inf.hSubMenu ) )
 {
  if ( GetMenuItemID( inf.hSubMenu, j ) == ID_LANGUAGES_LANG1 )
  {
   iPos = i;
   break;
  }

  if ( iPos > -1 )
  {
   break;
  }
 }
}

if ( iPos > -1 )
{
 // here iPos is the position index of the sub menu
}


Hope that helps,

ZOPPO
Avatar of Zoppo
Zoppo
Flag of Germany image

Sorry, in the second 'for' is a bug.

It must be:
>  for ( int j = 0; j < GetMenuItemCount( inf.hSubMenu ); j++ )

I hope there aren't more bugs - I didn't test that code, just wrote it here from the scratch ...

ZOPPO
Avatar of UdiRaz
UdiRaz

ASKER

ID_LANGUAGES_LANG1 is the id of the submenu before I created it some addition submenus. The submenu does not has an ID right now.
Avatar of UdiRaz
UdiRaz

ASKER

m_menu.GetMenuItemCount(); returns the number of main menu items. 8 in my case.

from the 7th menuitem I created some more menuItems and from one of them I create even more. I want the index of the one that has no ID. I am not sure now that it has a position...
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo