using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Rs.MotionPlat.Commom { public class ControlLanguageHelper { public static string DefaultLanguage = "zh-CN"; /// /// 修改默认语言 /// /// 待设置默认语言 public static void SetDefaultLanguage(string lang) { System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); DefaultLanguage = lang; } /// /// 加载语言 /// /// 加载语言的窗口 /// 窗口的类型 public static void LoadLanguage(Control form, Type formType) { if (form != null) { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType); resources.ApplyResources(form, "$this"); Loading(form, resources); } } /// /// 加载语言 /// /// 控件 /// 语言资源 private static void Loading(Control control, System.ComponentModel.ComponentResourceManager resources) { if (control is MenuStrip) { //将资源与控件对应 resources.ApplyResources(control, control.Name); MenuStrip ms = (MenuStrip)control; if (ms.Items.Count > 0) { foreach (ToolStripMenuItem c in ms.Items) { //遍历菜单 Loading(c, resources); } } } foreach (Control c in control.Controls) { try { resources.ApplyResources(c, c.Name); Loading(c, resources); } catch (Exception) { } } } /// /// 遍历菜单 /// /// 菜单项 /// 语言资源 private static void Loading(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources) { if (item is ToolStripMenuItem) { resources.ApplyResources(item, item.Name); ToolStripMenuItem tsmi = (ToolStripMenuItem)item; if (tsmi.DropDownItems.Count > 0) { foreach (ToolStripMenuItem c in tsmi.DropDownItems) { Loading(c, resources); } } } } } }