Devexpress 中对RedailMenu的使用

最近项目中用到RadialMenu,效果图如下所示:(图片因为公司要求不能挂出来,所以截取中一部分了)

 

 

闲下来就对,devexpress中的RedialMenu的使用总结一下。

第一:假设RedialMenu中全部是BarButtonItem的情况。

第二:假设RedialMenu中有BarSubItem也有BarButtonItem的情况。

第三:假设先按首字母进行归类,首字母全部为BarSubItem,其中的子类为BarButtonItem。

第一中情况的效果图如下所示

调用代码:

                radialMenu.InitDate(barManager, 9, ItemClick, this);
                radialMenu.AddSubItemAndItem(list);

其中红色字体list是初始化数据:

 List<string> list = new List<string>();
                for (int i = 1; i <= 5; i++)
                {
                    list.Add("啊受命于天" + i);
                }
                for (int i = 1; i <= 6; i++)
                {
                    list.Add("吧受命于天" + i);
                }
                for (int i = 1; i <= 7; i++)
                {
                    list.Add("才受命于天" + i);
                }
                for (int i = 1; i <= 8; i++)
                {
                    list.Add("的受命于天" + i);
                }
                for (int i = 1; i <= 9; i++)
                {
                    list.Add("额受命于天" + i);
                }
                for (int i = 1; i <= 10; i++)
                {
                    list.Add("飞受命于天" + i);
                }

                for (int i = 1; i <= 11; i++)
                {
                    list.Add("个受命于天" + i);
                }
                for (int i = 1; i <= 12; i++)
                {
                    list.Add("好吧受命于天" + i);
                }
                for (int i = 1; i <= 13; i++)
                {
                    list.Add("就才受命于天" + i);
                }
                for (int i = 1; i <= 14; i++)
                {
                    list.Add("看的受命于天" + i);
                }
                for (int i = 1; i <= 15; i++)
                {
                    list.Add("了额受命于天" + i);
                }
                for (int i = 1; i <= 16; i++)
                {
                    list.Add("吗飞受命于天" + i);
                }

                for (int i = 1; i <= 17; i++)
                {
                    list.Add("你受命于天" + i);
                }
                for (int i = 1; i <= 18; i++)
                {
                    list.Add("哦吧受命于天" + i);
                }
                for (int i = 1; i <= 19; i++)
                {
                    list.Add("平才受命于天" + i);
                }
                for (int i = 1; i <= 20; i++)
                {
                    list.Add("去的受命于天" + i);
                }
                for (int i = 1; i <= 21; i++)
                {
                    list.Add("人额受命于天" + i);
                }
                for (int i = 1; i <= 22; i++)
                {
                    list.Add("是飞受命于天" + i);
                }

                for (int i = 1; i <= 23; i++)
                {
                    list.Add("他才受命于天" + i);
                }
                for (int i = 1; i <= 24; i++)
                {
                    list.Add("我的受命于天" + i);
                }
                for (int i = 1; i <= 25; i++)
                {
                    list.Add("想受命于天" + i);
                }
                for (int i = 1; i <= 26; i++)
                {
                    list.Add("一飞受命于天" + i);
                }
                for (int i = 1; i <= 27; i++)
                {
                    list.Add("在飞受命于天" + i);
                }

 

第二中情况的效果图如下所示

调用代码:

                List<string> list = new List<string>();
                Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
                for (int i = 0; i < 10; i++)
                {
                    list.Clear();
                    for (int j = 0; j <= i + 5; j++)
                    {
                        list.Add("受命于天" + j);
                    }
                    if (i % 5 == 0)
                        list.Clear();
                    dic["受命于天" + i] = new List<string>(list);
                }
                radialMenu.InitDate(barManager, 10, ItemClick, this);
                radialMenu.AddSubItemAndItem(dic);

 

第三中情况的效果图如下所示

 

按汉子首字母拼英进行归类,其中的子项归为首字母中。

调用代码:

                radialMenu.InitDate(barManager, 9, ItemClick, this);
                radialMenu.AddSubItemAndItem(list);

代码中的list与第一种情况下的list一样。

 

新建RadialMenu的扩展方法InitDate和AddSubItemAndItem。代码如下所示:

   public static class RadialMenuExpression
    {
        private static bool isInit = false;
        private static int index = 0;
        private static int ItemCount = 9;
        private static DevExpress.XtraBars.Ribbon.RadialMenu radialMenu;
        private static BarManager barManager;
        private static Action<object, ItemClickEventArgs> itemClick;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="barManagerExpression">barManager</param>
        /// <param name="itemCountExpression">显示个数</param>
        /// <param name="itemClickExpression">点击事件</param>
        /// <param name="fromExpression">所属窗体</param>
        /// <returns></returns>
        public static bool InitDate(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, BarManager barManagerExpression, int itemCountExpression, Action<object, ItemClickEventArgs> itemClickExpression, Control fromExpression)
        {
            if (radialMenuExpression == null || barManagerExpression == null || itemCountExpression <= 0 || itemClickExpression == null)
                return false;
            radialMenu = radialMenuExpression;
            barManager = barManagerExpression;
            ItemCount = itemCountExpression;
            itemClick = itemClickExpression;
            InitRadialMenu(fromExpression);
            isInit = true;
            return true;
        }
        private static void InitRadialMenu(Control from)
        {
            //radialMenu = new DevExpress.XtraBars.Ribbon.RadialMenu();
            radialMenu.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
            radialMenu.MenuColor = System.Drawing.Color.Teal;
            barManager = new BarManager();
            barManager.Form = from;
            DevExpress.XtraBars.BarDockControl barDockControlLeft = new BarDockControl();
            DevExpress.XtraBars.BarDockControl barDockControlRight = new BarDockControl();
            DevExpress.XtraBars.BarDockControl barDockControlBottom = new BarDockControl();
            DevExpress.XtraBars.BarDockControl barDockControlTop = new BarDockControl();

            barDockControlTop.CausesValidation = false;
            barDockControlTop.Dock = System.Windows.Forms.DockStyle.Top;
            barDockControlTop.Location = new System.Drawing.Point(0, 0);
            barDockControlTop.Size = new System.Drawing.Size(1280, 0);

            barDockControlBottom.CausesValidation = false;
            barDockControlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            barDockControlBottom.Location = new System.Drawing.Point(0, 1024);
            barDockControlBottom.Size = new System.Drawing.Size(1280, 0);

            barDockControlLeft.CausesValidation = false;
            barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left;
            barDockControlLeft.Location = new System.Drawing.Point(0, 0);
            barDockControlLeft.Size = new System.Drawing.Size(0, 1024);

            barDockControlRight.CausesValidation = false;
            barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right;
            barDockControlRight.Location = new System.Drawing.Point(1280, 0);
            barDockControlRight.Size = new System.Drawing.Size(0, 1024);
            barManager.DockControls.Add(barDockControlTop);
            barManager.DockControls.Add(barDockControlBottom);
            barManager.DockControls.Add(barDockControlLeft);
            barManager.DockControls.Add(barDockControlRight);
            radialMenu.Manager = barManager;
            //this.barManager.MaxItemId = listName.Count;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dic">BarSubItem的名称为dic中key的值,BarSubItem中BarButtonItem的名称为对应的dic中value</param>
        /// <param name="itemClick">RadialMenu中BarButtonItem的单击事件</param>
        public static void AddSubItemAndBarItem(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, Dictionary<string, List<string>> dic)
        {
            if (!isInit)
                return;
            BarSubItem subItem = null;
            foreach (KeyValuePair<string, List<string>> item in dic)
            {
                subItem = SetSubItem(item.Key);
                if (item.Value.Count <= 0)
                {
                    AddItem(item.Key, itemClick);
                    continue;
                }
                else
                    AddSubItem(subItem);
                List<string> listItemName = item.Value;//.ToList();
                int count = 0;
                foreach (string Itemname in listItemName)
                {
                    count++;
                    if (subItem == null)
                        AddItem(Itemname, itemClick);
                    else
                        SubItemAddItem(itemClick, Itemname, subItem);
                    if (count != 0 && count % ItemCount == 0)
                    {
                        BarSubItem subItemTemo = SetSubItem("更多");
                        SubItemAddSubItem(subItem, subItemTemo);
                        subItem = subItemTemo;
                    }
                }
            }
        }
        private static BarSubItem SetSubItem(string name)
        {
            BarSubItem subItem = new BarSubItem();
            subItem.Id = index++;
            subItem.Caption = name;
            subItem.ItemAppearance.Hovered.ForeColor = System.Drawing.Color.Black;
            subItem.ItemAppearance.Hovered.Options.UseForeColor = true;
            subItem.ItemInMenuAppearance.Hovered.BackColor = System.Drawing.Color.Yellow;
            subItem.ItemInMenuAppearance.Hovered.ForeColor = System.Drawing.Color.Black;
            subItem.ItemInMenuAppearance.Hovered.Options.UseBackColor = true;
            subItem.ItemInMenuAppearance.Hovered.Options.UseForeColor = true;
            subItem.ItemInMenuAppearance.Normal.ForeColor = System.Drawing.Color.Yellow;
            subItem.ItemInMenuAppearance.Normal.Options.UseForeColor = true;
            return subItem;
        }
        private static BarButtonItem GetBarButtonItem(string name, Action<object, ItemClickEventArgs> itemClick)
        {
            BarButtonItem item = new BarButtonItem();
            item.Id = index++;
            item.Caption = name;
            item.ItemAppearance.Disabled.ForeColor = System.Drawing.Color.Yellow;
            item.ItemAppearance.Disabled.Options.UseForeColor = true;
            item.ItemInMenuAppearance.Hovered.BackColor = System.Drawing.Color.Yellow;
            item.ItemInMenuAppearance.Hovered.Options.UseBackColor = true;
            item.ItemInMenuAppearance.Hovered.Options.UseForeColor = true;
            item.ItemInMenuAppearance.Normal.ForeColor = System.Drawing.Color.Yellow;
            item.ItemInMenuAppearance.Normal.Options.UseForeColor = true;
            item.Name = name;
            item.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(itemClick);
            return item;
        }
        /// <summary>
        /// 正常初始化
        /// </summary>
        /// <param name="name">BarSubItem的名称</param>
        /// <param name="listName">RadialMenu中BarButtonItem的名称</param>
        /// <param name="itemClick">RadialMenu中BarButtonItem的单击事件</param>
        public static void AddSubItemAndBarItem(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, string name, List<string> listName)
        {
            if (!isInit)
                return;
            BarSubItem subItem = null;
            int count = 0;
            bool flag = false;
            foreach (string Itemname in listName)
            {
                count++;
                if (subItem == null)
                    AddItem(Itemname, itemClick);
                else
                    SubItemAddItem(itemClick, Itemname, subItem);
                if (count != 0 && count % ItemCount == 0)
                {
                    if (!flag)
                    {
                        subItem = SetSubItem(name);
                        AddSubItem(subItem);
                        flag = true;
                    }
                    else
                    {
                        BarSubItem subItemTemo = SetSubItem(name);
                        SubItemAddSubItem(subItem, subItemTemo);
                        subItem = subItemTemo;
                    }
                    count = 0;
                }
            }
        }
        private static void AddSubItem(BarSubItem subItem)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            radialMenu.LinksPersistInfo.Add(new LinkPersistInfo(subItem));
            barManager.Items.Add(subItem);
            radialMenu.EndInit();
            barManager.EndInit();
        }
        private static void SubItemAddSubItem(BarSubItem subItem, BarSubItem addItem)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            subItem.LinksPersistInfo.Add(new LinkPersistInfo(addItem));
            barManager.Items.Add(addItem);
            radialMenu.EndInit();
            barManager.EndInit();
        }
        private static void AddItem(string nameItem, Action<object, ItemClickEventArgs> itemClick)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            BarButtonItem item = GetBarButtonItem(nameItem, itemClick);
            radialMenu.LinksPersistInfo.Add(new LinkPersistInfo(item));
            barManager.Items.Add(item);
            radialMenu.EndInit();
            barManager.EndInit();
        }
        private static void SubItemAddItem(Action<object, ItemClickEventArgs> itemClick, string nameItem, BarSubItem subItem)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            BarButtonItem item = GetBarButtonItem(nameItem, itemClick);
            subItem.LinksPersistInfo.Add(new LinkPersistInfo(item));
            barManager.Items.Add(item);
            radialMenu.EndInit();
            barManager.EndInit();
        }

        /// <summary>
        /// 按首字母进行初始化
        /// </summary>
        /// <param name="listName">RadialMenu中BarButtonItem的名称</param>
        /// <param name="itemClick">RadialMenu中BarButtonItem的单击事件</param>
        public static void AddSubItemAndBarItem(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, List<string> listName)
        {
            if (!isInit)
                return;
            List<NameAndLetter> listNameAndLetter = new List<NameAndLetter>();
            Dictionary<string, List<NameAndLetter>> dic = new Dictionary<string, List<NameAndLetter>>();
            foreach (string name in listName)
            {
                NameAndLetter item = new NameAndLetter();
                item.Name = name;
                item.Letter = CNSpellTranslator.GetSpellCode(name);
                listNameAndLetter.Add(item);
            }
            List<string> list = listNameAndLetter.OrderBy(x => x.Letter.Substring(0, 1)).Select(x => x.Letter.Substring(0, 1)).Distinct().ToList();
            foreach (string name in list)
            {
                dic[name] = listNameAndLetter.Where(x => x.Letter.Substring(0, 1) == name).ToList();
            }
            SetDictionary(dic);
            BarSubItem subItem = null;
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dic)
            {
                subItem = SetSubItem(item.Key);
                AddSubItem(subItem);
                List<string> listItemName = item.Value.Select(x => x.Name).ToList();
                int count = 0;
                foreach (string Itemname in listItemName)
                {
                    count++;
                    if (subItem == null)
                        AddItem(Itemname, itemClick);
                    else
                        SubItemAddItem(itemClick, Itemname, subItem);
                    if (count != 0 && count % ItemCount == 0)
                    {
                        BarSubItem subItemTemo = SetSubItem("更多");
                        SubItemAddSubItem(subItem, subItemTemo);
                        subItem = subItemTemo;
                    }
                }
            }
        }
        private static void SetDictionary(Dictionary<string, List<NameAndLetter>> dic, bool flagCount = true)
        {
            if (dic.Count < 9)
                return;
            Dictionary<string, List<NameAndLetter>> dicMin = GetMinDicOrMax(dic, true);
            Dictionary<string, List<NameAndLetter>> dicMax = GetMinDicOrMax(dic, true);
            string key = "";
            List<NameAndLetter> value = new List<NameAndLetter>();
            if (dicMax.Count <= 0 && dicMin.Count <= 0)
                return;
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dicMin)
            {
                key += item.Key;
                value = value.Concat(item.Value).ToList();
            }
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dicMax)
            {
                key += item.Key;
                value = value.Concat(item.Value).ToList();
            }
            dic[key] = value;
            if (dic.Count >= 0)
                SetDictionary(dic);
        }
        private static Dictionary<string, List<NameAndLetter>> GetMinDicOrMax(Dictionary<string, List<NameAndLetter>> dic, bool
            isMin)
        {
            Dictionary<string, List<NameAndLetter>> dicTemp = new Dictionary<string, List<NameAndLetter>>();
            if (dic.Count <= 0)
                return dicTemp;
            int count = 0;
            bool flagMinDic = false;

            foreach (KeyValuePair<string, List<NameAndLetter>> item in dic)
            {
                int letterCount = item.Value.Count;
                if (!flagMinDic)
                {
                    count = item.Value.Count;
                    dicTemp[item.Key] = item.Value;
                    flagMinDic = true;
                    continue;
                }
                if (isMin)
                {
                    if (letterCount < count)
                    {
                        dicTemp.Clear();
                        dicTemp[item.Key] = item.Value;
                    }
                }
                else
                {
                    if (letterCount > count)
                    {
                        dicTemp.Clear();
                        dicTemp[item.Key] = item.Value;
                    }
                }
            }
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dicTemp)
            {
                dic.Remove(item.Key);
            }
            return dicTemp;
        }

    }

获取汉字拼音的类:

    public class CNSpellTranslator
    {
        /// <summary>
        ///     在指定的字符串列表CnStr中检索符合拼音索引字符串
        /// </summary>
        /// <param name="CnStr">汉字字符串</param>
        /// <returns>相对应的汉语拼音首字母串</returns>
        public static string GetSpellCode(string CnStr)
        {
            string strTemp = "";
            int iLen = CnStr.Length;
            int i = 0;

            for (i = 0; i <= iLen - 1; i++)
            {
                strTemp += GetCharSpellCode(CnStr.Substring(i, 1));
            }
            return strTemp;
        }

        /// <summary>
        /// 输入是字母还是汉字
        /// </summary>
        /// <param name="firstKey"></param>
        /// <returns></returns>
        public static bool IsChar(char firstKey)
        {
            if ((firstKey >= 'a' && firstKey <= 'z') || (firstKey >= 'A' && firstKey <= 'Z'))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
        /// </summary>
        /// <param name="cnChar">单个汉字</param>
        /// <returns>单个大写字母</returns>
        private static string GetCharSpellCode(string cnChar)
        {
            long iCnChar;

            byte[] ZW = Encoding.Default.GetBytes(cnChar);

            //如果是字母,则直接返回 
            if (ZW.Length == 1)
            {
                return cnChar.ToUpper();
            }
            // get the array of byte from the single char 
            int i1 = ZW[0];
            int i2 = ZW[1];
            iCnChar = i1 * 256 + i2;

            //expresstion 
            //table of the constant list 
            // 'A'; //45217..45252 
            // 'B'; //45253..45760 
            // 'C'; //45761..46317 
            // 'D'; //46318..46825 
            // 'E'; //46826..47009 
            // 'F'; //47010..47296 
            // 'G'; //47297..47613 

            // 'H'; //47614..48118 
            // 'J'; //48119..49061 
            // 'K'; //49062..49323 
            // 'L'; //49324..49895 
            // 'M'; //49896..50370 
            // 'N'; //50371..50613 
            // 'O'; //50614..50621 
            // 'P'; //50622..50905 
            // 'Q'; //50906..51386 

            // 'R'; //51387..51445 
            // 'S'; //51446..52217 
            // 'T'; //52218..52697 
            //没有U,V 
            // 'W'; //52698..52979 
            // 'X'; //52980..53640 
            // 'Y'; //53689..54480 
            // 'Z'; //54481..55289 

            // iCnChar match the constant 
            if ((iCnChar >= 45217) && (iCnChar <= 45252))
            {
                return "A";
            }
            if ((iCnChar >= 45253) && (iCnChar <= 45760))
            {
                return "B";
            }
            if ((iCnChar >= 45761) && (iCnChar <= 46317))
            {
                return "C";
            }
            if ((iCnChar >= 46318) && (iCnChar <= 46825))
            {
                return "D";
            }
            if ((iCnChar >= 46826) && (iCnChar <= 47009))
            {
                return "E";
            }
            if ((iCnChar >= 47010) && (iCnChar <= 47296))
            {
                return "F";
            }
            if ((iCnChar >= 47297) && (iCnChar <= 47613))
            {
                return "G";
            }
            if ((iCnChar >= 47614) && (iCnChar <= 48118))
            {
                return "H";
            }
            if ((iCnChar >= 48119) && (iCnChar <= 49061))
            {
                return "J";
            }
            if ((iCnChar >= 49062) && (iCnChar <= 49323))
            {
                return "K";
            }
            if ((iCnChar >= 49324) && (iCnChar <= 49895))
            {
                return "L";
            }
            if ((iCnChar >= 49896) && (iCnChar <= 50370))
            {
                return "M";
            }

            if ((iCnChar >= 50371) && (iCnChar <= 50613))
            {
                return "N";
            }
            if ((iCnChar >= 50614) && (iCnChar <= 50621))
            {
                return "O";
            }
            if ((iCnChar >= 50622) && (iCnChar <= 50905))
            {
                return "P";
            }
            if ((iCnChar >= 50906) && (iCnChar <= 51386))
            {
                return "Q";
            }
            if ((iCnChar >= 51387) && (iCnChar <= 51445))
            {
                return "R";
            }
            if ((iCnChar >= 51446) && (iCnChar <= 52217))
            {
                return "S";
            }
            if ((iCnChar >= 52218) && (iCnChar <= 52697))
            {
                return "T";
            }
            if ((iCnChar >= 52698) && (iCnChar <= 52979))
            {
                return "W";
            }
            if ((iCnChar >= 52980) && (iCnChar <= 53640))
            {
                return "X";
            }
            if ((iCnChar >= 53689) && (iCnChar <= 54480))
            {
                return "Y";
            }
            if ((iCnChar >= 54481) && (iCnChar <= 55289))
            {
                return "Z";
            }
            return ("?");
        }
    }
    public class NameAndLetter
    {
        public string Name { get; set; }
        public string Letter { get; set; }
    }

以上就是这次使用的总结,记录下来为以后使用方便,也加深记忆:

全部代码:

using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreawayMeasureManageTool
{
    public partial class RadialMenu : Form
    {
        DevExpress.XtraBars.Ribbon.RadialMenu radialMenu = new DevExpress.XtraBars.Ribbon.RadialMenu();
        BarManager barManager = new BarManager();
        public RadialMenu()
        {
            InitializeComponent();
            this.Load += delegate
            {
                #region MyRegion
                List<string> list = new List<string>();
                for (int i = 1; i <= 5; i++)
                {
                    list.Add("啊受命于天" + i);
                }
                for (int i = 1; i <= 6; i++)
                {
                    list.Add("吧受命于天" + i);
                }
                for (int i = 1; i <= 7; i++)
                {
                    list.Add("才受命于天" + i);
                }
                for (int i = 1; i <= 8; i++)
                {
                    list.Add("的受命于天" + i);
                }
                for (int i = 1; i <= 9; i++)
                {
                    list.Add("额受命于天" + i);
                }
                for (int i = 1; i <= 10; i++)
                {
                    list.Add("飞受命于天" + i);
                }

                for (int i = 1; i <= 11; i++)
                {
                    list.Add("个受命于天" + i);
                }
                for (int i = 1; i <= 12; i++)
                {
                    list.Add("好吧受命于天" + i);
                }
                for (int i = 1; i <= 13; i++)
                {
                    list.Add("就才受命于天" + i);
                }
                for (int i = 1; i <= 14; i++)
                {
                    list.Add("看的受命于天" + i);
                }
                for (int i = 1; i <= 15; i++)
                {
                    list.Add("了额受命于天" + i);
                }
                for (int i = 1; i <= 16; i++)
                {
                    list.Add("吗飞受命于天" + i);
                }

                for (int i = 1; i <= 17; i++)
                {
                    list.Add("你受命于天" + i);
                }
                for (int i = 1; i <= 18; i++)
                {
                    list.Add("哦吧受命于天" + i);
                }
                for (int i = 1; i <= 19; i++)
                {
                    list.Add("平才受命于天" + i);
                }
                for (int i = 1; i <= 20; i++)
                {
                    list.Add("去的受命于天" + i);
                }
                for (int i = 1; i <= 21; i++)
                {
                    list.Add("人额受命于天" + i);
                }
                for (int i = 1; i <= 22; i++)
                {
                    list.Add("是飞受命于天" + i);
                }

                for (int i = 1; i <= 23; i++)
                {
                    list.Add("他才受命于天" + i);
                }
                for (int i = 1; i <= 24; i++)
                {
                    list.Add("我的受命于天" + i);
                }
                for (int i = 1; i <= 25; i++)
                {
                    list.Add("想受命于天" + i);
                }
                for (int i = 1; i <= 26; i++)
                {
                    list.Add("一飞受命于天" + i);
                }
                for (int i = 1; i <= 27; i++)
                {
                    list.Add("在飞受命于天" + i);
                } 
                #endregion

                radialMenu.InitDate(barManager, 9, ItemClick, this);
                radialMenu.AddSubItemAndItem("更多", list);

                //Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
                //for (int i = 0; i < 10; i++)
                //{
                //    list.Clear();
                //    for (int j = 0; j <= i + 5; j++)
                //    {
                //        list.Add("受命于天" + j);
                //    }
                //    if (i % 5 == 0)
                //        list.Clear();
                //    dic["受命于天" + i] = new List<string>(list);
                //}
                //radialMenu.InitDate(barManager, 10, ItemClick, this);
                //radialMenu.AddSubItemAndItem(dic);
            };
        }
        private void ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var btnItem = sender as BarManager;
            if (btnItem != null)
            {
                string name = btnItem.PressedLink.Caption;
                switch (name)
                {
                    case "系统维护":
                    case "查询统计":
                    case "视频查看":
                    case "视频服务":
                    case "重新登录":
                    case "告警查询":
                    case "事件查看":
                    case "通讯状态查询":
                    case "告警提示开":
                    case "告警联动开":
                    case "告警提示关":
                    case "告警联动关":
                    case "告警联动配置":
                    case "设备联动配置":
                    case "入侵检测配置":
                    case "入所管理":
                    case "视频轮播方案":
                    case "站内图编辑":
                    case "钥匙箱变电站关联":
                    case "告警统计":
                    case "故障查询":
                    case "挂牌查询":
                    case "辅助设备统计":
                    case "入所查询":
                    case "日志查询":
                    case "退运查询":
                    case "登陆人员查询":
                    case "设备动环曲线":
                    case "设备铭牌查询":
                        MessageBox.Show(name);
                        break;
                    default: break;
                }
            }
            this.radialMenu.Collapse(false, true);
        }
        private void RadialMenu_Click(object sender, EventArgs e)
        {
            radialMenu.ShowPopup(Cursor.Position, true);
        }

    }
    public class CNSpellTranslator
    {
        /// <summary>
        ///     在指定的字符串列表CnStr中检索符合拼音索引字符串
        /// </summary>
        /// <param name="CnStr">汉字字符串</param>
        /// <returns>相对应的汉语拼音首字母串</returns>
        public static string GetSpellCode(string CnStr)
        {
            string strTemp = "";
            int iLen = CnStr.Length;
            int i = 0;

            for (i = 0; i <= iLen - 1; i++)
            {
                strTemp += GetCharSpellCode(CnStr.Substring(i, 1));
            }
            return strTemp;
        }

        /// <summary>
        /// 输入是字母还是汉字
        /// </summary>
        /// <param name="firstKey"></param>
        /// <returns></returns>
        public static bool IsChar(char firstKey)
        {
            if ((firstKey >= 'a' && firstKey <= 'z') || (firstKey >= 'A' && firstKey <= 'Z'))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
        /// </summary>
        /// <param name="cnChar">单个汉字</param>
        /// <returns>单个大写字母</returns>
        private static string GetCharSpellCode(string cnChar)
        {
            long iCnChar;

            byte[] ZW = Encoding.Default.GetBytes(cnChar);

            //如果是字母,则直接返回 
            if (ZW.Length == 1)
            {
                return cnChar.ToUpper();
            }
            // get the array of byte from the single char 
            int i1 = ZW[0];
            int i2 = ZW[1];
            iCnChar = i1 * 256 + i2;

            //expresstion 
            //table of the constant list 
            // 'A'; //45217..45252 
            // 'B'; //45253..45760 
            // 'C'; //45761..46317 
            // 'D'; //46318..46825 
            // 'E'; //46826..47009 
            // 'F'; //47010..47296 
            // 'G'; //47297..47613 

            // 'H'; //47614..48118 
            // 'J'; //48119..49061 
            // 'K'; //49062..49323 
            // 'L'; //49324..49895 
            // 'M'; //49896..50370 
            // 'N'; //50371..50613 
            // 'O'; //50614..50621 
            // 'P'; //50622..50905 
            // 'Q'; //50906..51386 

            // 'R'; //51387..51445 
            // 'S'; //51446..52217 
            // 'T'; //52218..52697 
            //没有U,V 
            // 'W'; //52698..52979 
            // 'X'; //52980..53640 
            // 'Y'; //53689..54480 
            // 'Z'; //54481..55289 

            // iCnChar match the constant 
            if ((iCnChar >= 45217) && (iCnChar <= 45252))
            {
                return "A";
            }
            if ((iCnChar >= 45253) && (iCnChar <= 45760))
            {
                return "B";
            }
            if ((iCnChar >= 45761) && (iCnChar <= 46317))
            {
                return "C";
            }
            if ((iCnChar >= 46318) && (iCnChar <= 46825))
            {
                return "D";
            }
            if ((iCnChar >= 46826) && (iCnChar <= 47009))
            {
                return "E";
            }
            if ((iCnChar >= 47010) && (iCnChar <= 47296))
            {
                return "F";
            }
            if ((iCnChar >= 47297) && (iCnChar <= 47613))
            {
                return "G";
            }
            if ((iCnChar >= 47614) && (iCnChar <= 48118))
            {
                return "H";
            }
            if ((iCnChar >= 48119) && (iCnChar <= 49061))
            {
                return "J";
            }
            if ((iCnChar >= 49062) && (iCnChar <= 49323))
            {
                return "K";
            }
            if ((iCnChar >= 49324) && (iCnChar <= 49895))
            {
                return "L";
            }
            if ((iCnChar >= 49896) && (iCnChar <= 50370))
            {
                return "M";
            }

            if ((iCnChar >= 50371) && (iCnChar <= 50613))
            {
                return "N";
            }
            if ((iCnChar >= 50614) && (iCnChar <= 50621))
            {
                return "O";
            }
            if ((iCnChar >= 50622) && (iCnChar <= 50905))
            {
                return "P";
            }
            if ((iCnChar >= 50906) && (iCnChar <= 51386))
            {
                return "Q";
            }
            if ((iCnChar >= 51387) && (iCnChar <= 51445))
            {
                return "R";
            }
            if ((iCnChar >= 51446) && (iCnChar <= 52217))
            {
                return "S";
            }
            if ((iCnChar >= 52218) && (iCnChar <= 52697))
            {
                return "T";
            }
            if ((iCnChar >= 52698) && (iCnChar <= 52979))
            {
                return "W";
            }
            if ((iCnChar >= 52980) && (iCnChar <= 53640))
            {
                return "X";
            }
            if ((iCnChar >= 53689) && (iCnChar <= 54480))
            {
                return "Y";
            }
            if ((iCnChar >= 54481) && (iCnChar <= 55289))
            {
                return "Z";
            }
            return ("?");
        }
    }
    public class NameAndLetter
    {
        public string Name { get; set; }
        public string Letter { get; set; }
    }
    struct ItemAndSubItem
    {
        int itemClass;
        List<string> listItems;
        List<string> listSubItems;
    }

    public static class RadialMenuExpression
    {
        private static bool isInit = false;
        private static int index = 0;
        private static int ItemCount = 9;
        private static DevExpress.XtraBars.Ribbon.RadialMenu radialMenu;
        private static BarManager barManager;
        private static Action<object, ItemClickEventArgs> itemClick;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="barManagerExpression">barManager</param>
        /// <param name="itemCountExpression">显示个数</param>
        /// <param name="itemClickExpression">点击事件</param>
        /// <param name="fromExpression">所属窗体</param>
        /// <returns></returns>
        public static bool InitDate(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, BarManager barManagerExpression, int itemCountExpression, Action<object, ItemClickEventArgs> itemClickExpression, Control fromExpression)
        {
            if (radialMenuExpression == null || barManagerExpression == null || itemCountExpression <= 0 || itemClickExpression == null)
                return false;
            radialMenu = radialMenuExpression;
            barManager = barManagerExpression;
            ItemCount = itemCountExpression;
            itemClick = itemClickExpression;
            InitRadialMenu(fromExpression);
            isInit = true;
            return true;
        }
        private static void InitRadialMenu(Control from)
        {
            //radialMenu = new DevExpress.XtraBars.Ribbon.RadialMenu();
            radialMenu.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
            radialMenu.MenuColor = System.Drawing.Color.Teal;
            barManager = new BarManager();
            barManager.Form = from;
            DevExpress.XtraBars.BarDockControl barDockControlLeft = new BarDockControl();
            DevExpress.XtraBars.BarDockControl barDockControlRight = new BarDockControl();
            DevExpress.XtraBars.BarDockControl barDockControlBottom = new BarDockControl();
            DevExpress.XtraBars.BarDockControl barDockControlTop = new BarDockControl();

            barDockControlTop.CausesValidation = false;
            barDockControlTop.Dock = System.Windows.Forms.DockStyle.Top;
            barDockControlTop.Location = new System.Drawing.Point(0, 0);
            barDockControlTop.Size = new System.Drawing.Size(1280, 0);

            barDockControlBottom.CausesValidation = false;
            barDockControlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            barDockControlBottom.Location = new System.Drawing.Point(0, 1024);
            barDockControlBottom.Size = new System.Drawing.Size(1280, 0);

            barDockControlLeft.CausesValidation = false;
            barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left;
            barDockControlLeft.Location = new System.Drawing.Point(0, 0);
            barDockControlLeft.Size = new System.Drawing.Size(0, 1024);

            barDockControlRight.CausesValidation = false;
            barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right;
            barDockControlRight.Location = new System.Drawing.Point(1280, 0);
            barDockControlRight.Size = new System.Drawing.Size(0, 1024);
            barManager.DockControls.Add(barDockControlTop);
            barManager.DockControls.Add(barDockControlBottom);
            barManager.DockControls.Add(barDockControlLeft);
            barManager.DockControls.Add(barDockControlRight);
            radialMenu.Manager = barManager;
            //this.barManager.MaxItemId = listName.Count;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dic">BarSubItem的名称为dic中key的值,BarSubItem中BarButtonItem的名称为对应的dic中value</param>
        /// <param name="itemClick">RadialMenu中BarButtonItem的单击事件</param>
        public static void AddSubItemAndBarItem(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, Dictionary<string, List<string>> dic)
        {
            if (!isInit)
                return;
            BarSubItem subItem = null;
            foreach (KeyValuePair<string, List<string>> item in dic)
            {
                subItem = SetSubItem(item.Key);
                if (item.Value.Count <= 0)
                {
                    AddItem(item.Key, itemClick);
                    continue;
                }
                else
                    AddSubItem(subItem);
                List<string> listItemName = item.Value;//.ToList();
                int count = 0;
                foreach (string Itemname in listItemName)
                {
                    count++;
                    if (subItem == null)
                        AddItem(Itemname, itemClick);
                    else
                        SubItemAddItem(itemClick, Itemname, subItem);
                    if (count != 0 && count % ItemCount == 0)
                    {
                        BarSubItem subItemTemo = SetSubItem("更多");
                        SubItemAddSubItem(subItem, subItemTemo);
                        subItem = subItemTemo;
                    }
                }
            }
        }
        private static BarSubItem SetSubItem(string name)
        {
            BarSubItem subItem = new BarSubItem();
            subItem.Id = index++;
            subItem.Caption = name;
            subItem.ItemAppearance.Hovered.ForeColor = System.Drawing.Color.Black;
            subItem.ItemAppearance.Hovered.Options.UseForeColor = true;
            subItem.ItemInMenuAppearance.Hovered.BackColor = System.Drawing.Color.Yellow;
            subItem.ItemInMenuAppearance.Hovered.ForeColor = System.Drawing.Color.Black;
            subItem.ItemInMenuAppearance.Hovered.Options.UseBackColor = true;
            subItem.ItemInMenuAppearance.Hovered.Options.UseForeColor = true;
            subItem.ItemInMenuAppearance.Normal.ForeColor = System.Drawing.Color.Yellow;
            subItem.ItemInMenuAppearance.Normal.Options.UseForeColor = true;
            return subItem;
        }
        private static BarButtonItem GetBarButtonItem(string name, Action<object, ItemClickEventArgs> itemClick)
        {
            BarButtonItem item = new BarButtonItem();
            item.Id = index++;
            item.Caption = name;
            item.ItemAppearance.Disabled.ForeColor = System.Drawing.Color.Yellow;
            item.ItemAppearance.Disabled.Options.UseForeColor = true;
            item.ItemInMenuAppearance.Hovered.BackColor = System.Drawing.Color.Yellow;
            item.ItemInMenuAppearance.Hovered.Options.UseBackColor = true;
            item.ItemInMenuAppearance.Hovered.Options.UseForeColor = true;
            item.ItemInMenuAppearance.Normal.ForeColor = System.Drawing.Color.Yellow;
            item.ItemInMenuAppearance.Normal.Options.UseForeColor = true;
            item.Name = name;
            item.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(itemClick);
            return item;
        }
        /// <summary>
        /// 正常初始化
        /// </summary>
        /// <param name="name">BarSubItem的名称</param>
        /// <param name="listName">RadialMenu中BarButtonItem的名称</param>
        /// <param name="itemClick">RadialMenu中BarButtonItem的单击事件</param>
        public static void AddSubItemAndBarItem(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, string name, List<string> listName)
        {
            if (!isInit)
                return;
            BarSubItem subItem = null;
            int count = 0;
            bool flag = false;
            foreach (string Itemname in listName)
            {
                count++;
                if (subItem == null)
                    AddItem(Itemname, itemClick);
                else
                    SubItemAddItem(itemClick, Itemname, subItem);
                if (count != 0 && count % ItemCount == 0)
                {
                    if (!flag)
                    {
                        subItem = SetSubItem(name);
                        AddSubItem(subItem);
                        flag = true;
                    }
                    else
                    {
                        BarSubItem subItemTemo = SetSubItem(name);
                        SubItemAddSubItem(subItem, subItemTemo);
                        subItem = subItemTemo;
                    }
                    count = 0;
                }
            }
        }
        private static void AddSubItem(BarSubItem subItem)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            radialMenu.LinksPersistInfo.Add(new LinkPersistInfo(subItem));
            barManager.Items.Add(subItem);
            radialMenu.EndInit();
            barManager.EndInit();
        }
        private static void SubItemAddSubItem(BarSubItem subItem, BarSubItem addItem)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            subItem.LinksPersistInfo.Add(new LinkPersistInfo(addItem));
            barManager.Items.Add(addItem);
            radialMenu.EndInit();
            barManager.EndInit();
        }
        private static void AddItem(string nameItem, Action<object, ItemClickEventArgs> itemClick)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            BarButtonItem item = GetBarButtonItem(nameItem, itemClick);
            radialMenu.LinksPersistInfo.Add(new LinkPersistInfo(item));
            barManager.Items.Add(item);
            radialMenu.EndInit();
            barManager.EndInit();
        }
        private static void SubItemAddItem(Action<object, ItemClickEventArgs> itemClick, string nameItem, BarSubItem subItem)
        {
            radialMenu.BeginInit();
            barManager.BeginInit();
            BarButtonItem item = GetBarButtonItem(nameItem, itemClick);
            subItem.LinksPersistInfo.Add(new LinkPersistInfo(item));
            barManager.Items.Add(item);
            radialMenu.EndInit();
            barManager.EndInit();
        }

        /// <summary>
        /// 按首字母进行初始化
        /// </summary>
        /// <param name="listName">RadialMenu中BarButtonItem的名称</param>
        /// <param name="itemClick">RadialMenu中BarButtonItem的单击事件</param>
        public static void AddSubItemAndBarItem(this DevExpress.XtraBars.Ribbon.RadialMenu radialMenuExpression, List<string> listName)
        {
            if (!isInit)
                return;
            List<NameAndLetter> listNameAndLetter = new List<NameAndLetter>();
            Dictionary<string, List<NameAndLetter>> dic = new Dictionary<string, List<NameAndLetter>>();
            foreach (string name in listName)
            {
                NameAndLetter item = new NameAndLetter();
                item.Name = name;
                item.Letter = CNSpellTranslator.GetSpellCode(name);
                listNameAndLetter.Add(item);
            }
            List<string> list = listNameAndLetter.OrderBy(x => x.Letter.Substring(0, 1)).Select(x => x.Letter.Substring(0, 1)).Distinct().ToList();
            foreach (string name in list)
            {
                dic[name] = listNameAndLetter.Where(x => x.Letter.Substring(0, 1) == name).ToList();
            }
            SetDictionary(dic);
            BarSubItem subItem = null;
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dic)
            {
                subItem = SetSubItem(item.Key);
                AddSubItem(subItem);
                List<string> listItemName = item.Value.Select(x => x.Name).ToList();
                int count = 0;
                foreach (string Itemname in listItemName)
                {
                    count++;
                    if (subItem == null)
                        AddItem(Itemname, itemClick);
                    else
                        SubItemAddItem(itemClick, Itemname, subItem);
                    if (count != 0 && count % ItemCount == 0)
                    {
                        BarSubItem subItemTemo = SetSubItem("更多");
                        SubItemAddSubItem(subItem, subItemTemo);
                        subItem = subItemTemo;
                    }
                }
            }
        }
        private static void SetDictionary(Dictionary<string, List<NameAndLetter>> dic, bool flagCount = true)
        {
            if (dic.Count < 9)
                return;
            Dictionary<string, List<NameAndLetter>> dicMin = GetMinDicOrMax(dic, true);
            Dictionary<string, List<NameAndLetter>> dicMax = GetMinDicOrMax(dic, true);
            string key = "";
            List<NameAndLetter> value = new List<NameAndLetter>();
            if (dicMax.Count <= 0 && dicMin.Count <= 0)
                return;
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dicMin)
            {
                key += item.Key;
                value = value.Concat(item.Value).ToList();
            }
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dicMax)
            {
                key += item.Key;
                value = value.Concat(item.Value).ToList();
            }
            dic[key] = value;
            if (dic.Count >= 0)
                SetDictionary(dic);
        }
        private static Dictionary<string, List<NameAndLetter>> GetMinDicOrMax(Dictionary<string, List<NameAndLetter>> dic, bool
            isMin)
        {
            Dictionary<string, List<NameAndLetter>> dicTemp = new Dictionary<string, List<NameAndLetter>>();
            if (dic.Count <= 0)
                return dicTemp;
            int count = 0;
            bool flagMinDic = false;

            foreach (KeyValuePair<string, List<NameAndLetter>> item in dic)
            {
                int letterCount = item.Value.Count;
                if (!flagMinDic)
                {
                    count = item.Value.Count;
                    dicTemp[item.Key] = item.Value;
                    flagMinDic = true;
                    continue;
                }
                if (isMin)
                {
                    if (letterCount < count)
                    {
                        dicTemp.Clear();
                        dicTemp[item.Key] = item.Value;
                    }
                }
                else
                {
                    if (letterCount > count)
                    {
                        dicTemp.Clear();
                        dicTemp[item.Key] = item.Value;
                    }
                }
            }
            foreach (KeyValuePair<string, List<NameAndLetter>> item in dicTemp)
            {
                dic.Remove(item.Key);
            }
            return dicTemp;
        }

    }
}
View Code

 

 

转载于:https://www.cnblogs.com/zhangyuanbo12358/p/6740365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值