CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2005
    Posts
    218

    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?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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);
    	}
    Why are you creating all your submenues in the heap, not on the stack?
    Note, that you do NOT delete these pSubMenus!
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured