WinForm 程序的界面多语言切换
下面介绍一种只需对现有代码做较小改动的方法。
在 Visual Studio 的设计视图中,如果在 Properties 窗口中改变了程序的默认界面语言(Language),我们会注意到无论是工程还是窗体对应的 .Designer.cs 文件都会有显著的改变。比如,我们创建一个叫 MyForm 的 form,并且添加一个叫 MyButton 的按钮。
在改变窗体 Properties 中的 Language 属性之前, .Designer.cs 代码文件中的 InitializeComponent 方法的内容大致如下:
- private void InitializeComponent()
- {
- this.myButton = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // myButton
- //
- this.myButton.Location = new System.Drawing.Point(100, 200);
- this.myButton.Name = "myButton";
- this.myButton.Size = new System.Drawing.Size(75, 23);
- this.myButton.TabIndex = 0;
- this.myButton.Text = "My Button";
- this.myButton.UseVisualStyleBackColor = true;
- //
- // myForm
- //
- this.ClientSize = new System.Drawing.Size(292, 273);
- this.Controls.Add(this.myButton);
- this.Name = "MyForm";
- this.Text = "My Form";
- this.ResumeLayout(false);
- }
而在改变了窗体 Properties 中的 Language 属性之后,工程中除了默认的 .resx 文件之外,还会自动添加一个 .zh-CHS.resx 文件(假设我们将 Language 改变为 Chinese (Simplified))。另外,.Designer.cs 文件中的 InitializeComponent 方法也会改变成:
- private void InitializeComponent()
- {
- System.ComponentModel.ComponentResourceManager resources
- = new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
- this.myButton = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // myButton
- //
- this.myButton.AccessibleDescription = null;
- this.myButton.AccessibleName = null;
- resources.ApplyResources(this.myButton, "myButton");
- this.myButton.BackgroundImage = null;
- this.myButton.Font = null;
- this.myButton.Name = "myButton";
- this.myButton.UseVisualStyleBackColor = true;
- //
- // myForm
- //
- this.AccessibleDescription = null;
- this.AccessibleName = null;
- resources.ApplyResources(this, "$this");
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackgroundImage = null;
- this.Controls.Add(this.myButton);
- this.Font = null;
- this.Icon = null;
- this.Name = "myForm";
- this.ResumeLayout(false);
- }
我们注意到改变 Language 属性之后,代码的主要变化有:
- ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
- resources.ApplyResources(this.myButton, "myButton");
resources.ApplyResources(this, "$this");
另外,设置控件属性(比如显示文字 Text,控件大小 Size,显示位置 Location 等)的代码都没有了。也就是说设置控件属性的代码都是由 resources.ApplyResource 方法来完成的。那么在我们想改变 WinForm 程序的界面显示语言的时候,能不能直接调用 ApplyResources 方法呢?答案是肯定的。
为 myButton 添加 Click 事件的事件处理函数:
- private void myButton_Click(object sender, EventArgs e)
- {
- int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;
- currentLcid = (currentLcid == 2052) ? 1033 : 2052;
- // Changes the CurrentUICulture property before changing the resources that are loaded for the win-form.
- Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid);
- // Reapplies resources.
- ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
- resources.ApplyResources(myButton, "myButton");
- resources.ApplyResources(this, "$this");
- }
当程序运行的时候,点击窗体上的 myButton 按钮,窗体的界面显示语言就会在英语和简体中文之间互相切换。
以下是有可能用的帮助方法
2 /**//// <summary>
3 /// 设置当前程序的界面语言
4 /// </summary>
5 /// <param name="lang">language:zh-CN, en-US</param>
6 /// <param name="form">窗体实例</param>
7 /// <param name="formType">窗体类型</param>
8 public static void SetLang(string lang, Form form, Type formType)
9 {
10 System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
11 if (form != null)
12 {
13 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
14 resources.ApplyResources(form, "$this");
15 AppLang(form, resources);
16 }
17 }
18
19 AppLang for control#region AppLang for control
20 /**//// <summary>
21 /// 遍历窗体所有控件,针对其设置当前界面语言
22 /// </summary>
23 /// <param name="control"></param>
24 /// <param name="resources"></param>
25 private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
26 {
27 if (control is MenuStrip)
28 {
29 resources.ApplyResources(control, control.Name);
30 MenuStrip ms = (MenuStrip)control;
31 if (ms.Items.Count > 0)
32 {
33 foreach (ToolStripMenuItem c in ms.Items)
34 {
35 AppLang(c, resources);
36 }
37 }
38 }
39
40 foreach (Control c in control.Controls)
41 {
42 resources.ApplyResources(c, c.Name);
43 AppLang(c, resources);
44 }
45 }
46 #endregion
47
48 AppLang for menuitem#region AppLang for menuitem
49 /**//// <summary>
50 /// 遍历菜单
51 /// </summary>
52 /// <param name="item"></param>
53 /// <param name="resources"></param>
54 private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
55 {
56 if (item is ToolStripMenuItem)
57 {
58 resources.ApplyResources(item, item.Name);
59 ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
60 if (tsmi.DropDownItems.Count > 0)
61 {
62 foreach (ToolStripMenuItem c in tsmi.DropDownItems)
63 {
64 AppLang(c, resources);
65 }
66 }
67 }
68 }
69 #endregion
70
71 #endregion
转载于:https://www.cnblogs.com/luqingfei/archive/2007/03/14/674035.html
WinForm 程序的界面多语言切换相关推荐
- 谈谈Winform程序的界面设计
合理的布局,绚丽的样式,谈谈Winform程序的界面设计 从事Winform开发很多年了,由于项目的需要,设计过各种各样的界面效果.一般来说,运用传统的界面控件元素,合理设计布局,能够设计出比较中规中 ...
- C# winform MessageBox的本地化,语言切换
之前文章说了本地化,只是界面操作的本地化. 如果遇到内置的MessageBox,其实也可以使用本地化,当然你用其他的办法也行. 1.建立一个程序 2.窗体选择语言设置,分别进行ms ms1的设置. ...
- 浅谈Winform程序的界面布局设计
DevExpress v20.2完整版下载 DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.DevExpress WinF ...
- 合理的布局,绚丽的样式,谈谈Winform程序的界面设计
从事Winform开发很多年了,由于项目的需要,设计过各种各样的界面效果.一般来说,运用传统的界面控件元素,合理设计布局,能够设计出比较中规中矩的标准界面:利用一些换肤的控件或者部分界面组件,能够设计 ...
- HTML界面多语言切换
HTML 你需要将所有想要翻译的模块部分的class中加上lang,然后自定义key.还需要有设置语言的按钮,class中添加translate,id用相应的代号. <button class= ...
- java界面多语言切换
前言 使用ResourceBundle和properties文件完成中英文切换 在src下创建文件 language_en.properties properites文件格式为ISO-8859-1,可 ...
- Qt+C++窗体界面中英文多语言切换
程序示例精选 Qt+C++窗体界面中英文语言切换 如需安装运行环境或远程调试,见文章底部个人微信名片,由专业技术人员远程协助! 前言 这篇博客针对<<Qt+C++窗体界面中英文语言切换& ...
- iOS 程序内语言切换 -- 中英文切换
随着时代的发展,应用程序相继出现了不同语言的版本方案,中文,英文,法文,韩文等等:想在应用程序中实现语言的自由切换,需要配置多个语言的文件,根据用户的动态选择获取不同语言文件下的语言文件,从而显示到界 ...
- Android开发应用内多国语言切换
看到微信可以切换语言,切换语言也就是app国际化问题.我们也都会想到通过改变系统的语言,让app是自己跟随系统走,但是每一台手机系统设置中支持选择的语言,就比较少,比如小米2a:就只有 简体中文,繁体 ...
最新文章
- 如何在asp.net页面使用css和js
- Node.js笔记 - 修改文件后自动重启node服务
- 在阿里,我们这样帮助用户实现业务云原生化迁云
- Mysql时间格式转换
- wordcloud用来制作中文词云
- OLEDB不使用SQL语句直接打开数据表
- php apache win7,win7安装apache+php
- mysql timestamp _mysql之TIMESTAMP(时间戳)用法详解
- python爬虫beautifulsoup_python爬虫beautifulsoup解析html方法
- 多条件and查询遇到的问题
- Vue-router路由使用,单页面的实现
- OpenWRT配置Zerotier实现内网映射
- 【loj】#10064. 「一本通 3.1 例 1」黑暗城堡(最短路径生成树 dijkstra+Prim)
- 可视化大屏是什么?有哪些应用场景?
- 2021山东上半年软考时间已定!!!
- Python编程好不好学?入门难吗?
- flink boardCast--广播变量
- IE,火狐,谷歌之间差异
- 免费论文下载:林巧稚的论文期刊
- java怎么编写软键盘_输入法编程教程---软键盘(小键盘?)类,C++编写
热门文章
- 微软的JavaScript,Post的实例(XMLHTTP)
- python中matplotlib.pyplot的使用示例
- 图像特征检测描述(一):SIFT、SURF、ORB、HOG、LBP特征的原理概述及OpenCV代码实现
- 在DataGridView控件中加入ComboBox下拉列表框的实现
- 用正则表达式验证php用户注册系统,php用户注册时常用的检验函数实例总结
- 带哨兵的冒泡排序_冒泡排序的优化以及快排过程及优化
- leetcode算法题--学生分数的最小差值
- leetcode算法题-- 买卖股票的最佳时机
- leetcode算法题--无重复字符的最长子串
- linux ssh登录源地址限制,H3C交换机配置SSH源地址登录限制和SNMP源地址限制的方法...