CSFrameworkV6旗舰版|动态加载菜单Demo|C/S开发框架
作者:csframework|C/S框架网  发布日期:2024/10/20 11:46:10

CSFrameworkV6旗舰版|动态加载菜单Demo|C/S开发框架

CSFrameworkV6旗舰版-动态加载菜单Demo

菜单效果

CSFrameworkV6旗舰版|动态加载菜单Demo|C/S开发框架

从服务器读取菜单数据

C# 全选
        /// <summary>
        /// 从服务器读取菜单数据
        /// </summary>
        private void LoadMenuFromServer()
        {
            const string rootMenuId = "HFSMTDEV"; 
            const int ModuleID= 11;

            //模拟的菜单数据
            List<MesMenuData> list = new List<MesMenuData>
            {
                new MesMenuData{ MenuId="SFC0000", MenuName="SFC0000", MenuParentId="HFSMTDEV", FormNamespace="SFC0000.frmMain", DefaultAuths=15 },
                new MesMenuData{ MenuId="SFC0001", MenuName="SFC0001", MenuParentId="HFSMTDEV", FormNamespace="SFC0001.frmMain", DefaultAuths=15 },
                new MesMenuData{ MenuId="SFC000101", MenuName="SFC000101", MenuParentId="SFC0001", FormNamespace="SFC000101.frmMain", DefaultAuths=15 },
                new MesMenuData{ MenuId="SFC000102", MenuName="SFC000102", MenuParentId="SFC0001", FormNamespace="SFC000102.frmMain", DefaultAuths=15 },
                new MesMenuData{ MenuId="SFC0002", MenuName="SFC0002", MenuParentId="HFSMTDEV", FormNamespace="SFC0002.frmMain", DefaultAuths=15 },
                new MesMenuData{ MenuId="SFC000201", MenuName="SFC000201", MenuParentId="SFC0002", FormNamespace="SFC000201.frmMain", DefaultAuths=15 },
                new MesMenuData{ MenuId="SFC0003", MenuName="SFC0003", MenuParentId="HFSMTDEV", FormNamespace="SFC0003.frmMain", DefaultAuths=15 },
            };

            //取当前模块的菜单根节点(HFSMTDEV)加载
            _mesLoader.LoadMenu(rootMenuId, this.menuMainMESProduction, list, OnMenuClick, ModuleID);
        }

递归加载模块主窗体的菜单

C#全选
        /// <summary>
        /// 递归加载模块主窗体的菜单
        /// </summary>
        /// <param name="rootMenuId">菜单根节点编码</param>
        /// <param name="rootMenu">模块主菜单</param>
        /// <param name="allMenus">菜单数据</param>
        /// <param name="clickEvent">菜单click事件</param>
        /// <param name="moduleId">当前大模块的ID</param>
        public void LoadMenu(string rootMenuId, ToolStripMenuItem rootMenu, List<MesMenuData> allMenus, 
            EventHandler? clickEvent, int moduleId)
        {
            _listMenus = allMenus;
            var rootNodes = allMenus.Where(w => w.MenuParentId == rootMenuId).ToList();
            LoadSubMenu(rootNodes, rootMenu);

            //递归加载菜单
            void LoadSubMenu(List<MesMenuData> nodes, ToolStripMenuItem parentMenu)
            {
                foreach (var node in nodes)
                {
                    var menu = CreateMenu(node);
                    parentMenu.DropDownItems.Add(menu);
                    var childs = allMenus.Where(w => w.MenuParentId == node.MenuId).ToList();
                    if (childs.Count > 0)
                    {
                        LoadSubMenu(childs, menu);
                    }
                    else
                    {
                        menu.Click += clickEvent;
                    }
                }
            }

            //创建菜单组件
            ToolStripMenuItem CreateMenu(MesMenuData item)
            {
                var subMenu = new ToolStripMenuItem
                {
                    Name = "menu_" + item.MenuId.Replace(" ", ""),
                    Text = item.MenuName,
                    Tag = new MenuItemTag(MenuType.ItemOwner, moduleId, item.DefaultAuths),
                    AccessibleName = item.MenuId, //作为缓存变量,缓存菜单ID
                };                
                return subMenu;
            }
        }

菜单数据模型

C# 全选
    /// <summary>
    /// 菜单数据模型
    /// </summary>
    public class MesMenuData
    {
        /// <summary>
        /// 菜单编号,如:SFC6040
        /// </summary>
        public string MenuId { get; set; }

        /// <summary>
        /// 父级菜单编号,如:SFC
        /// </summary>
        public string MenuParentId { get; set; }

        /// <summary>
        /// 菜单名称,如:SFC6040
        /// </summary>
        public string MenuName { get; set; }

        /// <summary>
        /// 窗体命名空间,如:SFC6040.frmMain
        /// </summary>
        public string FormNamespace { get; set; }

        /// <summary>
        /// 窗体的默认权限,比如:增\删\改\查=1+2+4+8=15
        /// </summary>
        public int DefaultAuths { get; set; }

    }
C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务
上一篇 下一篇