
dynamically changing MDI MainFrame menu
Quote:
>>I know enough to use the following to change the MDI MainFrame menu:
>>CMenu newmenu;
>> newmenu.LoadMenu(IDR_CUSTOMMENU);
>>AfxGetMainWnd()->SetMenu(&newmenu);
>> AfxGetMainWnd()->DrawMenuBar();
>> newmenu.Detach();
>>But where is the best place to put it so that it remains permanent when I
>>open new documents and views?
>Brent,
>Have you tried using WM_MDISETMENU rather than SetMenu?
>Looking at the MFC source code shows that MFC switches the menu in
>CMDIFrameWnd::OnUpdateFrameMenu and CMDIChildWnd::OnUpdateFrameMenu,
>so if you can override them, that may be a better choice.
>Dave
I recently just wrote some code to change the menus dynamically at run-time
to
a different language, so I know a little about the problems in this area.
The way I did it was to destroy the old Mainframe's menu and add my new one.
Then set the 'm_hMe{*filter*}fault' member to your new menu.
The problem then is that the Document/View templates cache their menus
so you need to replace these as well. To get around this if you are using
MDI
derive a class from CMultiDocTemplate, and add the following member to it
void CCustomMultiDocTemplate::ReloadResources()
{
// Free the menu
if (m_hMenuShared != NULL)
{
::DestroyMenu(m_hMenuShared);
}
// Free the accelarator table
if (m_hAccelTable != NULL)
::FreeResource((HGLOBAL)m_hAccelTable);
// Reload
CDocTemplate::LoadTemplate();
// Change this to Load whatever menu you want
HINSTANCE hInst =
AfxFindResourceHandle(MAKEINTRESOURCE(m_nIDResource), RT_MENU);
// Change this to Load whatever menu you want
m_hMenuShared = ::LoadMenu(hInst, MAKEINTRESOURCE(m_nIDResource));
m_hAccelTable = ::LoadAccelerators(hInst,
MAKEINTRESOURCE(m_nIDResource));
Quote:
}
Use this class to create your doc/view templates.
Then when you want to change menus do something like the following
POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
CCustomMultiDocTemplate* pCCustomMultiDocTemplate;
while (pos != NULL)
{
pCCustomMultiDocTemplate =
dynamic_cast<CCustomMultiDocTemplate*>(AfxGetApp()->GetNextDocTemplate(pos))
;
pCCustomMultiDocTemplate->ReloadResources();
}
The code above needs modified slightly to suit your needs, but you should
get the general idea.
Nic