Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multilanguage menu

Status
Not open for further replies.

ceodav

Programmer
Feb 21, 2003
106
IT
hi all

in a multilanguage application (visualc++ 6) how it is possble to change the menu language without restart it?

when i select a different language i can change everything in the dialog without restarting it but not for the menu.
And inside the menu i change the submenu but i'm not able to detect the main voices.

do you have some suggestions

thanks

Davide
 
I did this about 16 years ago on Windows 2.11. Took a while to locate the code and port it to the current system to see whether it still works. Surprise surprise - yes it still works.

1) Define enum with all the languages
Code:
enum LangEnum
{
   LangTag,       // What the menu was created with
   LangEnglish,
   LangOther,
   LangMax
};
2) Create the menu system in the .rc file
Code:
IDC_MULTILANG MENU 
BEGIN
    POPUP "File"
    BEGIN
        MENUITEM "FileEng",                     ID_FILE_ENGLISH
        MENUITEM "FileOther",                       ID_FILE_OTHER
        MENUITEM "FileExit",                        IDM_EXIT
    END
    POPUP "Help"
    BEGIN
        MENUITEM "HelpAbout",                    IDM_ABOUT
    END
END
3) Set up an array with the language translations
Code:
const TCHAR* langMenu[][LangMax] =
{
   { _T("File"),     _T("&File"),     _T("&Filo") },
   { _T("FileEng"),  _T("&English"),  _T("&English")},
   { _T("FileOther"),_T("&Other"),    _T("&Other") },
   { _T("FileExit"), _T("E&xit"),     _T("&Quit") },
   { _T("Help"),     _T("&Help"),     _T("&Oops") },
   { _T("HelpAbout"),_T("&About..."), _T("&Sad Gits...") }
};
4) Add a couple of routines
Code:
VOID LangChangeMenu (HMENU hMenu, LangEnum pinLang)
{
   TCHAR tag[TAGMAX];
   MENUITEMINFO mii;
   static LangEnum prevLang;
   
   if (hMenu == 0)
   {
      // Initialize
      prevLang = pinLang;
      return;
   }
   
   for (int ii = 0; ::GetMenuString (hMenu, ii, tag, TAGMAX, MF_BYPOSITION); ++ii)
   {
      memset (&mii, 0, sizeof (mii));
      mii.cbSize = sizeof (mii);
      mii.fMask = MIIM_SUBMENU;
      ::GetMenuItemInfo (hMenu, ii, TRUE, &mii);
      if (mii.hSubMenu != 0)
         LangChangeMenu (mii.hSubMenu, pinLang);
            
      for (int jj = sizeof (langMenu) / sizeof (langMenu[0]) - 1; jj >= 0; --jj)
      {
         if (_tcscmp (tag, langMenu[jj][prevLang]) == 0)
         {
            _tcscpy (tag, langMenu[jj][pinLang]);
            mii.fMask |= MIIM_STRING;
            mii.cch   = (UINT) _tcslen (tag);
            mii.dwTypeData = tag;
            mii.fType = MFT_STRING;
            ::SetMenuItemInfo (hMenu, ii, TRUE, &mii);
            break;
         }
      }     
   }
}

VOID LangChange (HWND hWnd, LangEnum pinLang)
{
   MENUBARINFO mbi;
   
   for (int ii = 0;; ++ii) 
   {
      memset (&mbi, 0, sizeof (mbi));
      mbi.cbSize = sizeof (mbi);
      if (::GetMenuBarInfo (hWnd, OBJID_MENU, ii, &mbi))
         LangChangeMenu (mbi.hMenu, pinLang);
      else
         break;
   }
   
   LangChangeMenu (0, pinLang);
   
   ::DrawMenuBar (hWnd);
}
5) Call the language change in InitInstance
Code:
   ShowWindow(hWnd, nCmdShow);
   LangChangeMenu (0, LangTag);		// Set the initial language
   LangChange (hWnd, LangEnglish);	// Switch to English
   UpdateWindow(hWnd);
6) Call the language change in the WndProc, case WM_COMMAND when selected
Code:
		case ID_FILE_ENGLISH:
		   LangChange (hWnd, LangEnglish);
		   break;
		   
		case ID_FILE_OTHER:
		   LangChange (hWnd, LangOther);
		   break;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top