日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

WinForm 程序的界面多语言切换

發布時間:2025/7/25 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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????????SetLang#region?SetLang
?2????????/**////?<summary>
?3????????///?設置當前程序的界面語言
?4????????///?</summary>
?5????????///?<param?name="lang">language:zh-CN,?en-US</param>
?6????????///?<param?name="form">窗體實例</param>
?7????????///?<param?name="formType">窗體類型</param>

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

17????????}

18
19????????AppLang?for?control#region?AppLang?for?control
20????????/**////?<summary>
21????????///?遍歷窗體所有控件,針對其設置當前界面語言
22????????///?</summary>
23????????///?<param?name="control"></param>
24????????///?<param?name="resources"></param>

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

37????????????????}

38????????????}

39
40????????????foreach?(Control?c?in?control.Controls)
41????????????{
42????????????????resources.ApplyResources(c,?c.Name);
43????????????????AppLang(c,?resources);
44????????????}

45????????}

46????????#endregion

47
48????????AppLang?for?menuitem#region?AppLang?for?menuitem
49????????/**////?<summary>
50????????///?遍歷菜單
51????????///?</summary>
52????????///?<param?name="item"></param>
53????????///?<param?name="resources"></param>

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

66????????????????}

67????????????}

68????????}

69????????#endregion

70
71????????#endregion


?

轉載于:https://www.cnblogs.com/luqingfei/archive/2007/03/14/674035.html

總結

以上是生活随笔為你收集整理的WinForm 程序的界面多语言切换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。