您的位置:首页 > 其它

WinForm 界面的多语言切换

2009-06-30 18:18 197 查看
下面介绍一种只需对现有代码做较小改动的方法。

在 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 按钮,窗体的界面显示语言就会在英语和简体中文之间互相切换。

代码:

Code
1
2 private void Form9_Load(object sender, EventArgs e)
3 {
4 pcd.WinControl.MulitLanguage.SetLang(this, typeof(Form9));
5 }
6
7 private void button1_Click(object sender, EventArgs e)
8 {
9 if (pcd.WinControl.MulitLanguage.CurLanguge == pcd.WinControl.Languge.Chines)
10 {
11 MessageBox.Show("请先选择", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
12 }
13 else if (pcd.WinControl.MulitLanguage.CurLanguge == pcd.WinControl.Languge.English)
14 {
15 MessageBox.Show("Please Select before Submit", "System Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
16 }
17 }
18 /// <summary>
19 /// 语言选择
20 /// </summary>
21 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
22 {
23 switch (this.comboBox1.SelectedIndex)
24 {
25 case 0:
26 pcd.WinControl.MulitLanguage.SetLang("zh-CN", this, typeof(Form8));
27
28 break;
29 case 1:
30 pcd.WinControl.MulitLanguage.SetLang("en-US", this, typeof(Form8));
31
32 break;
33 default:
34 pcd.WinControl.MulitLanguage.SetLang("zh-CN", this, typeof(Form8));
35 break;
36 }
37 }
38

MSDN:如何:为 Windows 窗体全球化设置区域性和用户界面的区域性

Visual Basic 或 Visual C# 应用程序的两个区域性值确定了为应用程序加载哪些资源以及如何设置像货币、数字和日期这样的信息的格式。加载的资源由 UI 区域性设置确定,而格式设置选项由区域性设置确定。应用程序首先将在以下位置查找区域性值:CurrentCultureCurrentUICulture 属性。可按下面的过程所示在代码中设置这些值。

CurrentCulture 属性的默认值是操作系统的用户区域设置,它在“区域选项”控制面板中设置。CurrentUICulture 属性的默认值是操作系统的用户界面 (UI) 语言,即您的操作系统用户界面所使用的语言。在 Windows 2000 和 Windows XP MultiLanguage Edition 上,CurrentUICulture 默认为当前用户 UI 语言设置。

在一些情况下,您可能要根据操作系统或用户的区域性设置更改大部分应用程序,但保留数字或日期不更改。您可以用区域性特定类通过固定区域性设置信息的格式,固定区域性与英语语言相关联,但没有特定的区域。有关这些类的更多信息,请参见不同区域性的格式设置和 System.Globalization。有关固定区域性的更多信息,请参见 InvariantCulture。有关可能有的区域性设置的信息,请参见 CultureInfo。

设置适合于特定区域性的格式设置选项

如果要重写用户或操作系统的设置,可以设置 CurrentCulture 和 CurrentUICulture 属性。

通常,您想要指定一个区域性,以便应用程序用户界面的每一部分都适合于该区域性。因此,您必须在调用 InitializeComponent 方法之前设置该区域性。


复制代码
' Visual Basic
' Put the Imports statements at the beginning of the code module
Imports System.Threading
Imports System.Globalization
' Put the following code before InitializeComponent()
' Sets the culture to French (France)
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR")
' Sets the UI culture to French (France)
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR")

// C#
// Put the using statements at the beginning of the code module
using System.Threading;
using System.Globalization;
// Put the following code before InitializeComponent()
// Sets the culture to French (France)
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
// Sets the UI culture to French (France)
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");

注意
区域性值必须始终是特定区域性的(如“fr-FR”),而不是非特定区域性的(如“fr”)。这样做的原因在于非特定区域性(例如“fr”)可以应用于所有讲法语的区域性,但在法国、比利时和魁北克(加拿大)使用不同的货币。

对于无论 CurrentCulture 属性的值如何设置都应不更改显示的任何字符串,用固定区域性调用格式设置方法。


复制代码
' Visual Basic
Dim MyInt As Integer = 100
Dim MyString As String = MyInt.ToString("C", CultureInfo.InvariantCulture)
MessageBox.Show(MyString)

// C#
int MyInt = 100;
string MyString = MyInt.ToString("C", CultureInfo.InvariantCulture);
MessageBox.Show(MyString);

// set culture for formatting
Thread.CurrentThread.CurrentCulture = ci;

// set culture for resources
Thread.CurrentThread.CurrentUICulture = ci;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: