扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
<?xml version="1.0" standalone="yes"?> <Language> <DefaultLanguage>ZH</DefaultLanguage> </Language> |
//读取默认语言 public static string ReadDefaultLanguage(){ XmlReader reader = new XmlTextReader("resources/LanguageDefine.xml"); XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode root = doc.DocumentElement; //选取DefaultLangugae节点 XmlNode node = root.SelectSingleNode("DefaultLanguage"); string result = "EN"; if (node != null) //取出节点中的内容 result = node.InnerText; reader.Close(); return result; } //修改默认语言 public static void WriteDefaultLanguage(string lang){ DataSet ds = new DataSet(); ds.ReadXml("resources/LanguageDefine.xml"); DataTable dt = ds.Tables["Language"]; dt.Rows[0]["DefaultLanguage"] = lang; ds.AcceptChanges(); ds.WriteXml("resources/LanguageDefine.xml"); } |
<?xml version="1.0" encoding="gb2312"?> <AppConfig> <Area> <Language>ZH</Language> <List> <Item>中文[ZH]</Item> <Item>英文[EN]</Item> </List> </Area> <Area> <Language>EN</Language> <List> <Item>Chinese[ZH]</Item> <Item>English[EN]</Item> </List> </Area> <Area> </AppConfig> |
public static IList GetLanguageList(string lang){ IList result = new ArrayList(); XmlReader reader = new XmlTextReader("resources/AppConfig.xml"); XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode root = doc.DocumentElement; XmlNodeList nodelist = root.SelectNodes("Area[Language='" + lang + "/List/Item"); foreach (XmlNode node in nodelist){ result.Add(node.InnerText); } reader.Close(); return result; } |
<?xml version="1.0" encoding="utf-8" ?> <Resource> <Form> <Name>FrmLogon</Name> <Controls> <Control name="frmlogon" text="User logon" /> <Control name="lblusername" text="Username:" /> <Control name="lblpassword" text="Password:" /> <Control name="lbllanguage" text="Language:" /> <Control name="btnlogon" text="Logon" /> <Control name="btncancel" text="Exit" /> <Control name="btnchangepwd" text="Change Password" /> </Controls> </Form> <Form> <Name>FrmPasswordChange</Name> <Controls> <Control name="frmpasswordchange" text="Change password" /> <Control name="lblusername" text="Username:" /> <Control name="lblpassword" text="Old Password:" /> <Control name="lblnewpassword" text="New Password:" /> <Control name="lblpasswordconfirm" text="Password Confirm:" /> <Control name="btnok" text="OK" /> <Control name="btncancel" text="Return" /> </Controls> </Form> </Resource> |
/// <summary> /// 读取多语言的资源文件 /// </summary> /// <param name="frmName">窗体的Name</param> /// <param name="lang">要显示的语言(如ZH或EN)</param> /// <returns></returns> public static Hashtable ReadResource(string frmName,string lang){ Hashtable result = new Hashtable(); XmlReader reader = null; FileInfo fi = new FileInfo("resources/AppResource_" + lang + ".xml"); if (!fi.Exists) reader = new XmlTextReader("resources/AppResource.xml"); else reader = new XmlTextReader("resources/AppResource_" + lang + ".xml"); XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode root = doc.DocumentElement; XmlNodeList nodelist = root.SelectNodes("Form[Name='" + frmName + "']/Controls/Control"); foreach (XmlNode node in nodelist){ try{ XmlNode node1 = node.SelectSingleNode("@name"); XmlNode node2 = node.SelectSingleNode("@text"); if (node1 != null){ result.Add(node1.InnerText.ToLower(),node2.InnerText); } }catch(FormatException fe){ Console.WriteLine(fe.ToString()); } } reader.Close(); return result; } |
/// <summary> /// 获取控件的名称 /// </summary> /// <param name="form"></param> public static void getNames(Form form) { //根据用户选择的语言获得表的显示文字 Hashtable table = AppConfig.ReadResource(form.Name,Global.GetValue("lang").ToString()); Control.ControlCollection controlNames = form.Controls; //可以在这里设置窗体的一些统一的属性,这样所有的窗体都会应用该设置 // form.KeyPreview = true; // form.MaximizeBox = false; // form.MinimizeBox = false; // form.ControlBox = false; // form.FormBorderStyle = FormBorderStyle.FixedDialog; // form.StartPosition = FormStartPosition.CenterScreen; // form.TopMost = true; // form.KeyDown += new KeyEventHandler(OnEnter); try { foreach(Control control in controlNames) { if(control.GetType() == typeof(System.Windows.Forms.Panel)) GetSubControls(control.Controls,table); if(control.GetType() == typeof(System.Windows.Forms.GroupBox)) GetSubControls(control.Controls,table); // if(control.GetType() == typeof(System.Windows.Forms.TextBox) && control.Enabled) // control.GotFocus += new EventHandler(Txt_Focus); if(table.Contains(control.Name.ToLower())) control.Text = (string)table[control.Name.ToLower()]; } if(table.Contains(form.Name.ToLower())) form.Text = (string)table[form.Name.ToLower()]; } catch(Exception ex) Console.WriteLine(ex.ToString()); } |
/// <summary> /// 获得子控件的显示名 /// </summary> /// <param name="controls"></param> /// <param name="table"></param> private static void GetSubControls(Control.ControlCollection controls,Hashtable table) { foreach(Control control in controls) { if(control.GetType() == typeof(System.Windows.Forms.Panel)) GetSubControls(control.Controls,table); if(control.GetType() == typeof(System.Windows.Forms.GroupBox)) GetSubControls(control.Controls,table); if(table.Contains(control.Name.ToLower())) control.Text = (string)table[control.Name.ToLower()]; } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者