CMenu problem with a dynamic submenu
Hello all,
I have a dialog based MFC application (VS7) and in the beginning I
didn't have many things to place into a menu. So I extended the system
menu in OnInitDialog and implemented the handlers accordingly. Now the
menu item list has grown up and it still works with the system menu.
The problem now is that I want to add a submenu as the first extension
to the system menu to place a recent file list in there. I added a blank
popup menu resource containing a separator - because the resource
compiler complains about totally empty menus. I never did such a thing
manually and it crashes adding the submenu to the system menu. I did it
like this:
// this is part of OnInitDialog():
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strRecent;
VERIFY(strRecent.LoadString(IDS_OPEN_RECENT));
UpdateRecentFileMenu();
// m_pMnuRecent is member and "new CMenu" done before
if (!strRecent.IsEmpty() && m_pMnuRecent != NULL)
{
HMENU hMenu = m_pMnuRecent->GetSafeHmenu();
(void)pSysMenu->AppendMenu(MF_POPUP,
(UINT_PTR)hMenu);
(void)pSysMenu->AppendMenu(MF_SEPARATOR);
}
}
// and here is first part of UpdateRecentFileMenu():
if (m_pMnuRecent != NULL)
{
BOOL bOK = m_pMnuRecent->DestroyMenu();
// initialize popup menu with the resource
if (m_pMnuRecent->LoadMenu(IDR_SUBMENU_RECENT))
{
// it crashes here !!! afxwin: IsMenu() == FALSE
CMenu* pMnuPopup = m_pMnuPopup->GetSubMenu(0);
ASSERT(pMnuPopup != NULL);
pMnuPopup->EnableMenuItem(IDR_SUBMENU_RECENT,
MF_BYPOSITION|MF_DISABLED|MF_GRAYED);
}
}
Well, I did a similar thing with GetSubMenu() for a popup that is
tracked on the taskbar which works. This one does not. Why that?
So how can I add this almost empty menu as an entry into the system
menu? And do I have to also associate ID's for the submenu items that
meet the system menu's condition, i.e. ID & 0x0fff?
Is it the right thing to disable and gray the submenu entry by the line
listed in the code above?
Regards,
Konran