Question

I have been working with winapi just a little bit, making a project with owner draw on menus. When I called GetMenuItemInfo, it sets the text of the menu item, but not the fType UINT variable flags.
Currently I have declared:

MenuItem->fMask = MIIM_TYPE

And MSDN says:

MIIM_TYPE Retrieves or sets the fType and dwTypeData members.

I don't know If I got confused with the MIIM_TYPE flag.

Here is my code:

void SetOwnerDrawMenu(HMENU * menu)
{
MENUIF * menu_item_information;
HMENU sub_menu_ocational;

UINT uId_menuitem;
int nMenuCountItems = GetMenuItemCount(*menu);

MENUITEMINFO * MenuItem = (MENUITEMINFO*)malloc(sizeof(MENUITEMINFO));

for(int i=0;i<nMenuCountItems;i++)
{

    menu_item_information = (MENUIF*)malloc(sizeof(MENUIF));
    menu_item_information->isSeparator=false;
    menu_item_information->max_width=0;
    sub_menu_ocational = 0;
    uId_menuitem = GetMenuItemID(*menu,i);
    memset(&MenuItem,0,sizeof(MenuItem));
    MenuItem = (MENUITEMINFO*)malloc(sizeof(MENUITEMINFO));
    MenuItem->cbSize = sizeof(MenuItem);
    MenuItem->fMask = MIIM_TYPE;
    MenuItem->cch = MAX_ODM_CCH;
    MenuItem->dwTypeData = menu_item_information->szItemText;

    GetMenuItemInfo(*menu,uId_menuitem,FALSE,MenuItem);

    UINT final_flags = MF_BYPOSITION | MF_OWNERDRAW;

    if( ( MFT_SEPARATOR & MenuItem->fType ) == MFT_SEPARATOR )
    {
        final_flags |= MF_SEPARATOR;
        menu_item_information->isSeparator = true;
    }
    else
    {
        // Not important stuff
    }

    sub_menu_ocational = GetSubMenu(*menu,i);

    if(sub_menu_ocational!=NULL)
    {
        ModifyMenu(*menu,i,final_flags,0,(LPCTSTR)menu_item_information);
        // We got a submenu, repeat this operation
        SetOwnerDrawMenu(&sub_menu_ocational);
    }
    else
    {
        ModifyMenu(*menu,i,final_flags,0,(LPCTSTR)menu_item_information);
    }
}
}

I am inserting the menus with the InsertMenu function:

InsertMenu(tid_cmenu,0,MF_BYPOSITION | MF_SEPARATOR,0,NULL);
InsertMenu(tid_cmenu,0, MF_BYPOSITION | MF_STRING, TID_EXIT, "Exit");

Exactly, why the GetMenuItemInfo is not retriving the fType?

Was it helpful?

Solution 2

Ok. The problem is not syntax or memory size errors. It is more like 'logic' error and a silly mistake.

The ModifyMenu was changing all the menu items and setting the fType of each one to NULL or setting the MF_SEPARATOR to all of the items.

That happened because the fourth argument of the ModifyMenu should be the ID of the menu item, I was declaring it as 0.

I changed that argument to the real ID of the menu Item using the GetMenuItemID return value inside the uId_menuitem variable and passing it to the fourth argument of ModifyMenu. That fixed the problem.

Thanks!

OTHER TIPS

If you were checking the return code from GetMenuItemInfo you would see that it is failing. Your error is in this line:

MenuItem->cbSize = sizeof(MenuItem);

The MENUITEMINFO::cbSize member is supposed to be set to the size of a MENUITEMINFO structure, but you are setting it to the size of a MENUITEMINFO* pointer (i.e. 4 or 8 bytes, depending on the platform).

Change your code to:

MenuItem->cbSize = sizeof(MENUITEMINFO);

Also, your code is allocating MenuItem outside the loop, as well as once per-iteration inside the loop, so you are leaking memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top