System.ComponentModel.ComponentResourceManager .ApplyResources

时间:2015-06-17 14:59:06      阅读:473      评论:0      收藏:0      [点我收藏+]

标签:des   winform   class   style   com   代码   http   si   it

Download the code for this article: WinForm-Multilanguages-2.rar (11 KB).

方法二:

下面介绍一种只需对现有代码做较小改动的方法。

在 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

标签:des   winform   class   style   com   代码   http   si   it

原文:http://www.cnblogs.com/1175429393wljblog/p/4583198.html

在运行时切换 WinForm 程序的界面语言 ---------多语言设置基础的更多相关文章

  1. 在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources

    Download the code for this article: WinForm-Multilanguages-2.rar (11 KB). 方法二: 下面介绍一种只需对现有代码做较小改动的方法 ...

  2. BMv2 simple_switch 运行时切换P4程序

    参考: [P4-dev] swapping p4 program using load_new_config and swap_configs commands BMv2 运行时切换P4程序 相关演示 ...

  3. PZISP自动下载软件运行时出现“应用程序无法启动,因为应用程序的并行配置不正确”

    在win7下以管理员身份运行“PZISP自动下载软件”时出现“应用程序无法启动,因为应用程序的并行配置不正确”时,是因为系统里面没有一些visual c++库 想一想,反正以后也要用上VS2010的, ...

  4. Java运行时内存划分与垃圾回收--以及类加载机制基础

    ----JVM运行时内存划分----不同的区域存储的内容不同,职责因为不同1.方法区:被线程共享,存储被JVM加载的类的信息,常量,静态变量等2.运行时常量池:属于方法区的一部分,存放编译时期产生的字 ...

  5. C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...

  6. VC++中的C运行时库浅析(控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    1.概论 运行时库是程序在运行时所需要的库文件,通常运行时库是以LIB或DLL形式提供的.C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于 ...

  7. Apache Flink 分布式运行时环境

    Tasks and Operator Chains(任务及操作链) 在分布式环境下,Flink将操作的子任务链在一起组成一个任务,每一个任务在一个线程中执行.将操作链在一起是一个不错的优化:它减少了线 ...

  8. Java内存区域(运行时数据区域)详解、JDK1.8与JDK1.7的区别

    2.1 概述 对Java程序员来说,在虚拟机自动内存管理机制的帮助下,不再需要为每个对象的new操作去写配对的delete/free 代码,不容易出现内存泄露和内存溢出的问题.不过,仍然需要Java虚 ...

  9. JVM学习-运行时数据区域

    目录 前言 运行时数据区 程序计数器 Java虚拟机栈 局部变量表 基础数据类型 对象引用 returnAddress 操作数栈 动态链接 方法返回地址 Java堆 方法区 类型信息 字段描述符 方法 ...

随机推荐

  1. ODBC连接MySQL出现"E_FAIL"错误

    ODBC不能处理这种格式的数据:0000-00-00,将其更新为正常的时间即可解决

  2. 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记2 Xcode、Auto Layout及MVC

    原文链接不知道在哪, 接着上一话来讲,上一话中讲到了MVC,那么MVC在IOS8开发中是如何应用的呢?Paul Hegarty老师给我们展示了一个计算器的Demo,首先新建一个工程,老师把AppDel ...

  3. 九度OJ 1348 数组中的逆序对 -- 归并排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1348 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求 ...

  4. C语言使用中的细节问题总结

    1.在使用bool关键字时,出现"error:'bool' undeclared(first use in this function)"的错误,原因为C语言本身是没有bool关键 ...

  5. 【HeadFirst设计模式】9.迭代器与组合模式

    迭代器: 定义: 提供一种方法,顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示.(不让你知道我内部是如何聚合的) 把游走的任务放在迭代器上,而不是聚合上.这样简化了聚合的接口和实现,也让责任 ...

  6. shell环境

    1 引言 一个进程运行在shell环境中,理解进程运行的环境是十分重要的.环境影响着进程的行为,利用环境提供的便利,可以极大地提高开发效率.本节深入讨论shell中与进程有关的环境问题,包括命令行参数 ...

  7. gdb提示Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6_5.2.x86_64

    用gdb debugc代码的时候弹出这个错误 Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6_5.2. ...

  8. 007.Compiled

    Delphi property Compiled: Boolean read FCompiled; 类型:property 可见性:public 所在单元:System.RegularExpressi ...

  9. Python环境搭建(windows)

    Python环境搭建(windows) Python简介 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象.直译式计算机编程语言,具有近二十年的发展历史,成 ...

  10. HBase Shell(转)

    HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”.HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建.删 ...