123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.ComponentModel;
- namespace LYFZ.ComponentLibrary
- {
- public class ContextMenuStripEx:System.Windows.Forms.ContextMenuStrip
- {
- public ContextMenuStripEx(System.ComponentModel.IContainer components)
- : base(components)
- {
- }
- public ContextMenuStripEx()
- {
- }
- /// <summary>
- /// 获取菜单项并包括所有子项
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- protected ToolStripMenuItem GetToolStripMenuItem(LYFZ.Model.Model_SystemToolsMenu.Item item)
- {
- ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Text);
- menuItem.Image = LYFZ.WinAPI.CustomPublicMethod.GetMenuItemImage(item.Icon);
- menuItem.Name = item.KeyName;
- menuItem.Tag = item;
- SetDefaultChecked(menuItem, item.OpenMode);
- SetMenuItem_Click(menuItem);
- if (item.IsChildMenu)
- {
- foreach (LYFZ.Model.Model_SystemToolsMenu.Item subItem in item.ChildItems)
- {
- if (subItem.Type.ToLower() == "separator")
- {
- menuItem.DropDownItems.Add(GetToolStripSeparator(subItem));
- }
- else
- {
- if (!item.Disable)
- {
- ToolStripMenuItem subMenuItem = GetToolStripMenuItem(subItem);
- menuItem.DropDownItems.Add(subMenuItem);
- }
- }
- }
- }
- return menuItem;
- }
- /// <summary>
- /// 设置默认是否选择
- /// </summary>
- /// <param name="menuItem"></param>
- private void SetDefaultChecked(ToolStripMenuItem menuItem, string openMode)
- {
- LYFZ.BLL.BLL_Config bllConfig = new BLL.BLL_Config();
- LYFZ.Model.Model_Config modelConfig = bllConfig.GetModel_SysConfig();
- switch (openMode)
- {
- case "SetShowToolsDescription":
- menuItem.Checked = bllConfig.Get_IsShowToolsDescription();
- break;
- case "SetInterfaceSelect":
- try
- {
- string thisInterface = bllConfig.GetInterfaceSelect();
- string menuItemInterface = menuItem.Name.Split('_')[0];
- if (thisInterface.ToLower() == menuItemInterface.ToLower())
- {
- menuItem.Checked = true;
- }
- }
- catch { }
- break;
- }
- }
- /// <summary>
- /// 设置菜单项单击事件 要在派生窗体中重写实现功能
- /// </summary>
- /// <param name="menuItem"></param>
- protected virtual void SetMenuItem_Click(ToolStripMenuItem menuItem)
- {
- //请在派生窗体中重写实现功能
- }
- /// <summary>
- /// 获取菜单分隔线
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- protected ToolStripSeparator GetToolStripSeparator(LYFZ.Model.Model_SystemToolsMenu.Item item)
- {
- ToolStripSeparator spItem = new ToolStripSeparator();
- spItem.Name = item.KeyName;
- return spItem;
- }
-
- /// <summary>
- /// 邦定菜单数据
- /// </summary>
- public void BondingMenuData(List<LYFZ.Model.Model_SystemToolsMenu.Item> MenuItems)
- {
- try
- {
- this.Items.Clear();
- if (MenuItems != null)
- {
- foreach (LYFZ.Model.Model_SystemToolsMenu.Item item in MenuItems)
- {
- string ToolStripItemType = item.Type.ToLower();
- switch (ToolStripItemType)
- {
- case "separator":
- this.Items.Add(GetToolStripSeparator(item));
- break;
- default:
- if (!item.Disable)
- this.Items.Add(GetToolStripMenuItem(item));
- break;
- }
- }
- }
- }
- catch { }
- }
- List<LYFZ.Model.Model_SystemToolsMenu.Item> MenuItems = null;
- /// <summary>
- /// 邦定菜单数据
- /// </summary>
- public void BondingMenuData(Type enumType)
- {
- //“Separator”分隔线 “MenuItem”菜单项
- this.MenuItems = new List<LYFZ.Model.Model_SystemToolsMenu.Item>();
- foreach (string value in Enum.GetNames(enumType))
- {
- object command = Enum.Parse(enumType,value);
- string menuText = LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(command);
- AddSystemToolsMenuItem(menuText, command);
- }
- BondingMenuData(this.MenuItems);
- }
- /// <summary>
- /// 添加菜单项
- /// </summary>
- /// <param name="text"></param>
- /// <param name="command"></param>
- /// <param name="item"></param>
- void AddSystemToolsMenuItem(string text, object command, LYFZ.Model.Model_SystemToolsMenu.Item item = null)
- {
- LYFZ.Model.Model_SystemToolsMenu.Item ParentItem = null;
- string[] temps = text.Split('>');
- List<LYFZ.Model.Model_SystemToolsMenu.Item> ContainsMenuItems = this.MenuItems;
- if (item != null)
- {
- ContainsMenuItems = item.ChildItems;
- }
- if (temps.Length > 1)
- {
- for (int i = 0; i < ContainsMenuItems.Count; i++)
- {
- LYFZ.Model.Model_SystemToolsMenu.Item tempItem = ContainsMenuItems[i];
- if (tempItem.Text.Contains(temps[0].Replace("|", "")))
- {
- if (tempItem.IsChildMenu)
- {
- ParentItem = tempItem;
- }
- }
- }
- if (ParentItem == null)
- {
- ParentItem = GetSystemToolsMenuItem(temps[0].Replace("|", ""), isChildItem: true);
- ContainsMenuItems.Add(ParentItem);
- }
- if (temps[0].Contains("|"))
- {
- ContainsMenuItems.Add(GetSystemToolsMenuItem(type: "Separator"));
- }
- AddSystemToolsMenuItem(text.Replace(temps[0] + ">", ""), command, ParentItem);
- }
- else
- {
- ContainsMenuItems.Add(GetSystemToolsMenuItem(temps[0].Replace("|", ""), command.ToString()));
- if (temps[0].Contains("|"))
- {
- ContainsMenuItems.Add(GetSystemToolsMenuItem(type: "Separator"));
- }
- }
- }
- /// <summary>
- /// 获取菜单Item对象
- /// </summary>
- /// <param name="text">显示文本</param>
- /// <param name="command">执行命令</param>
- /// <param name="type">菜单类型 “Separator”分隔线 “MenuItem”菜单项</param>
- /// <param name="iconPath">图标路径</param>
- /// <returns></returns>
- LYFZ.Model.Model_SystemToolsMenu.Item GetSystemToolsMenuItem(string text = "", string command = "", string type = "MenuItem", bool isChildItem = false, string iconPath = "")
- {
- LYFZ.Model.Model_SystemToolsMenu.Item item = new LYFZ.Model.Model_SystemToolsMenu.Item();
- item.Text = text;
- item.KeyName = command;
- item.Type = type;
- item.IsChildMenu = isChildItem;
- return item;
- }
- }
- }
|