winform使用本地化,中英文切换 您所在的位置:网站首页 2130的英文 winform使用本地化,中英文切换

winform使用本地化,中英文切换

2024-07-09 20:02| 来源: 网络整理| 查看: 265

在有些软件中,需要中英文切换的功能,甚至其他语言切换的功能,都可以使用winform自带的本地化功能。一共有2种方法。

第一种方法

1.首先建立一个项目,拖几个控件上去,如图所示。

2.点击Form1的属性,设置以下2项

此时,窗体就会变成带有英语的字样

3.这个时候,我们选择窗体界面上的控件,对控件的Text属性,进行英文填写,如图所示

4.如果想要切换到中文模式,也就是我们的默认模式,点击Form1的属性,把语言设置成默认,就是我们一开始的中文模式。如果要增加其他语言模式,重复第3步即可

在此界面上,修改中文模式的字体,如图所示

5.当我们修改完中文(默认)和英文模式后,在项目中,会出现2个文件,带en的就是英文,另一个就是中文。

 当我们分别打开后,也可以在这个里面进行修改

6.回到主界面中,分别写入radioButton的2个事件

 7.代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh");//中文是zh ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); resources.ApplyResources(label1, "label1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Text resources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Text resources.ApplyResources(this, "$this"); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en // Reapplies resources. ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); resources.ApplyResources(label1, "label1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中label1.Text resources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Text resources.ApplyResources(this, "$this"); } } }

8.效果

拓展1

我们也可以使用1个按钮进行切换

界面增加一个按钮,在按钮中写入以下代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh");//中文是zh ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); resources.ApplyResources(label1, "label1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Text resources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Text resources.ApplyResources(this, "$this"); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en // Reapplies resources. ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); resources.ApplyResources(label1, "label1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中label1.Text resources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Text resources.ApplyResources(this, "$this"); } private void button2_Click(object sender, EventArgs e) { int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID; currentLcid = (currentLcid == 2052) ? 1033 : 2052;//这里的2052是中文 1033是英文 Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid); ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); resources.ApplyResources(label1, "label1"); resources.ApplyResources(button1, "button1"); resources.ApplyResources(this, "$this"); } } }

拓展2

如果界面中,有大量的控件,那么可以写一个循环去设置

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh");//中文是zh ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); resources.ApplyResources(label1, "label1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Text resources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Text resources.ApplyResources(this, "$this"); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en // Reapplies resources. ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); resources.ApplyResources(label1, "label1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中label1.Text resources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Text resources.ApplyResources(this, "$this"); } private void button2_Click(object sender, EventArgs e) { int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID; currentLcid = (currentLcid == 2052) ? 1033 : 2052;//这里的2052是中文 1033是英文 Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid); ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); //resources.ApplyResources(label1, "label1"); //resources.ApplyResources(button1, "button1"); //resources.ApplyResources(this, "$this"); foreach (Control ct in this.Controls)//循环当前界面所有的控件 { resources.ApplyResources(ct, ct.Name); if (ct.HasChildren) { resources.ApplyResources(ct, ct.Name); } } } } }

第二种方法

这个是从全局的视角出发

1.建立一个项目,界面如图,这里我们点击English和中文按钮来切换中英文 

2.右键建立一个Resource文件夹,在Resource文件夹中,建立一个中文资源文件和一个英文资源文件

 3.打开对应的英文资源文件,看到名称和值。值就是对应的英文,名称分为3部分

Form1.button1.Text。

Form1是窗体

button1是窗体里面控件的名称

Text是控件文本

 注意:这里不能错,否则无效,还可以增加其他界面的值,有几个界面就写几个界面,格式要保持一样就行了。中文资源文件也按照英文资源文件一样操作。

修改好后的文件是

 4.此时我们回到主界面中,在2个按钮中增加对应的代码

代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { Thread.CurrentThread.CurrentCulture = new CultureInfo("en");//英文 ApplyResource(this);//传入当前的界面 } private void button3_Click(object sender, EventArgs e) { Thread.CurrentThread.CurrentCulture = new CultureInfo("zh");//中文 ApplyResource(this);//传入当前的界面 } ComponentResourceManager crm; public void ApplyResource(Control control) { switch (Thread.CurrentThread.CurrentCulture.Name) { case "en": crm = new ComponentResourceManager(typeof(Resource.Resource_en)); break; case "zh": crm = new ComponentResourceManager(typeof(Resource.Resource_zh)); break; default: crm = new ComponentResourceManager(typeof(Resource.Resource_zh)); break; } applyControl(control.GetType().Name, control);//调用 } //递归应用到控件 private void applyControl(string topName, Control control) { foreach (Control ctl in control.Controls) { crm.ApplyResources(ctl, topName + "." + ctl.Name, Thread.CurrentThread.CurrentCulture); if (ctl.HasChildren) { applyControl(topName, ctl); } } } } }

5.效果

拓展

用这个办法,会比上面更加的简单,使用配置文件。

1.上面修改英文资源文件和中文资源文件的方法不变,这里不说了。 

2.在App.config文件中配置如下代码

3.在程序的入口处,写入以下代码

代码

using System; using System.Collections.Generic; using System.Configuration; using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp5 { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Thread.CurrentThread.CurrentUICulture = new CultureInfo(ConfigurationManager.AppSettings["CultureInfo"]); //调用配置文件 Application.Run(new Form1()); } } }

4. 以后启动软件的时候,只需要修改配置即可。

注意:如果中英文切换的时候,牵扯到字体长度问题,那么直接修改窗体的控件位置就行了。中文就移动中文的位置,英文就移动英文的位置。此功能也可以解决一个cs文件配套多个界面的问题。

 

来源:winform使用本地化,中英文切换_winform 中英文切换_故里2130的博客-CSDN博客



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有