在运行时切换 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 按钮,窗体的界面显示语言就会在英语和简体中文之间互相切换。
转载于:https://www.cnblogs.com/1175429393wljblog/p/4583198.html
在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources...相关推荐
- #CodeSnippets:在运行时切换Vive的前置摄像头和“ Tron模式”
How to programmatically turn the front facing camera video and camera for chaperone bounds on and of ...
- vs运行时出现应用程序无法正常启动0xc000007
opencv运行时出现应用程序无法正常启动0xc000007b vs2015配置opencv的时候,编译可以过,但是运行就会出现下图的错误,试了网上很多种方法,但是都失败了,经过一天的不懈努力,在砸电 ...
- mysql与mangodb多租户_MongoDB多租户(Java):如何使用MongoClient在运行时切换具有不同数据库凭据的MongoDB数据库?...
我正面临一个关于MongoDB多租户的问题.我有两个不同的mongoDB数据库(db1和db2).这两者都有不同的凭据. db1凭据: userName:admin 密码:passwd db2凭据: ...
- java虚拟机能自动处理 异常_对于非运行时异常,程序中一般可不做处理,由java虚拟机自动...
1.Java异常机制 Java把异常当做对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类.Java中的异常分为两大类:错误Error和异常Exception,Java ...
- VS运行时无法定位程序输入点xxx于动态链接库xxx.exe上
对于这个问题在百度上百度了好久,都没有解决,百度上很多都是 无法定位程序输入点xxx于动态链接库xxx.dll上,为了解决这个问题真是走了好多坑,花费了几天时间,试了无数的解决方案,为了后面的uu在这 ...
- 大数据_Flink_数据处理_运行时架构7_程序结构和数据流图---Flink工作笔记0022
然后我们再来看实际上,flink对数据的处理,反应到代码上可以看到都是对DataStream的处理对吧,然后我们看,可以分成3个部分,一个是source部分,这里就是对源数据流的读取, 然后trans ...
- WinForm 程序的界面多语言切换
下面介绍一种只需对现有代码做较小改动的方法. 在 Visual Studio 的设计视图中,如果在 Properties 窗口中改变了程序的默认界面语言(Language),我们会注意到无论是工程还是 ...
- 程序运行时的存储组织及管理
目录 程序运行时的存储组织及管理概述 静态存储分配 临时变量的地址分配 简单栈式动态存储 嵌套过程语言的栈式实现 display 表方法 存取链(静态链)方法 参数传递 程序运行时的存储组织及管理概述 ...
- 在Java程序中打印java运行时参数
在Java程序中打印java运行时参数 本文是基于CentOS 7.3系统环境,进行java的学习和使用 CentOS 7.3 1. java运行时参数 Java程序在运行时,主要分为两大类参数,一类 ...
最新文章
- PyQt主窗体设置停靠窗口(QDockWidget)的叠加顺序
- HTML5会砸掉iOS和Android开发者的饭碗么?
- (0017)iOS 开发之Mac上安装Eclipse、创建java后台程序访问本地数据库
- 在ASP.NET MVC 模型中 选择最好的方法将多个model(数据模型)传递到视图
- 存货编码数字_用友T3软件存货编码与存货代码有什么不同?
- HTML发布那一年,开发版内测公告一般发布时间是几点
- BZOJ 3907: 网格( 组合数 + 高精度 )
- POJ - 2528 Mayor's posters(线段数+离散化)
- service注入为null_如何解决quartz调度时候,job中的service为null的问题?
- SpringAOP+自定义注解实现日志功能
- CCNP路由重分发(四)EIGRP-to-ISIS
- 关于Multi-bin实现的一些翻译
- 全国行政区划变更 (2010~2019)
- 仓库货位卡标识牌_怎样正确使用仓库货位卡?来看看这三点
- 快速理解旋转运动公式
- 微信好友只有昵称没有微信号_没微信号能找到人吗 只有微信昵称怎么找人
- 解决屏幕大小不一导致页面下方出现多余空白的问题
- 超声成像发射声场仿真(Ultrasound Emit Field Simulation)
- 大气颗粒物 PMF 源解析
- 成都比较好的建筑设计公司有哪些?
热门文章
- 菜鸟学习C++练笔之整理搜狗2008版语料库--获取分类语料库
- 问题 K: 输出月历 山东科技大学OJ
- c++ vector 赋值_数据分析:R篇 | 变量赋值、计算、画图、基本检验
- python如何制作一个工程软件_使用python制作一个解压缩软件
- Xcode新建View Controller Scene并实现界面间跳转的方法
- Graph Embedding:word2vec更进一步?
- 05NumPy--5.3数组运算
- 俄国防部《红星电视台》公布一个俄军“监督”下的大型加密货币矿场
- SUSHI区块奖励将根据此前提案在3月降至每区块40枚
- SAP License:如何获得成为SAP顾问能力?