在运行时切换 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. 运行时的时候类型的识别. 运行时类型信息程序.能够使用基类(父类)指针 或者引用 来检查这些指针或者引用所指的对象. 实际派生 ...
随机推荐
- javascript insertBefore 和 appendChild
js的appendChild()方法 :在一个元素内部追加一个子节点. js的insertBefore()方法:在一个元素内部指定的子节点之前插入子节点. 很明显,appendChild()方法只需要 ...
- PAT乙级真题1002. 写出这个数 (20)(解题)
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...
- Git之不明觉厉11-利其器source tree
前面10篇文章都在用命令行,虽然装逼不错,但是我想说一句,平时我也是用source tree比较多点,命令行一般都是在source tree的图形按钮找不到在哪里,就直接用命令行.对于初次用git的同 ...
- gc overhead limit exceeded
eclipse-- gc overhead limit exceeded 修改内存不足的方法如下: Eclipse报错:gc overhead limit exceeded eclipse 原因是Ec ...
- NET Reflector 8 使用
一,把杀毒软件停掉 二,把原机器上的 Reflector 文件删除 三,找到C:\Users\Administrator\AppData\Local\Red Gate这个目录,将里面的东西删除 四,v ...
- 编译升级php
http://www.linux-centos.com/2014/11/16/%E7%BC%96%E8%AF%91%E5%8D%87%E7%BA%A7%E4%BB%8Ephp5-2-17%E5%88% ...
- custom activities
Useful Sharepoint Designer Custom Workflow Activities http://spdactivities.codeplex.com/ http://stac ...
- setPreferredSize和setSize的区别及用法
我以前很喜欢borderlayout的布局方式,每次想特别调整每个区域的大小,但是每次将一个panel放入到north或者其他4个区域时,总是达不到想要的效果,刚刚才发现原来setPreferredS ...
- Any Way You Slice It (向量旋转 以及 判断线段是否相交)(模板)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11353 #include<iostream> # ...
- embed标签遮住div层
依然是上次的那个返工友情项目,当时帮忙用jquery ui写了一个漂浮的投票箱,就是类似点击一个项目然后就收藏到了投票箱中的效果.. 虽然不是很复杂,但是由于页面上有大面积的由kindeditor上传 ...