ContextMenuStripEx.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.ComponentModel;
  9. namespace LYFZ.ComponentLibrary
  10. {
  11. public class ContextMenuStripEx:System.Windows.Forms.ContextMenuStrip
  12. {
  13. public ContextMenuStripEx(System.ComponentModel.IContainer components)
  14. : base(components)
  15. {
  16. }
  17. public ContextMenuStripEx()
  18. {
  19. }
  20. /// <summary>
  21. /// 获取菜单项并包括所有子项
  22. /// </summary>
  23. /// <param name="item"></param>
  24. /// <returns></returns>
  25. protected ToolStripMenuItem GetToolStripMenuItem(LYFZ.Model.Model_SystemToolsMenu.Item item)
  26. {
  27. ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Text);
  28. menuItem.Image = LYFZ.WinAPI.CustomPublicMethod.GetMenuItemImage(item.Icon);
  29. menuItem.Name = item.KeyName;
  30. menuItem.Tag = item;
  31. SetDefaultChecked(menuItem, item.OpenMode);
  32. SetMenuItem_Click(menuItem);
  33. if (item.IsChildMenu)
  34. {
  35. foreach (LYFZ.Model.Model_SystemToolsMenu.Item subItem in item.ChildItems)
  36. {
  37. if (subItem.Type.ToLower() == "separator")
  38. {
  39. menuItem.DropDownItems.Add(GetToolStripSeparator(subItem));
  40. }
  41. else
  42. {
  43. if (!item.Disable)
  44. {
  45. ToolStripMenuItem subMenuItem = GetToolStripMenuItem(subItem);
  46. menuItem.DropDownItems.Add(subMenuItem);
  47. }
  48. }
  49. }
  50. }
  51. return menuItem;
  52. }
  53. /// <summary>
  54. /// 设置默认是否选择
  55. /// </summary>
  56. /// <param name="menuItem"></param>
  57. private void SetDefaultChecked(ToolStripMenuItem menuItem, string openMode)
  58. {
  59. LYFZ.BLL.BLL_Config bllConfig = new BLL.BLL_Config();
  60. LYFZ.Model.Model_Config modelConfig = bllConfig.GetModel_SysConfig();
  61. switch (openMode)
  62. {
  63. case "SetShowToolsDescription":
  64. menuItem.Checked = bllConfig.Get_IsShowToolsDescription();
  65. break;
  66. case "SetInterfaceSelect":
  67. try
  68. {
  69. string thisInterface = bllConfig.GetInterfaceSelect();
  70. string menuItemInterface = menuItem.Name.Split('_')[0];
  71. if (thisInterface.ToLower() == menuItemInterface.ToLower())
  72. {
  73. menuItem.Checked = true;
  74. }
  75. }
  76. catch { }
  77. break;
  78. }
  79. }
  80. /// <summary>
  81. /// 设置菜单项单击事件 要在派生窗体中重写实现功能
  82. /// </summary>
  83. /// <param name="menuItem"></param>
  84. protected virtual void SetMenuItem_Click(ToolStripMenuItem menuItem)
  85. {
  86. //请在派生窗体中重写实现功能
  87. }
  88. /// <summary>
  89. /// 获取菜单分隔线
  90. /// </summary>
  91. /// <param name="item"></param>
  92. /// <returns></returns>
  93. protected ToolStripSeparator GetToolStripSeparator(LYFZ.Model.Model_SystemToolsMenu.Item item)
  94. {
  95. ToolStripSeparator spItem = new ToolStripSeparator();
  96. spItem.Name = item.KeyName;
  97. return spItem;
  98. }
  99. /// <summary>
  100. /// 邦定菜单数据
  101. /// </summary>
  102. public void BondingMenuData(List<LYFZ.Model.Model_SystemToolsMenu.Item> MenuItems)
  103. {
  104. try
  105. {
  106. this.Items.Clear();
  107. if (MenuItems != null)
  108. {
  109. foreach (LYFZ.Model.Model_SystemToolsMenu.Item item in MenuItems)
  110. {
  111. string ToolStripItemType = item.Type.ToLower();
  112. switch (ToolStripItemType)
  113. {
  114. case "separator":
  115. this.Items.Add(GetToolStripSeparator(item));
  116. break;
  117. default:
  118. if (!item.Disable)
  119. this.Items.Add(GetToolStripMenuItem(item));
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. catch { }
  126. }
  127. List<LYFZ.Model.Model_SystemToolsMenu.Item> MenuItems = null;
  128. /// <summary>
  129. /// 邦定菜单数据
  130. /// </summary>
  131. public void BondingMenuData(Type enumType)
  132. {
  133. //“Separator”分隔线 “MenuItem”菜单项
  134. this.MenuItems = new List<LYFZ.Model.Model_SystemToolsMenu.Item>();
  135. foreach (string value in Enum.GetNames(enumType))
  136. {
  137. object command = Enum.Parse(enumType,value);
  138. string menuText = LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(command);
  139. AddSystemToolsMenuItem(menuText, command);
  140. }
  141. BondingMenuData(this.MenuItems);
  142. }
  143. /// <summary>
  144. /// 添加菜单项
  145. /// </summary>
  146. /// <param name="text"></param>
  147. /// <param name="command"></param>
  148. /// <param name="item"></param>
  149. void AddSystemToolsMenuItem(string text, object command, LYFZ.Model.Model_SystemToolsMenu.Item item = null)
  150. {
  151. LYFZ.Model.Model_SystemToolsMenu.Item ParentItem = null;
  152. string[] temps = text.Split('>');
  153. List<LYFZ.Model.Model_SystemToolsMenu.Item> ContainsMenuItems = this.MenuItems;
  154. if (item != null)
  155. {
  156. ContainsMenuItems = item.ChildItems;
  157. }
  158. if (temps.Length > 1)
  159. {
  160. for (int i = 0; i < ContainsMenuItems.Count; i++)
  161. {
  162. LYFZ.Model.Model_SystemToolsMenu.Item tempItem = ContainsMenuItems[i];
  163. if (tempItem.Text.Contains(temps[0].Replace("|", "")))
  164. {
  165. if (tempItem.IsChildMenu)
  166. {
  167. ParentItem = tempItem;
  168. }
  169. }
  170. }
  171. if (ParentItem == null)
  172. {
  173. ParentItem = GetSystemToolsMenuItem(temps[0].Replace("|", ""), isChildItem: true);
  174. ContainsMenuItems.Add(ParentItem);
  175. }
  176. if (temps[0].Contains("|"))
  177. {
  178. ContainsMenuItems.Add(GetSystemToolsMenuItem(type: "Separator"));
  179. }
  180. AddSystemToolsMenuItem(text.Replace(temps[0] + ">", ""), command, ParentItem);
  181. }
  182. else
  183. {
  184. ContainsMenuItems.Add(GetSystemToolsMenuItem(temps[0].Replace("|", ""), command.ToString()));
  185. if (temps[0].Contains("|"))
  186. {
  187. ContainsMenuItems.Add(GetSystemToolsMenuItem(type: "Separator"));
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// 获取菜单Item对象
  193. /// </summary>
  194. /// <param name="text">显示文本</param>
  195. /// <param name="command">执行命令</param>
  196. /// <param name="type">菜单类型 “Separator”分隔线 “MenuItem”菜单项</param>
  197. /// <param name="iconPath">图标路径</param>
  198. /// <returns></returns>
  199. LYFZ.Model.Model_SystemToolsMenu.Item GetSystemToolsMenuItem(string text = "", string command = "", string type = "MenuItem", bool isChildItem = false, string iconPath = "")
  200. {
  201. LYFZ.Model.Model_SystemToolsMenu.Item item = new LYFZ.Model.Model_SystemToolsMenu.Item();
  202. item.Text = text;
  203. item.KeyName = command;
  204. item.Type = type;
  205. item.IsChildMenu = isChildItem;
  206. return item;
  207. }
  208. }
  209. }