I was able solve the mystery myself. I found many of the common menu function wont work. MS has made every item on the toolbar objects. The number of objects associated with a popup menu alone is shocking.
I found out that generally when a menu altering operation has been carried out, rather that calling DrawMenuBar as was the case, the right thing to do is to call:
m_wndMenuBar.AdjustLayout();
m_wndMenuBar.AdjustSizeImmediate();
in order to effect the change.
The following are sample codes that I was able to use to get the desired menu changes.I was able to figure out what to do after spending considerable time on MSDN.
void CMainFrame::ChangeMenu()
{
CMenu menu,menu1;
menu.CreatePopupMenu();
CreateFileMenu(menu.m_hMenu);
m_wndMenuBar.InsertButton (CMFCToolBarMenuButton (0, menu, -1,szMenuName),0);
m_wndMenuBar.AdjustLayout();
m_wndMenuBar.AdjustSizeImmediate();
m_wndMenuBar.RemoveButton(1);
m_wndMenuBar.AdjustLayout();
m_wndMenuBar.AdjustSizeImmediate();
menu1.CreatePopupMenu();
CreateAccessMenu(menu1.m_hMenu);
m_wndMenuBar.InsertButton (CMFCToolBarMenuButton (0, menu1, -1,m_szUserName),3);
m_wndMenuBar.AdjustLayout();
m_wndMenuBar.AdjustSizeImmediate();
}
void CMainFrame::SetUserName()
{
int iCount = m_wndMenuBar.GetCount();
for(int i = 0; i < iCount; i++)
{
CString stText;
m_wndMenuBar.GetButtonText(i,stText);
if(!lstrcmp(stText,szMenuAccess))
{
m_wndMenuBar.SetButtonText(i,m_szUserName);
m_wndMenuBar.AdjustLayout();
m_wndMenuBar.AdjustSizeImmediate();
return;
}
}
}
The following are the definition, if you will, of the functions called in the two menu altering programs above.
bool CreateFileMenu(HMENU hPopup)
{
HMENU hNewPopup = CreatePopupMenu();
InsertMenu(hNewPopup,0,MF_BYPOSITION|MF_STRING,IDM_FILE_NEW_TEMPLATE1,_T("Template1"));
InsertMenu(hNewPopup,1,MF_BYPOSITION|MF_STRING,IDM_FILE_NEW_TEMPLATE2,_T("Template2"));
InsertMenu(hNewPopup,2,MF_BYPOSITION|MF_STRING,IDM_FILE_NEW_TEMPLATE3,_T("Template3"));
InsertMenu(hNewPopup,3,MF_BYPOSITION|MF_STRING,IDM_FILE_NEW_TEMPLATE4,_T("Template4"));
InsertMenu(hPopup,0,MF_BYPOSITION|MF_POPUP,(UINT_PTR)hNewPopup,_T("&New"));
InsertMenu(hPopup,1,MF_BYPOSITION | MF_STRING,ID_FILE_OPEN,_T("&Open \tCtrl+O"));
InsertMenu(hPopup,2,MF_BYPOSITION | MF_STRING,ID_APP_EXIT,_T("E&xit"));
return true;
}
bool CreateAccessMenu(HMENU hPopup)
{
HMENU hNewPopup = CreatePopupMenu();
InsertMenu(hPopup,0,MF_BYPOSITION | MF_STRING,ID_ACCESS_EXITACCESS,_T("Exit Access"));
return true;
}
Thanks all.I hope someone finds this helpful