編程軟件:VS 2015
需求:界面有兩個按鈕,點擊可以將界面上所有控件進行不同語言的切換。
一共兩種方案,個人認為第二種方案使用范圍更廣(這里以中英文切換為例)。
方案一:如圖所示,建立兩個資源文件
將所需控件的中英文分別填入對應的資源文件中,如下圖所示:
代碼如下:
public void ApplyResource(Control control){switch (Thread.CurrentThread.CurrentCulture.Name){case "en":crm = new ComponentResourceManager(typeof(Resource.Resource_en));break;case "zh":crm = new ComponentResourceManager(typeof(Resource.Resource_zh));break;default:crm = new ComponentResourceManager(typeof(Resource.Resource_zh));break;}applyControl(control.GetType().Name, control);//調用}//遞歸應用到控件private void applyControl(string topName, Control control){foreach (Control ctl in control.Controls){crm.ApplyResources(ctl, topName + "." + ctl.Name, Thread.CurrentThread.CurrentCulture);if (ctl.HasChildren){applyControl(topName, ctl);}}}
創建兩個按鈕,分別是中文和英文,將方法引用就可以了。
private void 中文ToolStripMenuItem_Click(object sender, EventArgs e){中文按鈕是否觸發 = true;英文按鈕是否觸發 = false;Thread.CurrentThread.CurrentCulture = new CultureInfo("zh");//中文ApplyResource(注冊界面對象);//傳入當前的界面}private void 英文ToolStripMenuItem_Click(object sender, EventArgs e){英文按鈕是否觸發 = true;中文按鈕是否觸發 = false;Thread.CurrentThread.CurrentCulture = new CultureInfo("en");//英文}
該方法的缺點在于:界面布局不能自適應,如果英文名和中文名長度不一致就會導致布局發生變化,控件相互遮擋的情況。
?方案二:如圖所示,將界面的Language改為英語
這樣的操作就有有兩個注冊界面,一個中文的,一個英文的,可以各自調整各自的布局。切換不同的語言,顯示不同的界面(界面上的控件翻譯自己來)
方案一和二完全不摻和,方案二不需要建立資源文件!
代碼(可以在界面加載的時候從新定義界面大小):
這個代碼適用于控件嵌套控件
public void 英文(){this.Text = "Registration interface";this.Size = new Size(1001, 669);Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en遍歷界面控件(this);}public void 中文(){this.Text = "注冊界面";this.Size = new Size(1001, 669);Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh"); //英文是en遍歷界面控件(this);}private void 遍歷界面控件(Control fatherControl){//結果:能獲取到絕大多數控件//問題:Timer、ContextMenuStrip等控件獲取不到ComponentResourceManager resources = new ComponentResourceManager(typeof(注冊界面));Control.ControlCollection sonControls = fatherControl.Controls;foreach (Control control in sonControls){if (control.Controls != null){遍歷界面控件(control);resources.ApplyResources(control, control.Name);}}}
如果各個控件之間不存在嵌套關系,可以用如下代碼:
public void 英文(){this.Text = "Input settings";this.Size = new Size(444, 340);Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是enComponentResourceManager resources = new ComponentResourceManager(typeof(注冊界面));foreach (Control ct in this.Controls)//循環當前界面所有的控件(但是遍歷不到控件中包含控件的控件){resources.ApplyResources(ct, ct.Name);if (ct.HasChildren){resources.ApplyResources(ct, ct.Name);}}}public void 中文(){this.Text = "輸入設置";this.Size = new Size(297, 343);Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh"); //英文是enComponentResourceManager resources = new ComponentResourceManager(typeof(注冊界面));foreach (Control ct in this.Controls)//循環當前界面所有的控件{resources.ApplyResources(ct, ct.Name);if (ct.HasChildren){resources.ApplyResources(ct, ct.Name);}}}
創建兩個按鈕,分別是中文和英文,將方法引用就可以了。
private void zhToolStripMenuItem_Click(object sender, EventArgs e){中文按鈕是否觸發 = true;英文按鈕是否觸發 = false;注冊界面對象.中文();}private void enToolStripMenuItem_Click(object sender, EventArgs e){英文按鈕是否觸發 = true;中文按鈕是否觸發 = false;注冊界面對象.英文();}
?