problem with appendmenu and menu
Hi,
I want to dynamiccally create a menu. Code is straigtforward and like this:
Code:
std::vector<CFunction*> funcs = m_pUserGroupFunctions->GetAllFunctions();
int group;
CMenu* newMenu;
newMenu = new CMenu();
newMenu->CreateMenu();
CMenu* pSubMenu;
for(int n = 0 ; n < groupcount ; n++)
{
pSubMenu = new CMenu();
VERIFY(pSubMenu->CreatePopupMenu());
for (UINT i = 0 ; i < funcs.size() ; i++)
{
menuNames.Lookup(funcs.at(i)->GetGroupId(),group);
if (group == n)
pSubMenu->AppendMenu(MF_STRING|MF_OWNERDRAW, funcs.at(i)->GetUint(), (LPCTSTR)funcs.at(i)->GetName());
}
newMenu->AppendMenu(MF_POPUP|MF_OWNERDRAW, (UINT) pSubMenu->m_hMenu);
}
All my datastructures are working correctly. I can see that the AppendMenu() calls get the correct string and UINT.
The problem is that the following statements do not work:
Code:
//does not work!!
CMenu tMenu;
tMenu.Attach(newMenu->m_hMenu);
tMenu.GetMenuString (nIndex, m_strBtn, MF_BYPOSITION);
tMenu.Detach();
//does not work
TCHAR szText[256] ;
MENUITEMINFO info; ::memset(&info, 0, sizeof(MENUITEMINFO));
info.cbSize = sizeof(MENUITEMINFO);
info.fMask = MIIM_ID | MIIM_TYPE ;
info.dwTypeData = szText;
info.cch = sizeof(szText);
::GetMenuItemInfo(hMenu, nIndex, TRUE, &info);
m_strBtn = CString(szText);
//does not work
CMenuItemInfo info;
info.cbSize = sizeof(CMenuItemInfo);
info.dwTypeData = NULL;
info.fType = MFT_STRING;
info.fMask = MIIM_STRING;
::GetMenuItemInfo(hMenu, nIndex, TRUE, (MENUITEMINFO *) &info);
char* lpBuffer;
lpBuffer = (char*) malloc(info.cch + 1);
info.dwTypeData = lpBuffer;
info.cch = info.cch + 1;
::GetMenuItemInfo(hMenu, nIndex, TRUE, (MENUITEMINFO *) &info);
m_strBtn = CString(lpBuffer);
//some other CMenu methods don't work either
What does work is the GetMenuItemCount, the GetSubMenu and the GetMenuItemCount on the submenus.
What can be wrong in this code?
Re: problem with appendmenu and menu
[QUOTE=timv]Hi,
I want to dynamiccally create a menu. Code is straigtforward and like this:
Code:
std::vector<CFunction*> funcs = m_pUserGroupFunctions->GetAllFunctions();
int group;
CMenu* newMenu;
newMenu = new CMenu();
newMenu->CreateMenu();
CMenu* pSubMenu;
for(int n = 0 ; n < groupcount ; n++)
{
pSubMenu = new CMenu();
VERIFY(pSubMenu->CreatePopupMenu());
for (UINT i = 0 ; i < funcs.size() ; i++)
{
menuNames.Lookup(funcs.at(i)->GetGroupId(),group);
if (group == n)
pSubMenu->AppendMenu(MF_STRING|MF_OWNERDRAW, funcs.at(i)->GetUint(), (LPCTSTR)funcs.at(i)->GetName());
}
newMenu->AppendMenu(MF_POPUP|MF_OWNERDRAW, (UINT) pSubMenu->m_hMenu);
}
Quote:
Why are you creating all your submenues in the heap, not on the stack?
Note, that you do NOT delete these
pSubMenus!
Quote:
All my datastructures are working correctly. I can see that the AppendMenu() calls get the correct string and UINT.
The problem is that the following statements do not work:
Code:
//does not work!!
CMenu tMenu;
tMenu.Attach(newMenu->m_hMenu);
tMenu.GetMenuString (nIndex, m_strBtn, MF_BYPOSITION);
tMenu.Detach();
//does not work
TCHAR szText[256] ;
MENUITEMINFO info; ::memset(&info, 0, sizeof(MENUITEMINFO));
info.cbSize = sizeof(MENUITEMINFO);
info.fMask = MIIM_ID | MIIM_TYPE ;
info.dwTypeData = szText;
info.cch = sizeof(szText);
::GetMenuItemInfo(hMenu, nIndex, TRUE, &info);
m_strBtn = CString(szText);
< skipped... >
What does work is the GetMenuItemCount, the GetSubMenu and the GetMenuItemCount on the submenus.
What can be wrong in this code?
Where have you declared/defined hMenu variable you are using in ::GetMenuItemInfo(...) call?