数据存储,读取控件在Panel中的位置,将控件的位置保存到xml文件中。
/// <summary> /// 将当前格式写入xml /// </summary> /// <param name="font"></param> private void xmlWrite(Font font) { try { //H:UsersindotDocumentsVisual Studio 2010ProjectsPrintPrintResources string path = Application.StartupPath + @"fomate.xml"; XmlDocument doc = new XmlDocument(); // 创建dom对象 XmlElement root = doc.CreateElement("Lable");// 创建根节点Page doc.AppendChild(root); // 加入到xml document foreach (Control c in panel1.Controls) { XmlElement rfont = doc.CreateElement("font"); rfont.SetAttribute("Key", c.Name.ToString()); rfont.SetAttribute("X", c.Location.X.ToString()); rfont.SetAttribute("Y", c.Location.Y.ToString()); rfont.SetAttribute("FontSize", font.Size.ToString()); rfont.SetAttribute("FontName", font.Name); root.AppendChild(rfont); } doc.Save(AppDomain.CurrentDomain.BaseDirectory + @"fomate.xml"); MessageBox.Show("默认格式保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
这个地方貌似还不是很严谨,对于文件不存在的情况没有进行判断,您可参考以下代码
FileInfo newFile = new FileInfo(filename); if (newFile.Exists) { newFile.Delete(); newFile = new FileInfo(filename); }
调整某个控件的大小与位置
1 private void ChangeOne(FontDialog f, string lblname) 2 { 3 try 4 { 5 //H:UsersindotDocumentsVisual Studio 2010ProjectsPrintPrintResources 6 string path = Application.StartupPath + @"/fomate.xml"; 7 XmlDocument xmldoc = new XmlDocument(); 8 xmldoc.Load(path); 9 XmlNodeList topM = xmldoc.DocumentElement.ChildNodes; 10 foreach (XmlElement el in topM) 11 { 12 if (el.Name.ToLower() == "font" && el.Attributes["Key"].Value == lblname) 13 { 14 el.Attributes["FontName"].Value = f.Font.FontFamily.Name.ToString(); 15 el.Attributes["FontSize"].Value = f.Font.Size.ToString(); 16 xmldoc.Save(AppDomain.CurrentDomain.BaseDirectory + @"fomate.xml"); 17 MessageBox.Show("默认格式保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 18 initFomate(); 19 } 20 } 21 22 } 23 catch (Exception ex) 24 { 25 MessageBox.Show(ex.Message); 26 } 27 }