柚子快報(bào)激活碼778899分享:C# 界面控件中英切換
柚子快報(bào)激活碼778899分享:C# 界面控件中英切換
編程軟件:VS 2015
需求:界面有兩個(gè)按鈕,點(diǎn)擊可以將界面上所有控件進(jìn)行不同語(yǔ)言的切換。
一共兩種方案,個(gè)人認(rèn)為第二種方案使用范圍更廣(這里以中英文切換為例)。
方案一:如圖所示,建立兩個(gè)資源文件
將所需控件的中英文分別填入對(duì)應(yīng)的資源文件中,如下圖所示:
代碼如下:
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);//調(diào)用
}
//遞歸應(yīng)用到控件
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);
}
}
}
創(chuàng)建兩個(gè)按鈕,分別是中文和英文,將方法引用就可以了。
private void 中文ToolStripMenuItem_Click(object sender, EventArgs e)
{
中文按鈕是否觸發(fā) = true;
英文按鈕是否觸發(fā) = false;
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh");//中文
ApplyResource(注冊(cè)界面對(duì)象);//傳入當(dāng)前的界面
}
private void 英文ToolStripMenuItem_Click(object sender, EventArgs e)
{
英文按鈕是否觸發(fā) = true;
中文按鈕是否觸發(fā) = false;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en");//英文
}
該方法的缺點(diǎn)在于:界面布局不能自適應(yīng),如果英文名和中文名長(zhǎng)度不一致就會(huì)導(dǎo)致布局發(fā)生變化,控件相互遮擋的情況。
?方案二:如圖所示,將界面的Language改為英語(yǔ)
這樣的操作就有有兩個(gè)注冊(cè)界面,一個(gè)中文的,一個(gè)英文的,可以各自調(diào)整各自的布局。切換不同的語(yǔ)言,顯示不同的界面(界面上的控件翻譯自己來(lái))
方案一和二完全不摻和,方案二不需要建立資源文件!
代碼(可以在界面加載的時(shí)候從新定義界面大小):
這個(gè)代碼適用于控件嵌套控件
public void 英文()
{
this.Text = "Registration interface";
this.Size = new Size(1001, 669);
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en
遍歷界面控件(this);
}
public void 中文()
{
this.Text = "注冊(cè)界面";
this.Size = new Size(1001, 669);
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh"); //英文是en
遍歷界面控件(this);
}
private void 遍歷界面控件(Control fatherControl)
{
//結(jié)果:能獲取到絕大多數(shù)控件
//問(wèn)題:Timer、ContextMenuStrip等控件獲取不到
ComponentResourceManager resources = new ComponentResourceManager(typeof(注冊(cè)界面));
Control.ControlCollection sonControls = fatherControl.Controls;
foreach (Control control in sonControls)
{
if (control.Controls != null)
{
遍歷界面控件(control);
resources.ApplyResources(control, control.Name);
}
}
}
如果各個(gè)控件之間不存在嵌套關(guān)系,可以用如下代碼:
public void 英文()
{
this.Text = "Input settings";
this.Size = new Size(444, 340);
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en
ComponentResourceManager resources = new ComponentResourceManager(typeof(注冊(cè)界面));
foreach (Control ct in this.Controls)//循環(huán)當(dāng)前界面所有的控件(但是遍歷不到控件中包含控件的控件)
{
resources.ApplyResources(ct, ct.Name);
if (ct.HasChildren)
{
resources.ApplyResources(ct, ct.Name);
}
}
}
public void 中文()
{
this.Text = "輸入設(shè)置";
this.Size = new Size(297, 343);
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh"); //英文是en
ComponentResourceManager resources = new ComponentResourceManager(typeof(注冊(cè)界面));
foreach (Control ct in this.Controls)//循環(huán)當(dāng)前界面所有的控件
{
resources.ApplyResources(ct, ct.Name);
if (ct.HasChildren)
{
resources.ApplyResources(ct, ct.Name);
}
}
}
創(chuàng)建兩個(gè)按鈕,分別是中文和英文,將方法引用就可以了。
private void zhToolStripMenuItem_Click(object sender, EventArgs e)
{
中文按鈕是否觸發(fā) = true;
英文按鈕是否觸發(fā) = false;
注冊(cè)界面對(duì)象.中文();
}
private void enToolStripMenuItem_Click(object sender, EventArgs e)
{
英文按鈕是否觸發(fā) = true;
中文按鈕是否觸發(fā) = false;
注冊(cè)界面對(duì)象.英文();
}
?
柚子快報(bào)激活碼778899分享:C# 界面控件中英切換
好文鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。