Devexpress 中对RedailMenu的使用
Posted 荣码一生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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在DevExpress程序中使用条形码二维码控件,以及进行报表打印处理