在运行时切换 WinForm 程序的界面语言 ---------多语言设置基础
2016-03-11 11:56
543 查看
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
相关文章推荐
- android 测试
- ALAssetsLibrary读取所有照片
- Myeclipse优化
- c++第一次实验——循环do~while
- get_magic_quotes_gpc() 对$_GET $_COOKIE $_POST转义处理
- 二维码图片生成(扩展知识:创建带圆角效果的图片)
- IPC机制---04 Android中的IPC通讯方式(D)
- maven中使用spring提示applicationContext.xml找不到
- 手机端的click
- 内存管理之堆和栈的区别
- C语言字符输入/输出函数(八)
- 内核内存泄露查找方式
- 微信红包随机算法初探
- CDOJ 1281 暴兵的卿学姐 构造题
- 使用xshell时中文横着显示的解决办法
- javascript中parseInt函数的用法
- win上传Linux文件乱码问题解决办法
- 初识C++ ——简单程序的编写
- Add Text To Image xamarin.ios
- 导出non-cdb作为一个pdb到cdb中。(change a non-cdb to a pdb)