写出来之后有点累了,待日后再去学习更好的了
应用类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.Globalization;
namespace 中英文界面
{
public partial class FormMain : Form
{
LanguageUtil language;
public FormMain()
{
InitializeComponent();
}
private void TSMIChinese_Click(object sender, EventArgs e)
{
language = new LanguageUtil(this,"zh");
}
private void TSMIEnglish_Click(object sender, EventArgs e)
{
language = new LanguageUtil(this, "en");
}
}
}
工具类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Collections;
using System.Windows.Forms;
namespace 中英文界面
{
class LanguageUtil
{
public Control control;
private ResourceManager resourceManager;
public LanguageUtil(Control con,string language)
{
control = con;
LoadResources(language);
}
private void LoadResources(string culture)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
// 创建 ResourceManager 对象并指定资源文件的基本名称和程序集
resourceManager = new ResourceManager("中英文界面.FormMain", typeof(FormMain).Assembly);
UpdateUI();
}
private void UpdateUI()
{
control.Text = resourceManager.GetString("this.Text");
// 获取资源的 ResourceSet
ResourceSet resourceSet = resourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
// 遍历资源集中的每个资源
if (resourceSet != null)
{
foreach (DictionaryEntry entry in resourceSet)
{
TraverseControls(control, entry.Key.ToString(), false);
}
}
}
private void TraverseControls(Control parentControl, string controlName, bool flag)
{
string name = controlName.Split('.')[0];
if (flag) return;
foreach (Control control in parentControl.Controls)
{
if (control.Name.Equals(name))
{
flag = true;
control.Text = resourceManager.GetString(controlName);
}
if (flag) return;
// 如果当前控件还包含子控件,递归遍历
if (control.HasChildren)
{
TraverseControls(control, controlName, flag);
}
}
}
}
}