Winform界面实现控件中英文语言切换

在有些软件中,需要中英文切换的功能,甚至其他语言切换的功能,都可以使用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文件中配置如下代码

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup>
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
        </startup>
      <appSettings>
        <!--en:英文,zh:中文-->
        <add key="CultureInfo" value="en"/>
      </appSettings>
    </configuration>

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
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(ConfigurationManager.AppSettings["CultureInfo"]); //调用配置文件
                Application.Run(new Form1());
            }
        }
    }

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

注意:如果中英文切换的时候,牵扯到字体长度问题,那么直接修改窗体的控件位置就行了。中文就移动中文的位置,英文就移动英文的位置。此功能也可以解决一个cs文件配套多个界面的问题。
学习交流,项目探讨,QQ同号
微信名片

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智尊宝2021

你的鼓励将是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值