C#WinForm实现多语言切换的示例

Rose ·
更新时间:2024-09-20
· 1433 次阅读

因项目需要,所以在网上找了一些方法实现了该功能,本文也是做一个总结和记录。使用resx文件实现Winform多语言切换,以实现简体中文、英文、泰语的切换为例。如果后续需要增加其它语言的切换,只需要按照步骤重复操作即可。

效果图如下:

中文:

英语:

泰语:

窗体设置

下面来说一下流程:

1.首先将Form1的 Localizable 属性为 true( 设置该属性后,.net 将根据不同的语言,为应用程序生成不同的资源文件),然后将Language属性设置为所需要的语言,如下所示。

2.当把Language属性设置成例如英语后,那么我们对窗体中的控件名称进行调整,如下图:

别的语言也是这种操作,只是拿英语做个示例。当我们把页面调整完后,我们重新生成一下,然后再把上述Language属性切回到默认或汉语时,再把项目重新生成一下。这样的话,语言文件就会自动生成好了。

注意:一定要重新启动一下,不然改过之后直接运行不会出现语言文件!

当我们把窗体的设置全部搞定后,我们来看怎么设置自动切换,如图所示,我是在登录页面就放了一个下拉框,当选中语言时,所有窗体的语言会自动切换,接下来我们看一下实现方法。

实时语言切换

1.项目的Properties的Settings.settings中添加变量DefaultLanguage,用于保存当前设置的默认语言。当下次启动程序时,会读取该变量,从而将程序的语言设置为上次程序关闭时的语言。第一次将默认语言设置为中文zh-CN。

2.创建一个静态类(MultiLanguage.cs)用于编写与切换语言相关的变量和代码。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CodePrint { //用于编写与切换语言相关的变量和代码 class MultiLanguage { //当前默认语言 public static string DefaultLanguage = "zh-CN"; /// <summary> /// 修改默认语言 /// </summary> /// <param name="lang">待设置默认语言</param> public static void SetDefaultLanguage(string lang) { System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); DefaultLanguage = lang; Properties.Settings.Default.DefaultLanguage = lang; Properties.Settings.Default.Save(); } /// <summary> /// 加载语言 /// </summary> /// <param name="form">加载语言的窗口</param> /// <param name="formType">窗口的类型</param> public static void LoadLanguage(Logins form, Type formType) { if (form != null) { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType); resources.ApplyResources(form, "$this"); Loading(form, resources); } } /// <summary> /// 加载语言 /// </summary> /// <param name="control">控件</param> /// <param name="resources">语言资源</param> private static void Loading(Control control, System.ComponentModel.ComponentResourceManager resources) { if (control is MenuStrip) { //将资源与控件对应 resources.ApplyResources(control, control.Name); MenuStrip ms = (MenuStrip)control; if (ms.Items.Count > 0) { foreach (ToolStripMenuItem c in ms.Items) { //遍历菜单 Loading(c, resources); } } } foreach (Control c in control.Controls) { resources.ApplyResources(c, c.Name); Loading(c, resources); } } /// <summary> /// 遍历菜单 /// </summary> /// <param name="item">菜单项</param> /// <param name="resources">语言资源</param> private static void Loading(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources) { if (item is ToolStripMenuItem) { resources.ApplyResources(item, item.Name); ToolStripMenuItem tsmi = (ToolStripMenuItem)item; if (tsmi.DropDownItems.Count > 0) { foreach (ToolStripMenuItem c in tsmi.DropDownItems) { Loading(c, resources); } } } } } }

3.在窗体的Load(双击Form1即可跳转至该事件)事件中读取Properties.Settings.Default.DefaultLanguage,并将ComboBox赋值为当前默认语言,即简体中文或英文。

private void Login_Load(object sender, EventArgs e) { //设置combobox的值 string language = Properties.Settings.Default.DefaultLanguage; if (language == "zh-CN") { comboBox1.Text = "简体中文(默认)"; } else if (language == "en-US") { comboBox1.Text = "English"; } else if(language == "th-TH") { comboBox1.Text = "ไทย"; } MultiLanguage.LoadLanguage(this, typeof(Logins)); }

4.编写用于切换语言的ComboBox的SelectedIndexChanged事件,使得当用户选择对应的语言时,程序会切换到该语言。

事件添加方式如下:选中ComboBox,点击事件,在SelectedIndexChanged事件中输入方法名languageTxt_SelectedIndexChanged,按回车即可自动生成。

private void languageTxt_SelectedIndexChanged(object sender, EventArgs e) { comboBox1.Enabled = false; if (comboBox1.Text == "中文") { //修改默认语言 MultiLanguage.SetDefaultLanguage("zh-CN"); //对所有打开的窗口重新加载语言 foreach (Form form in Application.OpenForms) { LoadAll(form); } } else if (comboBox1.Text == "English") { //修改默认语言 MultiLanguage.SetDefaultLanguage("en-US"); //对所有打开的窗口重新加载语言 foreach (Form form in Application.OpenForms) { LoadAll(form); } } else if(comboBox1.Text == "ไทย") { //修改默认语言 MultiLanguage.SetDefaultLanguage("th-TH"); //对所有打开的窗口重新加载语言 foreach (Form form in Application.OpenForms) { LoadAll(form); } } comboBox1.Enabled = true; }

LoadAll(form)方法:

private void LoadAll(Form form) { if (form.Name == "Logins") { MultiLanguage.LoadLanguage((Logins)form, typeof(Logins)); } else if (form.Name == "CodePrint") { MultiLanguage.LoadLanguage((Logins)form, typeof(CodePrint)); } else if(form.Name == "CodePrint") { MultiLanguage.LoadLanguage((Logins)form, typeof(CodePrint)); } }

if里面就是我们可以切换的窗体名称。

到此这篇关于C#WinForm实现多语言切换的示例的文章就介绍到这了,更多相关C# WinForm多语言切换内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



示例 多语言 winform

需要 登录 后方可回复, 如果你还没有账号请 注册新账号