C# BackupSupport 1.4.0 (自定义辅助菜单)

简介

我在操作电脑的时候往往会觉得右键菜单的功能局限性太强,想把一些使用频度高的机械性操作放到右键菜单里,可惜不管是操作系统还是各类软件的可定制性都没有这么强。没有的东西就自己做一个呗。
目标:按下Win+A跳出辅助菜单,辅助菜单可以方便地自定义
使用时
这里写图片描述
设置界面
这里写图片描述

实现

从结论开始说就是BackupSupport项目里增加了5个类
这里写图片描述
AuxiliaryMenuItem用来描述辅助菜单中的项目
AuxiliaryMenuBinder用来绑定AuxiliaryMenuItem列表到菜单控件
AuxiliaryMenuProcesser用来处理菜单命令
AuxiliaryMenuItemView和AuxiliaryMenuGroupView只是两个用来设置AuxiliaryMenuItem的视图

1、AuxiliaryMenuItem

我假定的辅助菜单中只有子菜单和按钮两种Item,那么AuxiliaryMenuItem需要描述的信息就只有:
(1)Name-名称
(2)Belong-属于哪个子菜单
(3)Command Type-命令类型
(4)Value-命令的值
其中也就只有命令类型需要按需求重点规划下。

我需要辅助菜单实现的操作的种类也不多,大致就是:
(1)执行外部文件
(2)执行BackupSupport的KeyScript来模拟按键输入
(3)粘贴文本文件的内容
(4)粘贴文本
(5)朗读选中的文本
(6)用搜索引擎搜索选中文本
(7)自定义打开操作
(8)运行时编译并执行C#文件

规划好后代码就变成下面这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZDevExpressTools;
using ZTools;

namespace AuxiliaryMenu {
    public class AuxiliaryMenuItem {
        public enum CommandTypeEnum {
            Extern,
            KeyScript,
            PasteContent,
            PasteFile,
            MSSpeech,
            Search,
            Open,
            CSharp,
        }

        public static string ConvertAuxiliaryMenuCommandTypeToString(CommandTypeEnum commandType) {
            return EnumTools.GetName(commandType);
        }

        public static CommandTypeEnum ConvertStringToAuxiliaryMenuCommandType(string src) {
            return EnumTools.GetValueIgnoreCase<CommandTypeEnum>(src);
        }

        public AuxiliaryMenuItem() {

        }

        public AuxiliaryMenuItem(string name) {
            this.Text = name;
        }


        private CommandTypeEnum commandType;
        /// <summary>
        /// 命令类型
        /// </summary>
        public CommandTypeEnum CommandType {
            get {
                return this.commandType;
            }
            set {
                this.commandType = value;
            }
        }

        [ZTreeListColumnToolsSettings("Command Type")]
        public string CommandTypeDescription {
            get {
                return AuxiliaryMenuItem.ConvertAuxiliaryMenuCommandTypeToString(this.CommandType);
            }
            set {
                this.CommandType = AuxiliaryMenuItem.ConvertStringToAuxiliaryMenuCommandType(value);
            }
        }

        private string text;
        [ZTreeListColumnToolsSettings("Name")]
        public string Text {
            get {
                return this.text;
            }
            set {
                this.text = value;
            }
        }

        private string value;
        [ZTreeListColumnToolsSettings("Value")]
        public string Value {
            get {
                return this.value;
            }
            set {
                this.value = value;
            }
        }

        private string belong;
        [ZTreeListColumnToolsSettings("Belong")]
        public string Belong {
            get {
                return this.belong;
            }
            set {
                this.belong = value;
            }
        }

        private bool visiable = true;
        [ZTreeListColumnToolsSettings("Visiable")]
        public bool Visiable {
            get {
                return this.visiable;
            }
            set {
                this.visiable = value;
            }
        }

        private bool isGroup = false;
        public bool IsGroup {
            get {
                return isGroup;
            }
            set {
                isGroup = value;
            }
        }

        public override string ToString() {
            if (this.IsGroup == true) {
                if (Belong == null || Belong.Length == 0) {
                    string result = "MenuGroup:{0}\t{1}";
                    return StringProcesser.Format(result, this.Text, this.Visiable);    
                } else {
                    string result = "MenuGroup:{0}\t{1}\t{2}";
                    return StringProcesser.Format(result, this.Text, this.Belong, this.Visiable);   
                }
            } else {
                string result = "MenuItem:{0}\t{1}\t{2}\t{3}";
                return StringProcesser.Format(result, this.Text, this.CommandTypeDescription, this.Value, this.Belong);
            }
        }

        public static AuxiliaryMenuItem Create(string context) {
            KeyValuePair<string, string> tagvalue = ConfigTools.ConvertStringToTagValue(context);
            if (tagvalue.Key.ToUpper().Equals("MenuItem".ToUpper())) {
                List<string> list = ListProcesser.SplitStringToList(tagvalue.Value, "\t");
                AuxiliaryMenuItem result = new AuxiliaryMenuItem();
                try {
                    result.Text = list[0];
                    result.CommandTypeDescription = list[1];
                    result.Value = list[2];
                    result.Belong = list[3];
                } catch (Exception) {
                }
                return result;
            } else if (tagvalue.Key.ToUpper().Equals("MenuGroup".ToUpper())) {
                List<string> list = ListProcesser.SplitStringToList(tagvalue.Value, "\t");
                AuxiliaryMenuItem result = new AuxiliaryMenuItem();
                try {
                    result.IsGroup = true;
                    result.Text = list[0];
                    if (list.Count == 3) {
                        result.Belong = list[1];
                        result.Visiable = StringProcesser.ConvertToBoolean(list[2]);
                    } else {
                        result.Visiable = StringProcesser.ConvertToBoolean(list[1]);
                    }
                } catch (Exception) {
                }
                return result;
            } else {
                return null;
            }
        }

        public void Explan(string src) {
            AuxiliaryMenuItem temp = AuxiliaryMenuItem.Create(src);
            if (temp != null) {
                this.Text = temp.Text;
                this.CommandTypeDescription = temp.CommandTypeDescription;
                this.Value = temp.Value;
                this.Belong = temp.Belong;
                this.IsGroup = temp.IsGroup;
            }
        }
    }
}

2、AuxiliaryMenuBinder

有了描述菜单项的类,接下来需要把一组这个类的列表绑定到菜单控件上,这里使用的菜单控件是PopupMenu。
单纯的一个逻辑问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraBars;
using ZTools;
using ZTools.KeyScriptProcesser;

namespace AuxiliaryMenu {
    public class AuxiliaryMenuBinder {

        private static void AuxiliaryMenu_Click(object sender, ItemClickEventArgs e) {
            PopupMenu popupMenu = e.Link.LinkedObject as PopupMenu;
            if (popupMenu != null) {
                popupMenu.HidePopup();
            }
            AuxiliaryMenuProcesser.ProcessAyns(e.Item);
        }

        public static void BindPopupMenu(PopupMenu popupMenu) {
            BindPopupMenu(popupMenu, AuxiliaryMenuProcesser.CurrentAuxiliaryMenuItemList);
        }

        /// <summary>
        /// 绑定PopupMenu
        /// </summary>
        /// <param name="popupMenu"></param>
        /// <param name="itemList"></param>
        public static void BindPopupMenu(PopupMenu popupMenu, List<AuxiliaryMenuItem> itemList) {
            if (popupMenu == null || itemList == null) {
                return;
            }
            popupMenu.ClearLinks();
            List<BarSubItem> groupItemList = new List<BarSubItem>();
            List<string> hideGroupList = new List<string>();
            if (itemList.Count > 0) {
                foreach (AuxiliaryMenuItem menuItem in itemList) {
                    if (menuItem.IsGroup == true && (menuItem.Belong == null || menuItem.Belong.Length == 0)) {
                        if (menuItem.Visiable == true) {
                            BarSubItem item = new BarSubItem();
                            item.Caption = menuItem.Text;
                            popupMenu.AddItem(item);
                            groupItemList.Add(item);
                        } else {
                            hideGroupList.Add(menuItem.Text);
                        }
                    }
                }

                bool subFlag = false;
                foreach (AuxiliaryMenuItem menuItem in itemList) {
                    if (menuItem.IsGroup == true && (menuItem.Belong != null && menuItem.Belong.Length > 0)) {
                        if (menuItem.Visiable == true && hideGroupList.Contains(menuItem.Belong) == false) {
                            BarSubItem item = new BarSubItem();
                            item.Caption = menuItem.Text;
                            groupItemList.Add(item);
                            subFlag = false;
                            if (groupItemList.Count > 0) {
                                foreach (BarSubItem subItem in groupItemList) {
                                    if (subItem.Caption.Equals(menuItem.Belong)) {
                                        subItem.AddItem(item);
                                        subFlag = true;
                                    }
                                }
                            }
                            if (subFlag == false) {
                                popupMenu.AddItem(item);
                            }
                        } else {
                            hideGroupList.Add(menuItem.Text);
                        }
                    }
                }


                foreach (AuxiliaryMenuItem menuItem in itemList) {
                    if (menuItem.IsGroup == false) {
                        if (hideGroupList.Contains(menuItem.Belong) == false) {
                            BarButtonItem item = new BarButtonItem();
                            item.Caption = menuItem.Text;
                            subFlag = false;
                            item.ItemClick += new ItemClickEventHandler(AuxiliaryMenu_Click);
                            if (groupItemList.Count > 0) {
                                foreach (BarSubItem subItem in groupItemList) {
                                    if (subItem.Caption.Equals(menuItem.Belong)) {
                                        subItem.AddItem(item);
                                        subFlag = true;
                                    }
                                }
                            }
                            if (subFlag == false) {
                                popupMenu.AddItem(item);
                            }
                            if (menuItem.CommandType == AuxiliaryMenuItem.CommandTypeEnum.KeyScript) {
                                KeyScriptProcesser.LoadCommandListFormKeyScriptFile(menuItem.Value);
                            }
                            AuxiliaryMenuProcesser.AddCommandBuffer(item, menuItem);
                        }
                    }
                }
            }
        }
    }
}

3、AuxiliaryMenuProcesser

最后需要一个命令处理器来处理当菜单按钮按下后需要执行的命令。基本就是调用BackupSupport的其他功能,或者往外抛让别的程序来执行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using DevExpress.XtraBars;
using ZTools.KeyScriptProcesser;
using ZTools;

namespace AuxiliaryMenu {
    public class AuxiliaryMenuProcesser {

        private static Dictionary<BarButtonItem, AuxiliaryMenuItem> AuxiliaryMenuDictionary = new Dictionary<BarButtonItem,AuxiliaryMenuItem>();

        public static void ClearCommandBuffer() {
            AuxiliaryMenuDictionary.Clear();
        }

        public static void AddCommandBuffer(BarButtonItem barItem, AuxiliaryMenuItem item) {
            try {
                AuxiliaryMenuDictionary.Add(barItem, item);
            } catch (Exception) {

            }
        }

        public static void Process(BarButtonItem item) {
            if (item != null) {
                if (AuxiliaryMenuDictionary.ContainsKey(item) == true) {
                    Process(AuxiliaryMenuDictionary[item]);
                }
            }
        }

        public static void Process(AuxiliaryMenuItem item) {
            if (item != null) {
                switch (item.CommandType) {
                    case AuxiliaryMenuItem.CommandTypeEnum.Extern:
                        try {
                            System.Diagnostics.Process.Start(item.Value);   
                        } catch (Exception e) {
                            MessageBox.Show(e.Message);
                        }
                        break;
                    case AuxiliaryMenuItem.CommandTypeEnum.KeyScript:
                        KeyScriptProcesser.ProcessKeyScriptFile(item.Value);
                        break;
                    case AuxiliaryMenuItem.CommandTypeEnum.PasteContent:
                        if (ClipboardTools.SetFileContent(item.Value) == true) {
                            SendKeys.SendWait("^v");    
                        }
                        break;
                    case AuxiliaryMenuItem.CommandTypeEnum.PasteFile:
                        if (ClipboardTools.SetFile(item.Value) == true) {
                            SendKeys.SendWait("^v");
                        }
                        break;
                    case AuxiliaryMenuItem.CommandTypeEnum.MSSpeech:
                        try {
                            System.Diagnostics.Process.Start("MSSpeechTrigger.exe", "/tag:" + item.Value);
                        } catch (Exception e) {
                            MessageBox.Show(e.Message);
                        }
                        break;
                    case AuxiliaryMenuItem.CommandTypeEnum.Search:
                        try {
                            System.Diagnostics.Process.Start("SearchEngineTrigger.exe", "/s:" + item.Value);
                        } catch (Exception e) {
                            MessageBox.Show(e.Message);
                        }
                        break;
                    case AuxiliaryMenuItem.CommandTypeEnum.Open:
                        try {
                            System.Diagnostics.Process.Start("SearchEngineTrigger.exe", "/s:" + item.Value + " /e:OFF");
                        } catch (Exception e) {
                            MessageBox.Show(e.Message);
                        }
                        break;
                    case AuxiliaryMenuItem.CommandTypeEnum.CSharp:
                        try {
                            DynamicCSharpTools.ProcessCSharpFile(item.Value);
                        } catch (Exception e) {
                            MessageBox.Show(e.Message);
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        public static void Process(object item) {
            BarButtonItem barItem = item as BarButtonItem;
            if (barItem != null) {
                Process(barItem);
                return;
            }

            AuxiliaryMenuItem menuItem = item as AuxiliaryMenuItem;
            if (menuItem != null) {
                Process(menuItem);
                return;
            }
        }

        public static List<AuxiliaryMenuItem> CurrentAuxiliaryMenuItemList = new List<AuxiliaryMenuItem>();

        public static List<string> CurrentAuxiliaryMenuItemListSavedContentList {
            get {
                List<string> resultList = new List<string>();
                if (CurrentAuxiliaryMenuItemList != null && CurrentAuxiliaryMenuItemList.Count > 0) {
                    foreach (AuxiliaryMenuItem menuItem in CurrentAuxiliaryMenuItemList) {
                        resultList.Add(menuItem.ToString());
                    }
                }
                return resultList;
            }
            set {
                if (CurrentAuxiliaryMenuItemList == null) {CurrentAuxiliaryMenuItemList = new List<AuxiliaryMenuItem>();}
                CurrentAuxiliaryMenuItemList.Clear();
                if (value != null && value.Count > 0) {
                    foreach (string temp in value) {
                        AuxiliaryMenuItem item = AuxiliaryMenuItem.Create(temp);
                        if (item != null) {
                            CurrentAuxiliaryMenuItemList.Add(item);
                        }
                    }
                }
            }
        }

        //--------------------------------------------------------------------
        public static void ProcessAyns(object args) {
            Thread th = new Thread(Thread_Param);
            th.SetApartmentState(ApartmentState.STA);
            th.Start(args);
        }

        private static void Thread_Param(object args) {
            AuxiliaryMenuProcesser.Process(args);
        }
    }
}

BackupSupport的使用视频和下载地址

使用视频见twitter:@photonaaa
下载地址:http://pan.baidu.com/s/1sl2V1k5
欢迎加群讨论347678765(加群成功率50%)

阅读更多
文章标签: c# 简便操作 winform
想对作者说点什么? 我来说一句

可自主定义工具的辅助菜单

2011年03月30日 326KB 下载

d3d9外挂辅助菜单-虚幻引擎

2017年10月26日 16KB 下载

ps的各个工具菜单--工具菜单

2009年05月22日 36KB 下载

c# 自定义winform系统菜单

2018年03月14日 61KB 下载

没有更多推荐了,返回首页

不良信息举报

C# BackupSupport 1.4.0 (自定义辅助菜单)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭