数据存储,读取控件在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);
}
判断文件是否存在
调整某个控件的大小与位置
private void ChangeOne(FontDialog f, string lblname)
{
try
{
//H:\Users\bindot\Documents\Visual Studio 2010\Projects\Print\Print\Resources
string path = Application.StartupPath + @"/fomate.xml";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(path);
XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
foreach (XmlElement el in topM)
{
if (el.Name.ToLower() == "font" && el.Attributes["Key"].Value == lblname)
{
el.Attributes["FontName"].Value = f.Font.FontFamily.Name.ToString();
el.Attributes["FontSize"].Value = f.Font.Size.ToString();
xmldoc.Save(AppDomain.CurrentDomain.BaseDirectory + @"fomate.xml");
MessageBox.Show("默认格式保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
initFomate();
}
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
ChangeOne