日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

WinForm 程序的界面多语言切换

發(fā)布時(shí)間:2025/7/25 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WinForm 程序的界面多语言切换 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下面介紹一種只需對(duì)現(xiàn)有代碼做較小改動(dòng)的方法。

在 Visual Studio 的設(shè)計(jì)視圖中,如果在 Properties 窗口中改變了程序的默認(rèn)界面語(yǔ)言(Language),我們會(huì)注意到無(wú)論是工程還是窗體對(duì)應(yīng)的 .Designer.cs 文件都會(huì)有顯著的改變。比如,我們創(chuàng)建一個(gè)叫 MyForm 的 form,并且添加一個(gè)叫 MyButton 的按鈕。

在改變窗體 Properties 中的 Language 屬性之前, .Designer.cs 代碼文件中的 InitializeComponent 方法的內(nèi)容大致如下:

  • 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 屬性之后,工程中除了默認(rèn)的 .resx 文件之外,還會(huì)自動(dòng)添加一個(gè) .zh-CHS.resx 文件(假設(shè)我們將 Language 改變?yōu)?Chinese (Simplified))。另外,.Designer.cs 文件中的 InitializeComponent 方法也會(huì)改變成:

  • 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");

另外,設(shè)置控件屬性(比如顯示文字 Text,控件大小 Size,顯示位置 Location 等)的代碼都沒有了。也就是說(shuō)設(shè)置控件屬性的代碼都是由 resources.ApplyResource 方法來(lái)完成的。那么在我們想改變 WinForm 程序的界面顯示語(yǔ)言的時(shí)候,能不能直接調(diào)用 ApplyResources 方法呢?答案是肯定的。

?

為 myButton 添加 Click 事件的事件處理函數(shù):

  • 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");
  • }

?

當(dāng)程序運(yùn)行的時(shí)候,點(diǎn)擊窗體上的 myButton 按鈕,窗體的界面顯示語(yǔ)言就會(huì)在英語(yǔ)和簡(jiǎn)體中文之間互相切換。


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

?1????????SetLang#region?SetLang
?2????????/**////?<summary>
?3????????///?設(shè)置當(dāng)前程序的界面語(yǔ)言
?4????????///?</summary>
?5????????///?<param?name="lang">language:zh-CN,?en-US</param>
?6????????///?<param?name="form">窗體實(shí)例</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????????///?遍歷窗體所有控件,針對(duì)其設(shè)置當(dāng)前界面語(yǔ)言
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


?

轉(zhuǎn)載于:https://www.cnblogs.com/luqingfei/archive/2007/03/14/674035.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。