在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources
方法二:
下面介绍一种只需对现有代码做较小改动的方法。
在 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 按钮,窗体的界面显示语言就会在英语和简体中文之间互相切换。
在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources的更多相关文章
- 在运行时切换 WinForm 程序的界面语言 ---------多语言设置基础
System.ComponentModel.ComponentResourceManager .ApplyResources 时间:2015-06-17 14:59:06 阅读:473 ...
- BMv2 simple_switch 运行时切换P4程序
参考: [P4-dev] swapping p4 program using load_new_config and swap_configs commands BMv2 运行时切换P4程序 相关演示 ...
- PZISP自动下载软件运行时出现“应用程序无法启动,因为应用程序的并行配置不正确”
在win7下以管理员身份运行“PZISP自动下载软件”时出现“应用程序无法启动,因为应用程序的并行配置不正确”时,是因为系统里面没有一些visual c++库 想一想,反正以后也要用上VS2010的, ...
- C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...
- VC++中的C运行时库浅析(控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
1.概论 运行时库是程序在运行时所需要的库文件,通常运行时库是以LIB或DLL形式提供的.C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于 ...
- Apache Flink 分布式运行时环境
Tasks and Operator Chains(任务及操作链) 在分布式环境下,Flink将操作的子任务链在一起组成一个任务,每一个任务在一个线程中执行.将操作链在一起是一个不错的优化:它减少了线 ...
- Java内存区域(运行时数据区域)详解、JDK1.8与JDK1.7的区别
2.1 概述 对Java程序员来说,在虚拟机自动内存管理机制的帮助下,不再需要为每个对象的new操作去写配对的delete/free 代码,不容易出现内存泄露和内存溢出的问题.不过,仍然需要Java虚 ...
- 基于 Java 2 运行时安全模型的线程协作--转
在 Java 2 之前的版本,运行时的安全模型使用非常严格受限的沙箱模型(Sandbox).读者应该熟悉,Java 不受信的 Applet 代码就是基于这个严格受限的沙箱模型来提供运行时的安全检查.沙 ...
- MFC原理第三讲.RTTI运行时类型识别
MFC原理第三讲.RTTI运行时类型识别 一丶什么是RTTI RTTI. 运行时的时候类型的识别. 运行时类型信息程序.能够使用基类(父类)指针 或者引用 来检查这些指针或者引用所指的对象. 实际派生 ...
随机推荐
- SharedPreference 的存取
1.通过名称来获取指定的SharedPreferences,下面这句代码表示获取名字问hello的SharedPreferences,数据保存在 data/data/package名/shared_p ...
- laravel5.1关于lists函数的bug
查询语句为: class DateAttrModel extends BaseModel{ -- static function getDays(--){ $days = self::lists('d ...
- asp .NET弹出窗口 汇总(精华,麒麟创想)
asp .NET弹出窗口 汇总(精华,麒麟创想) 注://关闭,父窗口弹出对话框,子窗口直接关闭 this.Response.Write("<script language=javas ...
- Truncating HTML attribute value in SharePoint DataFormWebPart
<xsl:value-ofdisable-output-escaping="yes"select="@Body"/>
- poi导出到excel步骤分析
在没用过poi之前感觉poi是很高大上的样子, 项目中用了发现poi的代码重复性很高类似于jdbc的模板代码, 项目中如果大量使用最好封装起来; 总结一下归结为6步 1 打开或新创建一个工作薄(使用H ...
- Indri查询命令及Java调用并保存结果
查询参数 index Indri索引库路径.在参数文件中像/path/to/repository这样指定,在命令行中像-index=/path/to/repository这样指定.该参数可以设置多次来 ...
- 进入 App Store 打分
很多用户用了好软件后忘记或嫌麻烦而不去 App Store 进行打分评星,为此开发者可以在应用中加入打分按钮,点击后直接跳转到 App Store 的评分界面. App Store 上评论的链接地址是 ...
- swift基础--运算符
(1)加减乘除 (2)三目运算,切记后面的空格如果不加会报错的.估计是苹果的bug. (3)聚合运算符,省却了一个判断,很人性化 (4)区间运算符 //加减乘除等等 let a = 2 let b = ...
- mac 如何让文件隐藏
1.首先,要确保知道目标文件或文件夹的名称,你不把这个名称正确地输入到终端中,Mac也无能为力啊... 2.打开终端,输入chflags hidden 3.在上述代码的后面加上该文件夹的路径,但是注意 ...
- jquery.dragsort实现列表拖曳、排序
在一次工作中需要将功能模块实现拖曳并且排序,并且将排序结果保存到数据库,用户下次登录后直接读取数据库排序信息进行显示.LZ找了好多插件,最后发现 jquery.dragsort 这款插件是最好使用的, ...