Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-cn");
InitializeComponent();
InitializeComponent();
很显然,这种方法有一个致命的缺点,就是只能在程序运行初始阶段使用,一旦主界面被构造完成,那就不能再更改程序的运行语言了。
目前我想到的一个方法,就是遍历控件,将控件的资源重新应用一次,代码如下:
private void ApplyControl(Control parentControl, ComponentResourceManager resources)
{
if (parentControl.HasChildren)
{
foreach (Control control in parentControl.Controls)
{
ApplyControl(control, resources);
}
resources.ApplyResources(parentControl, parentControl.Name);
}
//else if ((parentControl as ToolStrip) != null && (parentControl as ToolStrip).Items.Count > 0)
//{
// foreach (ToolStripStatusLabel control in (parentControl as ToolStrip).Items)
// {
// resources.ApplyResources(parentControl, parentControl.Name);
// }
//}
else
{
resources.ApplyResources(parentControl, parentControl.Name);
}
}
private void toolStripMenuItemSelected(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(((ToolStripMenuItem)sender).Tag.ToString());
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
foreach (Control control in this.Controls)
{
ApplyControl(control, resources);
}
SetLanguageCheck();
}
private void SetLanguageCheck()
{
foreach (ToolStripMenuItem item in languageLToolStripMenuItem.DropDownItems)
{
if (item.Tag.ToString().ToLower() == Thread.CurrentThread.CurrentUICulture.Name.ToLower())
item.Checked = true;
else
item.Checked = false;
}
}
目前这个还是有一定的缺陷的,已知的就是对菜单不能实时更改语言,因为从菜单与子菜单的关系不是父子控件的关系。在代码中被我注释掉的就是想对菜单进行兼容处理的,但很可惜,没有成功。并且这种方法,可能对一些未知的不是父子关系的控件,也是束手无策的。{
if (parentControl.HasChildren)
{
foreach (Control control in parentControl.Controls)
{
ApplyControl(control, resources);
}
resources.ApplyResources(parentControl, parentControl.Name);
}
//else if ((parentControl as ToolStrip) != null && (parentControl as ToolStrip).Items.Count > 0)
//{
// foreach (ToolStripStatusLabel control in (parentControl as ToolStrip).Items)
// {
// resources.ApplyResources(parentControl, parentControl.Name);
// }
//}
else
{
resources.ApplyResources(parentControl, parentControl.Name);
}
}
private void toolStripMenuItemSelected(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(((ToolStripMenuItem)sender).Tag.ToString());
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
foreach (Control control in this.Controls)
{
ApplyControl(control, resources);
}
SetLanguageCheck();
}
private void SetLanguageCheck()
{
foreach (ToolStripMenuItem item in languageLToolStripMenuItem.DropDownItems)
{
if (item.Tag.ToString().ToLower() == Thread.CurrentThread.CurrentUICulture.Name.ToLower())
item.Checked = true;
else
item.Checked = false;
}
}
在网上搜了半天相关知识点,好像根本没有有价值的东西,或许是我使用的关键字不对吧。不过对于自己想到的暂时还是记下来的为好。