CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006
    Posts
    515

    How to dynamically load main menu in MDI

    I want to dynamically load the main menu (the new manu is in different language) but I can't get passed the GetMenu() call because it alwasy return 0. Here is the code that I am running.
    Code:
    void CMainFrame::OnButtonGerman()
    {
       CMenu *pMenuCurrent = GetMenu();
    
       // has the menu changed?
       // m_hMenuDefault is the default menu resource for this frame see AFXWIN.H
       if(pMenuCurrent->m_hMenu != m_hMenuDefault)
       {
    	   // Destroy the "New" menu and delete the resource
    	   // We, after all created it!
    	   pMenuCurrent->DestroyMenu();
    	   delete pMenuCurrent;
       }
    
       // Load our new men from another resource now
       m_pMenuNew.LoadMenu(IDR_MAINFRAME2);
       
       // Displauy the new menu
       SetMenu(m_pMenuNew);
    }
    My problem is that my pMenuCurrent is always zero so I can't get anywhere. I googled this quite a bit and everyone is usins GetMenu() to access the current menu but I always 0 for it. What could I be missing?

    Note: In this example I am just reloading the menu from a different resource. Once I have this working I will load the new resource from my satellite dll for that language.

    Update:
    This happens only in VC2010. I tried same in VC2005 and it works there.
    Last edited by zspirit; March 6th, 2012 at 04:46 PM.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: How to dynamically load main menu in MDI

    If you want to localize resources (including menus), changing them in the code at run-time is not a good solution.
    In an MDI application, menus are dynamically changed by the framework itself. Besides, the way to change may differ from one MFC implamentation to another one. E.g. if using MFC shipped with VS2010, your main frame window class may be derived from CMDIFrameWndEx with a different implementation than "classic" CMDIFrameWnd.

    So, a better solution which I propose, is to make localized copies in the resource editor (right-click on the in "Resource View" then choose "Insert Copy...".
    This way, the framework will choose the appropriate resourse having the appropriate language, depending on locale settings in the system's control panel.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: How to dynamically load main menu in MDI

    Funny you should ask this... I've been trying to do exactly the same thing for over a week now, and have made good progress, but unfortunately there seems to be something I'm missing, because it ALMOST works - with one small problem. Once in a while (like when I close a view) the menu reverts back to the incorrect one.

    I have decided not to pursue this, because in our application, it's possible for many views to be opened when the user changes the language choice, and it would be massive modifications to update them all. We currently update the views using UpdateAllViews, and that works well, but each of the frame windows must also be updated since the window title also contains language specific items.

    Having said that, here's a link that you might want to visit to see if you can have any success in your application.

    http://support.microsoft.com/kb/145857

    Good luck - let me know if you have any success.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  4. #4
    Join Date
    Aug 2006
    Posts
    515

    Re: How to dynamically load main menu in MDI

    Quote Originally Posted by ovidiucucu View Post
    So, a better solution which I propose, is to make localized copies in the resource editor (right-click on the in "Resource View" then choose "Insert Copy...".
    This way, the framework will choose the appropriate resourse having the appropriate language, depending on locale settings in the system's control panel.
    I would really like to explore this, can you give me any info/link how to use 'Insert Copy' thing for languages? My understanding is that satellite dll is the recommended option like in this microsoft documentation which describes how there is support for multiple languages in MFC. An excerpt from here

    "To support localized resources in an MFC application, MFC attempts to load a satellite DLL containing resources localized to a specific language. Satellite DLLs are named ApplicationNameXXX.dll, where ApplicationName is the name of the .exe or .dll using MFC, and XXX is the three-letter code for the language of the resources (for example, 'ENU' or 'DEU')."

    Does 'Insert Copy' of resource does something similar? When I 'Insert Copy', there is a 'condition' field..can you tell me what this is for? Thanks!

  5. #5
    Join Date
    Aug 2006
    Posts
    515

    Re: How to dynamically load main menu in MDI

    Quote Originally Posted by krmed View Post
    We currently update the views using UpdateAllViews, and that works well, but each of the frame windows must also be updated since the window title also contains language specific items.
    Are you using satellite dlls as well or what ovidiucucu suggested?

    You might have to override OnUpdateFrameTitle() to update the title. Thanks for the link.

  6. #6
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: How to dynamically load main menu in MDI

    Quote Originally Posted by zspirit View Post
    Are you using satellite dlls as well or what ovidiucucu suggested?

    You might have to override OnUpdateFrameTitle() to update the title. Thanks for the link.
    Actually, we're using separate resource-only DLLs - one for each supported language. At run time, we check a user-selected language flag (stored in the registry) and then load the appropriate resource DLL. Since we add strings frequently, and modify the various dialogs and such, it provides an easy way to do languages. We have language resource DLLs for each application, and also a "string" resource DLL that contains the various strings we build at run-time. There is one string DLL for each language.

    In our application (MDI), we really only have one document open, however it is quite possible to have numerous views open, and each different type of view has its own frame window associated with it. There is too much retrofit to modify all of them, so we have decided just to continue with the current scheme - when the user changes the language, we simply give them a message that the changes will take effect the next time they start the application.

    When we change the .rc file of an app, we simply run an rc file translator that makes a language specific .rc file for the resource-only DLL. It uses a glossary and translates the original .rc file strings as needed.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

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