WinForm 程序的界面多语言切换

下面介绍一种只需对现有代码做较小改动的方法。

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


以下是有可能用的帮助方法

 1 ContractedBlock.gif ExpandedBlockStart.gif          SetLang #region SetLang
 2ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 3InBlock.gif        /// 设置当前程序的界面语言
 4InBlock.gif        /// </summary>
 5InBlock.gif        /// <param name="lang">language:zh-CN, en-US</param>
 6InBlock.gif        /// <param name="form">窗体实例</param>
 7ExpandedSubBlockEnd.gif        /// <param name="formType">窗体类型</param>

 8InBlock.gif        public static void SetLang(string lang, Form form, Type formType)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
11InBlock.gif            if (form != null)
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
14InBlock.gif                resources.ApplyResources(form, "$this");
15InBlock.gif                AppLang(form, resources);
16ExpandedSubBlockEnd.gif            }

17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19ContractedSubBlock.gifExpandedSubBlockStart.gif        AppLang for control#region AppLang for control
20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
21InBlock.gif        /// 遍历窗体所有控件,针对其设置当前界面语言
22InBlock.gif        /// </summary>
23InBlock.gif        /// <param name="control"></param>
24ExpandedSubBlockEnd.gif        /// <param name="resources"></param>

25InBlock.gif        private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            if (control is MenuStrip)
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                resources.ApplyResources(control, control.Name);
30InBlock.gif                MenuStrip ms = (MenuStrip)control;
31InBlock.gif                if (ms.Items.Count > 0)
32ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
33InBlock.gif                    foreach (ToolStripMenuItem c in ms.Items)
34ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
35InBlock.gif                        AppLang(c, resources);
36ExpandedSubBlockEnd.gif                    }

37ExpandedSubBlockEnd.gif                }

38ExpandedSubBlockEnd.gif            }

39InBlock.gif
40InBlock.gif            foreach (Control c in control.Controls)
41ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
42InBlock.gif                resources.ApplyResources(c, c.Name);
43InBlock.gif                AppLang(c, resources);
44ExpandedSubBlockEnd.gif            }

45ExpandedSubBlockEnd.gif        }

46ExpandedSubBlockEnd.gif        #endregion

47InBlock.gif
48ContractedSubBlock.gifExpandedSubBlockStart.gif        AppLang for menuitem#region AppLang for menuitem
49ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
50InBlock.gif        /// 遍历菜单
51InBlock.gif        /// </summary>
52InBlock.gif        /// <param name="item"></param>
53ExpandedSubBlockEnd.gif        /// <param name="resources"></param>

54InBlock.gif        private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56InBlock.gif            if (item is ToolStripMenuItem)
57ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
58InBlock.gif                resources.ApplyResources(item, item.Name);
59InBlock.gif                ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
60InBlock.gif                if (tsmi.DropDownItems.Count > 0)
61ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
62InBlock.gif                    foreach (ToolStripMenuItem c in tsmi.DropDownItems)
63ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
64InBlock.gif                        AppLang(c, resources);
65ExpandedSubBlockEnd.gif                    }

66ExpandedSubBlockEnd.gif                }

67ExpandedSubBlockEnd.gif            }

68ExpandedSubBlockEnd.gif        }

69ExpandedSubBlockEnd.gif        #endregion

70InBlock.gif
71ExpandedBlockEnd.gif        #endregion


 

转载于:https://www.cnblogs.com/luqingfei/archive/2007/03/14/674035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值