From: SM on
Hi All,
I have menu like below in the resource file
File
Option1
Option2
Option3
------------(menu separator)
Option4
-------------
Exit
During run time i need to add a new menu "Option5" before "Option4",
new menu will be like below
File
Option1
Option2
Option3
------------(menu separator)
Option5->Option6
Option7

Option4
-------------
Exit
For this i looped through the menu items using GetMenuItemCount for
getting the position to insert new menu
int menuCount = GetMenuItemCount(fileMenu);
int menuPosToInsert =0;
for(int i=0;i<menuCount ;i++)
{

if(GetMenuItemID(fileMenu,i) == MF_SEPARATOR)
{
menuPosToInsert=i;
}


}

GetMenuItemID(fileMenu,i) for separator is always returning 0 only,
so changed the code like below
if(GetMenuItemID(fileMenu,i) == 0)
after this, i used insertmenu to add the new menu item and it's
working fine.
Is the above approach ok for adding new menu item? i.e finding the
menu separator

Thanks,
Sony

From: Doug Harrison [MVP] on
On Fri, 16 Apr 2010 06:18:10 -0700 (PDT), SM <sony.m.2007(a)googlemail.com>
wrote:

>int menuCount = GetMenuItemCount(fileMenu);
> int menuPosToInsert =0;
> for(int i=0;i<menuCount ;i++)
> {
>
> if(GetMenuItemID(fileMenu,i) == MF_SEPARATOR)

Where'd you get the idea to use MF_SEPARATOR as an ID? It's a flag, not an
ID.

> {
> menuPosToInsert=i;

You probably want to "break;" here? Be careful about adding things inside a
loop and continuing to loop.

> }
>
>
> }
>
>GetMenuItemID(fileMenu,i) for separator is always returning 0 only,
>so changed the code like below
>if(GetMenuItemID(fileMenu,i) == 0)
>after this, i used insertmenu to add the new menu item and it's
>working fine.
>Is the above approach ok for adding new menu item? i.e finding the
>menu separator

The MFC documentation for CMenu::GetMenuItemID says it returns zero for
separators, but it doesn't say zero cannot be returned for anything else.
The Platform SDK is silent on this. So I'd go with the Platform SDK and not
assume anything. This is what I've used:

MENUITEMINFO info = { sizeof(info), MIIM_FTYPE };
if (menu.GetMenuItemInfo(..., &info, ...)
&& (info.fType & MFT_SEPARATOR))
{
// Is a separator
}

Aside: Note that MFT_STRING is an oddball with the value zero and would
require special handling. Microsoft has a bad habit of including values of
0 in bitflag sets, so you always have to watch for this. In fact, I'd
always look the values up in the Windows headers if they aren't given by
the documentation, which they typically aren't. However, a good clue is
conflicting language such as "This member can be one or more of the
following values. The MFT_BITMAP, MFT_SEPARATOR, and MFT_STRING values
cannot be combined with one another."

--
Doug Harrison
Visual C++ MVP