The Design of winform in multiple languages and the use of. Net Chinese source files

Source: Internet
Author: User
Design of Multi-language forms in winform and use of. Net Chinese source files

Recently, I have been working on a number of multi-language software programs and I have learned a lot about. Net's multi-language knowledge. I will briefly introduce the implementation methods of the multi-language in winform. It can be divided into seven parts: Resource file Overview , Resource file category , Resource file creation and utilization IDE To create multi-language versions, use resource files for multi-language versions . Net Use of resource file Generator And Summary These seven parts. The first two parts are theoretical things. If you are not interested, you can skip them and check the three parts, namely, 3, 4, and 5. Part 1: Resource file Overview A resource file is a file that stores resources. Resource file in Program The design has its own unique advantages. It is independent from the source program, so that resource files can be used by multiple programs. At the same time, during program design, important things are stored in resource files for security or other reasons, which can also achieve confidentiality and security. Resource files generally store three types of data: byte stream (byte []), object, and string ). For some pure file information, the string type can be used for saving. For images and icons, objects can be used for saving. For others, byte streams can be used for saving. System. Resources The namespace contains a large number of classes and methods to process resource files. I will introduce them in the following chapter.   Part 2: Resource file category Resource files can be divided into two types: files with. resx as the suffix and files with. resources as the suffix. The difference between the two is: 1. Although resx is a file ending with resx, It is a file in XML format. You can use Notepad and other tools to directly open it and modify the content. Resources is a binary file, relatively better security. 2. Resources, as an embedded resource, can be directly referenced in the program when the specified path is correct. Although resx is also an embedded resource, it must be attached to the. CS file. That is to say, it exists as a descriptive resource of the winform form. To use it directly in a program, the solution must have the same name (but the same name and suffix name) as it. the CS file exists (see figure 1 ). This will be detailed in the following example. 3. We can use the CSC command to convert the resx file to the resources file. Resgen. EXE litwarestrings. resx litwarestrings. Resources Note that the variable environment is Framework1.1 . Part 3: Resource File Creation Here I will introduce two methods to create resource files. One is to use System. Resources Under Resourcewriter class, the other is to use the resource file generator. This section focuses on how to use the resourcewriter class to create source files. For the second method, see section 6. First, instantiate a resourcewriter type variable: Resourcewriter RW = new resourcewriter ("My. Resources "); Where My. Resources Is the file name you want to generate. Second, you need to generate this file on the disk: Call RW. Generate (); You can. Next we need to add content for this resource file. The resourcewriter class provides an addresource () method to add resources to the resource file. In C #, different resources are added in different ways. 1. Add a byte array in the syntax format:
Public void addresource (string, byte []);
Note: string is the unique identifier of the byte array in the program when the resource file is used. 2. Add the object. Syntax format:
Public void addresource (string, object );
Note: string is the unique identifier of the object in the program when the resource file is used. In this project, we use this call method to add icons and images, as shown below:
icon ICO = new icon (" Main. ICO ");
image turnoff = image. fromfile ("Turn-off.png");
image turnon = image. fromfile ("turn -on.png");
RW. addresource ("Main. ICO ", ico); // Add an icon to the resource file
// Add images to the resource file
RW. addresource ("turn -off.png", turnoff);
RW. addresource ("turn -on.png", turnlon);
3. Add a string. The syntax is as follows:
Public void addresource (string1, string2); note: the unique identifier of this string in the program is used in the program in this article when the resource file is used:
RW. addresource ("mystr ","Read strings from resource files");
At this point, we have created a resource file and added several resources to the resource file. After that, you should also note that you should save the resource file and close the resource file, the details are as follows:
RW. Close ();
I have introduced the usage Resourcewrite Class to create Resources File method, Resx File, we can Vs2003 Of IDE Right-click solution Management -> Adding a new project -> Control resource file to add. Part 4: Exploitation IDE To create a multi-language version I finally entered the topic, but the content of this section is not relevant to the previous sections, Because I mainly talk about how to use vs ide To create multi-language versions. Each form1.cs file has one or more corresponding resx files as ancillary resources. Their Naming rules are form1.cs resource files form1.resx, form1.zh-chs. resx, form1.zh-CHT. resx, where form1.resx is the default form resource document, and other resource documents to be used in different language environments, where form1.zh-chs. resx is a simplified Chinese system, form1.zh-CHT. resx is a traditional Chinese System (for example ). You can refer to the naming rules of different regions in msdn. (Figure 1) Each form has two attributes: localizable and language. The default value is as follows: (Figure 2) localizable indicates whether to use localized resources. If it is true, you can select a language in the language, for example, select traditional Chinese, for example: (Figure 3) The layout of the form will change, you need to re-layout the widget location, size, content, and so on the form. After the layout is complete, you can continue to select other languages for layout. Don't worry, there will be no conflict between them, because all the layout differences between different languages are saved to the corresponding resx file. Finally, save and compile. To test the results, we can change the language environment of the machine by using the following two methods: 1. Change the language options in the control panel. The other is to use a program for modification. Next I will introduce the second method to use a program to change the language environment of the current process. When the program runs, the system automatically detects the current system language environment. System. Threading. thread. currentthread. currentuiculture This attribute changes the language environment of the current UI, Code As follows: Static VoidMain () { System. Threading. thread. currentthread. currentuiculture =NewSystem. Globalization. cultureinfo ("ZH-CHS "); Application. Run (NewForm1 ()); } (Figure 4) Change System. Threading. thread. currentthread. currentuiculture =NewSystem. Globalization. cultureinfo ("ZH-CHT "); The effect is as follows: (figure 5) Part 5: Use resource files for multi-language versions In section 3, we have discussed how to use the resourcewriter class to create resource files. This section describes how to use these resource files. The methods described in Section 3 are used to create three files, myresource. Resources, myresourcezh_tw.resources, and myresourceen. Resources, which correspond to the default, traditional, and English systems. Each file contains two strings and an object, corresponding to button, Textbox, and icon. Figure 4. The code for the language menu bar in Figure 5 is as follows: Private VoidEnglish_click (ObjectSender, system. eventargs E) { ResourceManager Rm =NewResourceManager ("embedresource. myresourceen", assembly. getexecutingassembly ()); Textbox1.text = RM. getstring ("textboxvalue "); Button1.text = RM. getstring ("buttonvalue "); This. Icon = (icon) Rm. GetObject ("demo. ICO "); } Private VoidChinesetraditional_click (ObjectSender, system. eventargs E) { ResourceManager Rm =NewResourceManager ("embedresource. myresourcezh_tw", assembly. getexecutingassembly ()); Textbox1.text = RM. getstring ("textboxvalue "); Button1.text = RM. getstring ("buttonvalue "); This. Icon = (icon) Rm. GetObject ("demo. ICO "); } As follows: In the second part, I have said that resx and resources call different methods. Next I will talk about how to call the resx file. Because resx is a subsidiary file of the CS file, a CS file with the same name must exist in the project. I. Use the following code to call resx: ResourceManager Rm =NewResourceManager ("embedresource. form1", assembly. getassembly (Typeof(Embedresource. form1 ))); Textbox1.text = RM. GetObject ("textbox1.visible"). tostring (); Where Embedresource Is namespace, form1 is the resource file name (excluding the extension name), the second parameter Assembly. getassembly (Typeof(Embedresource. form1 )) Medium Embedresource Is namespace, and Form1 Is the name of the CS file attached to resx. Note the difference. Part 6: Common . Net Use of resource file Generator The software related to the network has been widely used. I will introduce resourcer.exe to you. It is open-source and can be used in Http://www.aisto.com/roeder/dotnet/ . The running interface is as follows: The software can be automatically saved as text, resx, and resources files. The preceding resources can be easily created using this software. You can download and try it yourself. I won't say much here. Part 7: Summary Through the above introduction, we have already had the foundation to make multi-language production. Of course, this is only a preliminary introduction to multi-language production. Please correct me in the comment. I will change it as soon as possible.

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.