Using VC + + to create a multi-lingual menu application [go]

Source: Internet
Author: User

With the rapid development of computer technology, software Exchange more and more frequently, the internationalization of software has become a software developers can not avoid a problem. The so-called internationalization of software is to allow the software interface to support multiple languages, which can be used by people in many countries. In order to enable the application to support multiple languages, a good way is to use the dynamic link library, the different language resources into different libraries, according to the needs of the program load different dynamic libraries, so as to achieve the internationalization of the program. However, this method is not very convenient to implement, the requirements for software developers, especially for beginners, the difficulty is greater. In order to solve this problem, in order to implement the multi-language menu as an example, the author introduces a simple implementation method in VC programming, so as to realize the internationalization support of software. An application interface for Chinese and English menus is given:


(a) Chinese menu

(b) English menu
Figure I, Multi-language menu supported applications


  First, the realization method

If the default menu for the application generated in visual c++6.0 is Chinese, we need to add an English menu to the application first in order to enable the application's menu to support English, the first thing a reader friend might think of is the use of a resource editor provided by Visual C + +, which is of course feasible. But there is a more convenient and faster way. First use the Windows applet Notepad to open the resource file (. rc file) in your project as text, and find the menu text in the project as follows:

idr_mainframe MENU preload discardable beginpopup"file (&f)"Beginmenuitem"New (&n) \tctrl+n", Id_file_newmenuitem"open (&o) ... \tctrl+o", Id_file_openmenuitem"Save (&s) \tctrl+s", Id_file_savemenuitem"Save As (&a) ...", Id_file_save_asmenuitem Separatormenuitem"print (&p) ... \tctrl+p", Id_file_printmenuitem"Print Preview (&v)", Id_file_print_previewmenuitem"print settings (&r) ...", Id_file_print_setupmenuitem Separatormenuitem"Recent Documents", Id_file_mru_file1,grayedmenuitem Separatormenuitem"exit (&x)", Id_app_exitendpopup"Editor (&e)"Beginmenuitem"undo (&u) \tctrl+z", Id_edit_undomenuitem Separatormenuitem"Cut (&t) \tctrl+x", Id_edit_cutmenuitem"copy (&c) \tctrl+c", Id_edit_copymenuitem"paste (&p) \tctrl+v", Id_edit_pasteendpopup"view (&v)"Beginmenuitem"Toolbars (&t)", Id_view_toolbarmenuitem"status bar (&s)", Id_view_status_barendpopup"Help (&h)"Beginmenuitem"about Mutilanguagemenu (&a) ...", Id_app_aboutendend

 

Copy the above content, copy it to the back of the resource file, and modify the first line of the above content to "idr_english_mainframe menu preload discardable", which is called "Idr_" for the English menu. Mainframe_english "the name. Finally, the Chinese description of each menu item is changed to English description, the content is as follows:

"idr_englishi_mainframe MENU preload discardable"Beginpopup"File (&f)"Beginmenuitem"New (&n) \tctrl+n", Id_file_newmenuitem"Open (&o) ... \tctrl+o", Id_file_openmenuitem"Save (&s) \tctrl+s", Id_file_savemenuitem"Save as (&a) ...", Id_file_save_asmenuitem Separatormenuitem"Print (&p) ... \tctrl+p", Id_file_printmenuitem"Printview (&v)", Id_file_print_previewmenuitem"Print Setup (&r) ...", Id_file_print_setupmenuitem Separatormenuitem"recent Files", id_file_mru_file1,grayed MENUITEM separatormenuitem"Exit (&x)", Id_app_exitendpopup"Edit (&e)"Beginmenuitem"Undo (&u) \tctrl+z", Id_edit_undomenuitem Separatormenuitem"Cut (&t) \tctrl+x", Id_edit_cutmenuitem"Copy (&c) \tctrl+c", Id_edit_copymenuitem"Paste (&p) \tctrl+v", Id_edit_pasteendpopup"View (&v)"Beginmenuitem"ToolBar (&t)", Id_view_toolbarmenuitem"StatusBar (&s)", Id_view_status_barendpopup"Help (&h)"Beginmenuitem"About Mutilanguagemenu (&a) ...", Id_app_aboutendend

After modifying the program's resource file and after exiting the disk, it is necessary to add the variable CMenu m_currentmenu object in the program's CMainFrame class to load the Chinese or English menu resources as needed. The CMenu class is the class of the Management menu provided by MFC, the LoadMenu () function of the class that loads the menu resource, and the function is prototyped as follows:

BOOL loadmenu(UINT nidresource);


The parameter nIDResource in the function is the ID number of the menu resource. Returns "Flase" if the function call successfully returns "TRUE".

After the menu resource is loaded, the menu needs to be set to the program's current menu, which requires the SetMenu () function of the CWnd class, which is prototyped as follows:

BOOL setmenu(cmenu* pmenu);


The arguments in the function are a pointer to a menu object. Returns "Flase" if the function call successfully returns "TRUE".

In addition, in order to be able to dynamically set the menu of the program, you also need to add enum variable enum enum{e,c}m_current. For example, if you currently need to set the program to "Chinese menu", you can give m_current an initial value of "C", if the variable equals: "E" when the item has the English menu selected.

  Second, the programming step

1, start the visual c++6.0, generate a single document view structure of the program, named "Mutilanguagemenu";

2, start the Widows "Notepad" program, modify the program's resource files;

3. Add CMenu Class object M_currentmenu and enum variable enum enum{e,c}m_current to the CMainFrame class of the application;

4, add code, compile and run the program.

  Third, the program code

/////////////////////////////////////////////////////////intcmainframe::oncreate (lpcreatestruct lpcreatestruct) {if(Cframewnd::oncreate (lpcreatestruct) = =-1)return-1;if(!m_wndtoolbar.createex ( This, Tbstyle_flat, Ws_child | ws_visible |Cbrs_top| Cbrs_gripper | Cbrs_tooltips |cbrs_flyby| cbrs_size_dynamic) | |!M_wndtoolbar.loadtoolbar (IDR_MAINFRAME)) {TRACE0 ("Failed to create toolbar\n"); return-1;//fail to create}if(!m_wndstatusbar.create ( This) ||! M_wndstatusbar.setindicators (indicators,sizeof(indicators)/sizeof(UINT))) {TRACE0 ("Failed to create status bar\n"); return-1;//fail to create}//Todo:delete These three lines if you don ' t want the toolbar to//Be dockablem_wndtoolbar.enabledocking (Cbrs_align_any); EnableDocking (Cbrs_align_any); DockControlBar (&m_wndToolBar);
//////////////////////////////////////////////////////////////////////////////////// //Multi-language menu implementation  if(m_current==c) M_currentmenu.loadmenu (IDR_MAINFRAME); ElseM_currentmenu.loadmenu ("Idr_english_mainframe");
SetMenu (&m_currentmenu);//Setting the Interface menu ////////////////////////////////////////////////////////////////////////////////////   return 0;}

Iv. Summary

The code in this example compiles the program into a different language version by setting different values for m_current. In fact, you can also add menus in the program to allow users to dynamically change the menu of the program, the specific implementation method reader friends read here should not have any doubts. In addition, for the implementation of the program's dynamic menu, there is a simple way to do this is to publish a text file with the application, the program according to the need to read different string resources from the text file, and then read the string as "title" Assigned to the corresponding menu item.

Transferred from: http://dev.yesky.com/88/2239588.shtml

Using VC + + to create a multi-lingual menu application [go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.