C# 毕业证书打印《四》

数据存储,读取控件在Panel中的位置,将控件的位置保存到xml文件中。

/// <summary>
        /// 将当前格式写入xml
        /// </summary>
        /// <param name="font"></param>
        private void xmlWrite(Font font)
        {
            try
            {
                //H:\Users\bindot\Documents\Visual Studio 2010\Projects\Print\Print\Resources
                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);
            }
        }
写入到xml

这个地方貌似还不是很严谨,对于文件不存在的情况没有进行判断,您可参考以下代码

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:\Users\bindot\Documents\Visual Studio 2010\Projects\Print\Print\Resources
 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         }
ChangeOne

 

 

posted @ 2013-06-26 21:30  FengLu-1  阅读(967)  评论(1编辑  收藏  举报