最近项目中用到RadialMenu,效果图如下所示:(图片因为公司要求不能挂出来,所以截取中一部分了)
闲下来就对,devexpress中的RedialMenu的使用总结一下。
第一:假设RedialMenu中全部是BarButtonItem的情况。
第二:假设RedialMenu中有BarSubItem也有BarButtonItem的情况。
第三:假设先按首字母进行归类,首字母全部为BarSubItem,其中的子类为BarButtonItem。
第一中情况的效果图如下所示
调用代码:
radialMenu.InitDate(barManager, , ItemClick, this);
radialMenu.AddSubItemAndItem(list);
其中红色字体list是初始化数据:
List<string> list = new List<string>();
for (int i = ; i <= ; i++)
{
list.Add("啊受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("吧受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("额受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("飞受命于天" + i);
} for (int i = ; i <= ; i++)
{
list.Add("个受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("好吧受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("就才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("看的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("了额受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("吗飞受命于天" + i);
} for (int i = ; i <= ; i++)
{
list.Add("你受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("哦吧受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("平才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("去的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("人额受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("是飞受命于天" + i);
} for (int i = ; i <= ; i++)
{
list.Add("他才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("我的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("想受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("一飞受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("在飞受命于天" + i);
}
第二中情况的效果图如下所示
调用代码:
List<string> list = new List<string>();
Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
for (int i = ; i < ; i++)
{
list.Clear();
for (int j = ; j <= i + ; j++)
{
list.Add("受命于天" + j);
}
if (i % == )
list.Clear();
dic["受命于天" + i] = new List<string>(list);
}
radialMenu.InitDate(barManager, , ItemClick, this);
radialMenu.AddSubItemAndItem(dic);
第三中情况的效果图如下所示
按汉子首字母拼英进行归类,其中的子项归为首字母中。
调用代码:
radialMenu.InitDate(barManager, , ItemClick, this);
radialMenu.AddSubItemAndItem(list);
代码中的list与第一种情况下的list一样。
新建RadialMenu的扩展方法InitDate和AddSubItemAndItem。代码如下所示:
public static class RadialMenuExpression
{
private static bool isInit = false;
private static int index = ;
private static int ItemCount = ;
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 <= || 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
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(, );
barDockControlTop.Size = new System.Drawing.Size(, ); barDockControlBottom.CausesValidation = false;
barDockControlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
barDockControlBottom.Location = new System.Drawing.Point(, );
barDockControlBottom.Size = new System.Drawing.Size(, ); barDockControlLeft.CausesValidation = false;
barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left;
barDockControlLeft.Location = new System.Drawing.Point(, );
barDockControlLeft.Size = new System.Drawing.Size(, ); barDockControlRight.CausesValidation = false;
barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right;
barDockControlRight.Location = new System.Drawing.Point(, );
barDockControlRight.Size = new System.Drawing.Size(, );
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 <= )
{
AddItem(item.Key, itemClick);
continue;
}
else
AddSubItem(subItem);
List<string> listItemName = item.Value;//.ToList();
int count = ;
foreach (string Itemname in listItemName)
{
count++;
if (subItem == null)
AddItem(Itemname, itemClick);
else
SubItemAddItem(itemClick, Itemname, subItem);
if (count != && count % ItemCount == )
{
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 = ;
bool flag = false;
foreach (string Itemname in listName)
{
count++;
if (subItem == null)
AddItem(Itemname, itemClick);
else
SubItemAddItem(itemClick, Itemname, subItem);
if (count != && count % ItemCount == )
{
if (!flag)
{
subItem = SetSubItem(name);
AddSubItem(subItem);
flag = true;
}
else
{
BarSubItem subItemTemo = SetSubItem(name);
SubItemAddSubItem(subItem, subItemTemo);
subItem = subItemTemo;
}
count = ;
}
}
}
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(, )).Select(x => x.Letter.Substring(, )).Distinct().ToList();
foreach (string name in list)
{
dic[name] = listNameAndLetter.Where(x => x.Letter.Substring(, ) == 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 = ;
foreach (string Itemname in listItemName)
{
count++;
if (subItem == null)
AddItem(Itemname, itemClick);
else
SubItemAddItem(itemClick, Itemname, subItem);
if (count != && count % ItemCount == )
{
BarSubItem subItemTemo = SetSubItem("更多");
SubItemAddSubItem(subItem, subItemTemo);
subItem = subItemTemo;
}
}
}
}
private static void SetDictionary(Dictionary<string, List<NameAndLetter>> dic, bool flagCount = true)
{
if (dic.Count < )
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 <= && dicMin.Count <= )
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 >= )
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 <= )
return dicTemp;
int count = ;
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 = ; for (i = ; i <= iLen - ; i++)
{
strTemp += GetCharSpellCode(CnStr.Substring(i, ));
}
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 == )
{
return cnChar.ToUpper();
}
// get the array of byte from the single char
int i1 = ZW[];
int i2 = ZW[];
iCnChar = i1 * + 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 >= ) && (iCnChar <= ))
{
return "A";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "B";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "C";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "D";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "E";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "F";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "G";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "H";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "J";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "K";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "L";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "M";
} if ((iCnChar >= ) && (iCnChar <= ))
{
return "N";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "O";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "P";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "Q";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "R";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "S";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "T";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "W";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "X";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "Y";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
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 = ; i <= ; i++)
{
list.Add("啊受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("吧受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("额受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("飞受命于天" + i);
} for (int i = ; i <= ; i++)
{
list.Add("个受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("好吧受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("就才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("看的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("了额受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("吗飞受命于天" + i);
} for (int i = ; i <= ; i++)
{
list.Add("你受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("哦吧受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("平才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("去的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("人额受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("是飞受命于天" + i);
} for (int i = ; i <= ; i++)
{
list.Add("他才受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("我的受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("想受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("一飞受命于天" + i);
}
for (int i = ; i <= ; i++)
{
list.Add("在飞受命于天" + i);
}
#endregion radialMenu.InitDate(barManager, , 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 = ; for (i = ; i <= iLen - ; i++)
{
strTemp += GetCharSpellCode(CnStr.Substring(i, ));
}
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 == )
{
return cnChar.ToUpper();
}
// get the array of byte from the single char
int i1 = ZW[];
int i2 = ZW[];
iCnChar = i1 * + 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 >= ) && (iCnChar <= ))
{
return "A";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "B";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "C";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "D";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "E";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "F";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "G";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "H";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "J";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "K";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "L";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "M";
} if ((iCnChar >= ) && (iCnChar <= ))
{
return "N";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "O";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "P";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "Q";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "R";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "S";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "T";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "W";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "X";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
return "Y";
}
if ((iCnChar >= ) && (iCnChar <= ))
{
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 = ;
private static int ItemCount = ;
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 <= || 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
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(, );
barDockControlTop.Size = new System.Drawing.Size(, ); barDockControlBottom.CausesValidation = false;
barDockControlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
barDockControlBottom.Location = new System.Drawing.Point(, );
barDockControlBottom.Size = new System.Drawing.Size(, ); barDockControlLeft.CausesValidation = false;
barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left;
barDockControlLeft.Location = new System.Drawing.Point(, );
barDockControlLeft.Size = new System.Drawing.Size(, ); barDockControlRight.CausesValidation = false;
barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right;
barDockControlRight.Location = new System.Drawing.Point(, );
barDockControlRight.Size = new System.Drawing.Size(, );
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 <= )
{
AddItem(item.Key, itemClick);
continue;
}
else
AddSubItem(subItem);
List<string> listItemName = item.Value;//.ToList();
int count = ;
foreach (string Itemname in listItemName)
{
count++;
if (subItem == null)
AddItem(Itemname, itemClick);
else
SubItemAddItem(itemClick, Itemname, subItem);
if (count != && count % ItemCount == )
{
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 = ;
bool flag = false;
foreach (string Itemname in listName)
{
count++;
if (subItem == null)
AddItem(Itemname, itemClick);
else
SubItemAddItem(itemClick, Itemname, subItem);
if (count != && count % ItemCount == )
{
if (!flag)
{
subItem = SetSubItem(name);
AddSubItem(subItem);
flag = true;
}
else
{
BarSubItem subItemTemo = SetSubItem(name);
SubItemAddSubItem(subItem, subItemTemo);
subItem = subItemTemo;
}
count = ;
}
}
}
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(, )).Select(x => x.Letter.Substring(, )).Distinct().ToList();
foreach (string name in list)
{
dic[name] = listNameAndLetter.Where(x => x.Letter.Substring(, ) == 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 = ;
foreach (string Itemname in listItemName)
{
count++;
if (subItem == null)
AddItem(Itemname, itemClick);
else
SubItemAddItem(itemClick, Itemname, subItem);
if (count != && count % ItemCount == )
{
BarSubItem subItemTemo = SetSubItem("更多");
SubItemAddSubItem(subItem, subItemTemo);
subItem = subItemTemo;
}
}
}
}
private static void SetDictionary(Dictionary<string, List<NameAndLetter>> dic, bool flagCount = true)
{
if (dic.Count < )
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 <= && dicMin.Count <= )
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 >= )
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 <= )
return dicTemp;
int count = ;
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;
} }
}