示例#1
0
        public void Save()
        {
            XDocument doc  = new XDocument();
            XElement  root = new XElement("root");

            doc.Add(root);
            foreach (var kvp in allActions.Where(x => x.Value.GetType() == typeof(FolderAction)))
            {
                XElement elem   = new XElement("FolderAction");
                var      action = kvp.Value as FolderAction;
                elem.SetAttributeValue("Key", kvp.Key);
                elem.SetAttributeValue("Title", action.Title);
                elem.SetAttributeValue("ExeCondition", action.ExeConditionName ?? "");
                elem.SetAttributeValue("BackgroundColor", "#" + action.BackgroundColor.R.ToString("X2") + action.BackgroundColor.G.ToString("X2") + action.BackgroundColor.B.ToString("X2"));
                elem.SetAttributeValue("Font", FontSerializationHelper.Serialize(action.Titlefont));
                elem.SetAttributeValue("ShowTitleLabel", action.ShowTitleLabel);
                root.Add(elem);
            }

            foreach (var kvp in allActions.Where(x => x.Value.GetType() == typeof(LaunchAction)))
            {
                XElement elem   = new XElement("LaunchAction");
                var      action = kvp.Value as LaunchAction;
                elem.SetAttributeValue("Key", kvp.Key);
                elem.SetAttributeValue("Title", action.Title);
                elem.SetAttributeValue("ExePath", action.ExePath);
                elem.SetAttributeValue("Args", action.Args);
                elem.SetAttributeValue("AlreadyRunningAction", action.AlreadyRunningAction.ToString());
                elem.SetAttributeValue("BackgroundColor", "#" + action.BackgroundColor.R.ToString("X2") + action.BackgroundColor.G.ToString("X2") + action.BackgroundColor.B.ToString("X2"));
                elem.SetAttributeValue("Font", FontSerializationHelper.Serialize(action.Titlefont));
                elem.SetAttributeValue("ShowTitleLabel", action.ShowTitleLabel);
                root.Add(elem);
            }

            foreach (var kvp in allActions.Where(x => x.Value.GetType() == typeof(TextStringAction)))
            {
                XElement elem   = new XElement("StringAction");
                var      action = kvp.Value as TextStringAction;

                elem.SetAttributeValue("Key", kvp.Key);
                elem.SetAttributeValue("Title", action.Title);
                elem.SetAttributeValue("Value", action.Value);
                elem.SetAttributeValue("BackgroundColor", "#" + action.BackgroundColor.R.ToString("X2") + action.BackgroundColor.G.ToString("X2") + action.BackgroundColor.B.ToString("X2"));
                elem.SetAttributeValue("Font", FontSerializationHelper.Serialize(action.Titlefont));
                elem.SetAttributeValue("ShowTitleLabel", action.ShowTitleLabel);
                root.Add(elem);
            }

            foreach (var kvp in allActions.Where(x => x.Value.GetType() == typeof(HotkeyAction)))
            {
                XElement elem   = new XElement("HotkeyAction");
                var      action = kvp.Value as HotkeyAction;
                elem.SetAttributeValue("Key", kvp.Key);
                elem.SetAttributeValue("Title", action.Title);
                elem.SetAttributeValue("BackgroundColor", "#" + action.BackgroundColor.R.ToString("X2") + action.BackgroundColor.G.ToString("X2") + action.BackgroundColor.B.ToString("X2"));
                elem.SetAttributeValue("Font", FontSerializationHelper.Serialize(action.Titlefont));
                elem.SetAttributeValue("ShowTitleLabel", action.ShowTitleLabel);
                for (int i = 0; i < 3; i++)
                {
                    if (action?.Modifiers.Count > i)
                    {
                        elem.SetAttributeValue("Mod" + i, ((int)action?.Modifiers[i]).ToString());
                    }
                    else
                    {
                        elem.SetAttributeValue("Mod" + i, "0");
                    }
                }
                elem.SetAttributeValue("MainKey", ((int)action?.MainKey).ToString());
                root.Add(elem);
            }

            doc.Save(SaveFileName);
        }
示例#2
0
        public void Load()
        {
            allActions.Clear();
            if (!File.Exists(SaveFileName))
            {
                return;
            }
            XDocument doc = XDocument.Load(SaveFileName);

            foreach (XElement elem in doc.Element("root").Elements())
            {
                string             path   = "";
                IButtonPressAction action = null;
                switch (elem.Name.LocalName)
                {
                case "FolderAction":
                    FolderAction folderAction = new FolderAction();
                    action                        = folderAction;
                    path                          = elem.Attribute("Key").Value.ToString();
                    folderAction.Title            = elem.Attribute("Title").Value.ToString();
                    folderAction.ExeConditionName = elem.Attribute("ExeCondition").Value.ToString();
                    if (elem.Attribute("BackgroundColor") != null)
                    {
                        folderAction.BackgroundColor =
                            System.Drawing.ColorTranslator.FromHtml(elem.Attribute("BackgroundColor").Value.ToString());
                    }
                    if (elem.Attribute("Font") != null)
                    {
                        folderAction.Titlefont = FontSerializationHelper.Deserialize(elem.Attribute("Font").Value);
                    }
                    folderAction.ShowTitleLabel = bool.Parse(elem.Attribute("ShowTitleLabel")?.Value ?? "False");
                    allActions.TryAdd(path, folderAction);
                    break;

                case "LaunchAction":
                    LaunchAction launchAction = new LaunchAction();
                    action               = launchAction;
                    path                 = elem.Attribute("Key").Value.ToString();
                    launchAction.Title   = elem.Attribute("Title").Value.ToString();
                    launchAction.ExePath = elem.Attribute("ExePath").Value.ToString();
                    launchAction.Args    = elem.Attribute("Args").Value.ToString();
                    if (elem.Attribute("BackgroundColor") != null)
                    {
                        launchAction.BackgroundColor =
                            System.Drawing.ColorTranslator.FromHtml(elem.Attribute("BackgroundColor").Value.ToString());
                    }
                    if (elem.Attribute("Font") != null)
                    {
                        launchAction.Titlefont = FontSerializationHelper.Deserialize(elem.Attribute("Font").Value);
                    }
                    launchAction.ShowTitleLabel = bool.Parse(elem.Attribute("ShowTitleLabel")?.Value ?? "False");
                    var result = LaunchAction.ProcessRunningAction.FocusOldProcess;
                    Enum.TryParse(elem.Attribute("AlreadyRunningAction").Value.ToString(), out result);
                    launchAction.AlreadyRunningAction = result;
                    allActions.TryAdd(path, launchAction);
                    break;

                case "StringAction":
                    TextStringAction textAction = new TextStringAction();
                    action           = textAction;
                    path             = elem.Attribute("Key").Value.ToString();
                    textAction.Title = elem.Attribute("Title").Value.ToString();
                    textAction.Value = elem.Attribute("Value").Value.ToString();
                    if (elem.Attribute("BackgroundColor") != null)
                    {
                        textAction.BackgroundColor =
                            System.Drawing.ColorTranslator.FromHtml(elem.Attribute("BackgroundColor").Value.ToString());
                    }
                    if (elem.Attribute("Font") != null)
                    {
                        textAction.Titlefont = FontSerializationHelper.Deserialize(elem.Attribute("Font").Value);
                    }
                    textAction.ShowTitleLabel = bool.Parse(elem.Attribute("ShowTitleLabel")?.Value ?? "False");
                    allActions.TryAdd(path, textAction);
                    break;

                case "HotkeyAction":
                    HotkeyAction hotkeyAction = new HotkeyAction();
                    action = hotkeyAction;
                    path   = elem.Attribute("Key").Value.ToString();
                    if (elem.Attribute("BackgroundColor") != null)
                    {
                        hotkeyAction.BackgroundColor =
                            System.Drawing.ColorTranslator.FromHtml(elem.Attribute("BackgroundColor").Value.ToString());
                    }
                    if (elem.Attribute("Font") != null)
                    {
                        hotkeyAction.Titlefont = FontSerializationHelper.Deserialize(elem.Attribute("Font").Value);
                    }
                    hotkeyAction.Title          = elem.Attribute("Title").Value.ToString();
                    hotkeyAction.ShowTitleLabel = bool.Parse(elem.Attribute("ShowTitleLabel")?.Value ?? "False");
                    for (int i = 0; i < 3; i++)
                    {
                        VirtualKeyCode key = (VirtualKeyCode)int.Parse(elem.Attribute("Mod" + i).Value.ToString());
                        if (key != 0)
                        {
                            hotkeyAction.Modifiers.Add(key);
                        }
                    }

                    hotkeyAction.MainKey = (VirtualKeyCode)int.Parse(elem.Attribute("MainKey").Value.ToString());
                    allActions.TryAdd(path, hotkeyAction);
                    break;
                }
                if (File.Exists("Images/" + path + ".png"))
                {
                    action.Icon = Bitmap.FromFile("Images/" + path + ".png");
                }
            }
        }