using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace PcgDrawR
{
///
/// 自定义菜单
///
public class CustomMenu
{
public string GroupName { get; set; }
public List MenuItems { get; set; }
private string strFile = "CustomMenu.xml";
public CustomMenu()
{
strFile = Path.Combine(Application.StartupPath, "CustomMenu.xml");
//GroupName = "钻井辅助分析";
MenuItems = new List();
//MenuItems.Add(new CustomMenuItem("TestDrawerInteract.exe", "TestDrawerInteract"));
//MenuItems.Add(new CustomMenuItem("TestDrawerInteract2.exe", "TestDrawerInteract2"));
}
///
/// 加载菜单项
///
/// 是否成功
public bool LoadItems()
{
if (!File.Exists(strFile))
{
return false;
}
using (StreamReader sr = new StreamReader(strFile, Encoding.Default))
{
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(CustomMenu));
CustomMenu customMenu = xmlSerializer.Deserialize(sr) as CustomMenu;
this.GroupName = customMenu.GroupName;
this.MenuItems = customMenu.MenuItems;
}
catch
{
return false;
}
}
return true;
}
///
/// 保存自定义菜单
///
/// 是否成功
public bool SaveItems()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineChars = "\r\n";
settings.Encoding = Encoding.Default;
using (StreamWriter sw = new StreamWriter(strFile, false, Encoding.Default))
using (XmlWriter writer = XmlWriter.Create(sw, settings))
{
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(CustomMenu));
xmlSerializer.Serialize(writer, this);
}
catch
{
return false;
}
}
return true;
}
}
public class CustomMenuItem
{
[XmlAttribute]
public string Caption { get; set; }
[XmlAttribute]
public string ProgramFile { get; set; }
[XmlAttribute]
public string ClassName { get; set; }
[XmlAttribute]
public bool IsPopupItem { get; set; } = false;
[XmlAttribute]
public string SvgImage { get; set; }
public int ImageTextStyles { get; set; }
public CustomMenuItem()
{
ImageTextStyles = 0;
}
public CustomMenuItem(string caption, string programFile, string className, string svgImage)
: this()
{
this.Caption = caption;
this.ProgramFile = programFile;
this.ClassName = className;
this.SvgImage = svgImage;
}
}
}