Posted on 2006-04-09 13:41
Programmer 阅读(2765)
评论(12) 编辑 收藏
方法二:
下面介绍一种只需对现有代码做较小改动的方法。
在 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();
- 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;
- 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();
- 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;
- 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;
-
- Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid);
-
- ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
- resources.ApplyResources(myButton, "myButton");
- resources.ApplyResources(this, "$this");
- }
当程序运行的时候,点击窗体上的 myButton 按钮,窗体的界面显示语言就会在英语和简体中文之间互相切换。
Feedback
请教一下:
不知道楼主这个切换是什么意思,如果按钮上写的是“china”,切换后会成为“中国”吗
我按照楼主的例子,怎么得不到正确结果,是不是要设置什么东西(resx)
把你代码发给我看看吧...
资源文件是肯定需要的。比如上面提到的两种语言:英语和中文,那么在visual stutio里面至少需要两个资源文件(.resx)。比如: MyForm.resx 和 MyForm.zh-CHS.resx。在 MyForm.resx 里面设置英语需要用到的属性(包括显示文字,图片,大小等控件属性),而在 MyForm.zh-CHS.resx 里面设置中文需要用到的属性。
已经可以了,我是没理解resources.ApplyResources第二个参数是什么东西,原来是控件的名字。
谢谢楼主。
我是想做简繁两个版本的。
像
ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
resources.ApplyResources(myButton, "myButton");
resources.ApplyResources(this, "$this");
这些代码应该放什么地方比较合适,是load吗
。
对我要完成的这两个语言版本,楼主认为怎么做最好,有什么建议。
谢谢。
如果只是从简单地完成你所述功能的话,那么你需要两步来完成:
1. 在 InitializeComponent 方法中写这些代码:
ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
resources.ApplyResources(myButton, "myButton");
resources.ApplyResources(this, "$this");
2. 在需要切换界面显示语言的时候,再次调用上述的代码。
稍微的一个改进,就是使用重构将这些代码抽出来放到一个新的方法中,然后第一步和第二步都调用这个方法就可以了。
当然我觉得还有另外一个方法,在有些情况下还是挺有用的。就是在 InitializeComponent 方法中调用 ApplyResources 的时候,我们可以将相应控件和要使用的资源名称保存起来。比如:
private static Dictionary<Control, string> ResourceTable = new Dictionary<Control, string>();
private void InitializeComponent(){
ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
resources.ApplyResources(myButton, "myButton");
ResourceTable[myButton] = "myButton";
resources.ApplyResources(this, "$this");
ResourceTable[this] = "$this";
}
private void ReapplyResources(){
ComponentResourceManager resources;
resources = new ComponentResourceManager(typeof(MyForm));
foreach( KeyValuePair<Control, string> kvp in ResourceTable){
resources.ApplyResources(kvp.Key, kvp.Value);
}
}
如果一个窗体上有太多的空间和文字,会不会太麻烦了?
不会呀。我觉得这样做还要简单些吧。即使你不像这么用,那你就得直接用代码设置每个控件的属性,代码应该还会多些的!
My.Application.ChangeCulture 方法可以直接该所有资源相关的东西,不知道C#里面有没有对应的方法
楼主,你好.我看了此篇文章后,深受启发.
打算研究下源码,发现无法下载.能传我一份不?谢谢
xiyang-0@163.com
诚招zen-cart程序
1、两年以上的 PHP或asp系统开发经历。具备独立完成PHP5 + MySQL5 的开
发能力;
2、具有较强的独立工作能力,有创新精神,工作踏实、主动积极、善于学习和沟通、逻辑性强,具备严谨的代码编程习惯及较强的文档编写能力,团队协作能力强。
3.熟悉SEO的规范,能够编写符合规范的CSS、HTML代码会zen-cart者优先。
请相信:我们会给您提供一份具有良好发展前途,办公环境优美,富有激情和战斗力的团队氛围以及丰厚的薪金待遇和充分的能力发挥的工作!
工作地区:开封
工资待遇:面议
性别要求:不限
联系电话:姜小姐 18964757198
联系邮箱:info@hisou.net
通讯地址:河南省开封市京西宾馆西五十米长城雅园B座506室
诚招zen-cart程序
1、两年以上的 PHP或asp系统开发经历。具备独立完成PHP5 + MySQL5 的开
发能力;
2、具有较强的独立工作能力,有创新精神,工作踏实、主动积极、善于学习和沟通、逻辑性强,具备严谨的代码编程习惯及较强的文档编写能力,团队协作能力强。
3.熟悉SEO的规范,能够编写符合规范的CSS、HTML代码会zen-cart者优先。
请相信:我们会给您提供一份具有良好发展前途,办公环境优美,富有激情和战斗力的团队氛围以及丰厚的薪金待遇和充分的能力发挥的工作!
工作地区:开封
工资待遇:面议
性别要求:不限
联系电话:姜小姐 18964757198
联系邮箱:info@hisou.net
通讯地址:河南省开封市京西宾馆西五十米长城雅园B座506室