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

这篇具有很好参考价值的文章主要介绍了winform使用本地化,中英文切换。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

第一种方法

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

winform使用本地化,中英文切换 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.效果

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

拓展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和中文按钮来切换中英文 

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

2.右键建立一个Resource文件夹,在Resource文件夹中,建立一个中文资源文件winform使用本地化,中英文切换和一个英文资源文件winform使用本地化,中英文切换

winform使用本地化,中英文切换 3.打开对应的英文资源文件,看到名称和值。值就是对应的英文,名称分为3部分

Form1.button1.Text。

Form1是窗体

button1是窗体里面控件的名称

Text是控件文本

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

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

修改好后的文件是

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

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

 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.效果

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

拓展

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

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

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

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. 以后启动软件的时候,只需要修改配置即可。

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

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

 

来源:winform使用本地化,中英文切换_winform 中英文切换_故里2130的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-406178.html

到了这里,关于winform使用本地化,中英文切换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用正则表达式在中英文之间添加空格

    博主历时三年精心创作的《大数据平台架构与原型实现:数据中台建设实战》一书现已由知名IT图书品牌电子工业出版社博文视点出版发行,点击《重磅推荐:建大数据平台太难了!给我发个工程原型吧!》了解图书详情,京东购书链接:https://item.jd.com/12677623.html,扫描左侧

    2024年02月09日
    浏览(33)
  • vue 管理后台 使用虚拟键盘组件 支持中英文切换

    前言:在大型触屏设备(如双屏设备)中,就没有键盘去操作,而且在触屏input或者textarea的输入时候就无法去输入值,没办法触发输入框enter事件,所以就需要去建立一个虚拟键盘去操作 就想着找一找vue有没有类似的键盘组件。 还!真!有! 项目使用框架:vue+element ui 官网

    2024年02月11日
    浏览(29)
  • nuxt使用i18n进行中英文切换

    中文效果图: 英文效果图: 版本: 安装:  新建en.js与zh.js两个文件进行切换显示 en.js内容  zh.js内容: 在plugins下新建i18n.js i18n.js内容:  在nuxt.config.js引入i18n.js 切换按钮( 两种切换方式选择一种即可 ) 页面展示 有导航的需要在导航切换的时候处理一下相关内容:

    2024年02月07日
    浏览(40)
  • 全网最详细中英文ChatGPT-API文档(一)开始使用ChatGPT——导言

    The OpenAI API can be applied to virtually any task that involves understanding or generating natural language or code. We offer a spectrum of models with different levels of power suitable for different tasks, as well as the ability to fine-tune your own custom models. These models can be used for everything from content generation to semantic search and cl

    2023年04月25日
    浏览(38)
  • Vray中英文对照

    渲染元素,中英文对照表 【英文】 【中文翻译】 VRayAlpha VRay Alpha VRayAO VRay AO VRayDRBucket VRay DR 渲染块 VRayMtlReflectIOR VRay Mtl 反射IOR VRayMtlReflectHilightGlossiness VRay Mtl 反射高光光泽度 VRayMtlReflectGlossiness VRay Mtl 反射光泽度 VRayMtlRefractGlossiness VRay Mtl 折射光泽度 VRayZDepth VRay Z 深度 VR

    2024年02月05日
    浏览(42)
  • Qt 动态中英文切换

            需要界面实现动态国际化,一键点击切换中英文或其他语言。         已经完成了整个界面的翻译,拿到匹配的ts翻译文件,注意:要保证界面切换后,翻译的全覆盖,要保证任何需要反应的地方,都用到了tr(\\\"\\\")包含,不然Linguist会捕捉不到。.ts文件的生成参考下文

    2024年02月10日
    浏览(56)
  • 面试算法十问2(中英文)

    算法题 1: 数组和字符串 Q: How would you find the first non-repeating character in a string? 问:你如何找到字符串中的第一个不重复字符? Explanation: Use a hash table to store the count of each character, then iterate through the string to find the first character with a count of one. 解释: 使用哈希表存储每个字符的计

    2024年04月25日
    浏览(34)
  • 汽车研发与制造中英文对照

    FPDS(Ford Product Development System)福特产品开发系统 threetype chassis 三类底盘 inter-citybus 长途客车 PassengerVehicle 乘用车 MPV(Multi-PurposeVehicle)多用途汽车 SUV(Sports Utility Vehicle) 运动型多用途车 four-wheeldrive 四轮驱动 front-wheeldrive 前轮驱动 DFA-Design For Assembly 面向装配的设计 toolb

    2024年02月21日
    浏览(59)
  • 网络安全中英文术语大全

    01享级持久感动(APT) 一种阿络攻击。使用复杂的技术持续对目标 政府和公司进行网络间谍活造或其他咨意活 动。遗常由具有丰富专业知识和大量安渗的 对手进行-通营与民族国家参与者相关。 这些攻击往往来自多个入口点,并且可能使 用多个攻击媒介 《例运,同络攻击,

    2024年02月05日
    浏览(41)
  • 英文视频自动生成中英文字幕+pr导入并添加字幕

    呐,这里要给大家推荐一个特别强大的工具,那就是 网易见外 ,这是一个AI智能语音转写听翻平台。 我这里主要用到了视频智能字幕功能。整体感觉在国内应该算比较挺强大的,可能也是因为没有用过别的,欢迎小伙伴们推荐别的。嘿嘿! 需要注意的是,有时候生成的字幕

    2024年02月12日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包