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. XMPP即时通讯(代码实现)

    1.配置XMPP(XMPPConfig.m) 2.配置XMPPFramework框架 3.创建单例类(XMPPManager.h/XMPPManager.m)管理器 XMPPManager.m: #i ...

  2. iOS开发——百度云推送

    由于公司项目是集成的极光推送,详见下一篇博客. 集成百度推送大体相当,最好都参考官方文档集成,官方文档或官方网站教程是最好的博客. 百度Push服务SDK用户手册(iOS版) http://push. ...

  3. WPF 程序中启动和关闭外部.exe程序

    当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...

  4. CUDA_矢量相加

    #include<iostream> #define N 10 _ _global_ _ void add(*a,*b,*c) { int tid=blockIdx.x; if(tid&l ...

  5. OpenJudge 2813 画家问题 / Poj 1681 Painter's Problem

    1.链接地址: http://bailian.openjudge.cn/practice/2813 http://poj.org/problem?id=1681 2.题目: 总时间限制: 1000ms ...

  6. [转]select模型的一种技巧运用-libevent

    参见网址 http://www.cppblog.com/xvsdf100/archive/2013/12/10/204689.html

  7. Linux C 程序 基础语法(1)

    1.Linux 下第一支C程序,控制台打印一句话. vi first.c //linux新建文件 #include<stdio.h> int main() { printf("w ...

  8. Linux CPU亲缘性详解

    前言 在淘宝开源自己基于nginx打造的tegine服务器的时候,有这么一项特性引起了笔者的兴趣.“自动根据CPU数目设置进程个数和绑定CPU亲缘性”.当时笔者对CPU亲缘性没有任何概念,当时作者只是 ...

  9. 关于$.fn

    今天看一篇文章,里面的一段代码出现了$.fn,第一次见到这样的写法,于是跑去问度娘...代码如下: $.fn.scrollUnique = function() { return $(this).ea ...

  10. JEECG开发总结

    一:datagrid列表 (1)时间:<t:dgCol title="创建时间" field="createtime" width="60&qu ...