標籤:多語言 winform c#
摘要:本文旨在實現對winForm程式的多語言設定,包含自訂控制項的多語言設定。
實現步驟
1.添加
using System.Threading;
using System.Globalization;
2.設定winForm 程式Localizable屬性為true
更改Language屬性為要改變的語言,在設計介面對需要更改語言的控制項變更,解決方案資源管理中會自動產生相應的資源設定檔,*.zh-CN.resx(中文) *.en.resx(英文)。語言代碼錶見備忘。
3.設定兩個Button,分別為中文,英文
Click事件:
private void 英語ToolStripMenuItem_Click(object sender, EventArgs e){Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en"); //應用資源檔,en為英語,zh-CN為中文ApplayResource(); //ApplayResource()中執行資源應用}private void ApplayResource() //應用資源檔{System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(typeof(Form1)); //擷取Form1 的資源管理員foreach (Control ctl in Controls)//迴圈遍曆控制項Controlsd對{res.ApplyResources(ctl, ctl.Name); //應用ctl的ctl.Name資源設定檔;}foreach (ToolStripMenuItem item in this.menuStrip1.Items){res.ApplyResources(item, item.Name);foreach (ToolStripMenuItem subitem in item.DropDownItems){res.ApplyResources(subitem,subitem.Name);}}//由於res中不包含userControl1中資源需要重新引用資源檔//對每個新的視窗都需重新設定resSystem.ComponentModel.ComponentResourceManager res1 = new ComponentResourceManager(typeof(userControl1)); //擷取userControl1的資源管理員foreach (Control item in userControl12.Controls) //對自己建立的空間進行遍曆{res1.ApplyResources(item, item.Name);}}
Foreach對控制項進行遍曆,遍曆後應用res.ApplayResource(item,item.Name);對item控制項的item.Name 屬性進行應用資源檔。
備忘資訊:
語言代碼錶網址:http://www.lingoes.cn/zh/translator/langcode.htm
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
WinForm軟體多語言版本實現