dynamically changing MDI MainFrame menu 
Author Message
 dynamically changing MDI MainFrame menu

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? I've tried just about everywhere I can think
(View,App,Mainframe,ChildFrame). The best was:

CChildFrame::OnMDIActivate

but this caused lot's of flashing with the default menu bar.
--

======================================
Brent Corkum
Rocscience Inc.
ph: (416) 978 4576
fax: (416) 978 8906

web: http://www.*-*-*.com/
======================================



Thu, 13 Apr 2000 02:00:00 GMT  
 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
----
Address is altered to discourage junk mail.
Remove ".---" for the real address.



Thu, 13 Apr 2000 02:00:00 GMT  
 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



Sun, 16 Apr 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dynamically changing menu in MDI app

2. MDI Menus - how to change dynamically?

3. Dynamically changing menus in an MDI application

4. Changing Mainframe's title dynamically

5. removing menu from mdi mainframe

6. how to change the color of the MainFrame in SDI or MDI

7. Changing MDI MainFrame icon

8. Dynamically Customizing MDI menus

9. Dynamically changing main window title bar for an MDI app

10. changing controls dynamically on a MDI window

11. Dynamically change a menu label

12. Dynamically change menu text

 

 
Powered by phpBB® Forum Software