WINFORM权限系统开发系列教程(六)主窗口

2021-01-09 19:31

阅读:527

标签:child   tor   信息   win   parentId   cte   exit   drawing   creat   

主窗口效果

技术图片

 

 

主窗口代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Xwy.WindowsFormsApp.Common;
using Xwy.WindowsFormsApp.DAL;
using Xwy.WindowsFormsApp.Models;

namespace Xwy.WindowsFormsApp
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        UserDAL userDAL=new UserDAL();
        MenuDAL menuDAL = new MenuDAL();
        int userId = 0;

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //获取传递过来的UserId
            if (this.Tag != null)
            {
                userId = this.Tag.GetInt();
            }
            if (userId > 0)
            {
                //获取用户的角色信息
                List roles = userDAL.GetUserRoles(userId);
                bool bl = false;
                List menuList = new List();
                foreach (RoleInfoModel role in roles)
                {
                    if (role.IsAdmin==1)
                    {
                        //加载所有菜单列表
                        bl = true;
                        menuList = menuDAL.GetUserMenuList(role.RoleId.ToString()); 
                        break;
                    }
                }
                if (!bl)
                {
                    string roleIds = string.Join(",", roles.Select(r => r.RoleId));
                    //权限菜单
                    menuList = menuDAL.GetUserMenuList(roleIds);
                }
                //加载菜单树
                TreeNode rootNode = new TreeNode();
                rootNode.Name = "0";
                rootNode.Text = "权限管理系统";
                tvMenus.Nodes.Add(rootNode);
                CreateNode(menuList, rootNode, 0);
                tvMenus.ExpandAll();
            }
        }
   
        private void CreateNode(List menuList,TreeNode pNode,int parentId)
        {
            if (menuList.Count > 0)
            {
                //获取所有的子菜单
                var childList = menuList.Where(m => m.ParentId == parentId).ToList();
                foreach (MenuInfoModel menu in childList)
                {
                    TreeNode tn = new TreeNode();
                    tn.Name = menu.MenuId.ToString();
                    tn.Text = menu.MenuName;
                    if (!string.IsNullOrEmpty(menu.FrmName))
                    {
                        tn.Tag = menu.FrmName;
                    }
                    
                    if (pNode != null)
                    {
                        pNode.Nodes.Add(tn);//节点添加
                    }
                    else
                    {
                        tvMenus.Nodes.Add(tn);
                    }
                    CreateNode(menuList, tn, menu.MenuId);
                }
            }
            
        }

        private void tvMenus_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selNode = tvMenus.SelectedNode;
            if (selNode.Tag != null)
            {
                string url = selNode.Tag.ToString();
                Form f = null;
                bool bl = false;
                //判断当前我要实例化的窗口是否已经打开
                foreach (Form frm in Application.OpenForms)
                {
                    if (frm.Name == url)
                    {
                        bl = true;
                        f = frm;
                        f.Show();
                        f.Activate();
                        break;

                    }
                }
                if (!bl)
                {
                    //获取程序集的名称
                    string assName = this.GetType().Assembly.GetName().Name;
                    //f = (Form)Activator.CreateInstance(assName,assName+"."+url).Unwrap();或者
                    f = (Form)Activator.CreateInstance(Type.GetType(assName + "." + url));
                    f.MdiParent = this;
                    f.Name = url;
                    f.Show();
                }
              
            }
            
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //让用户确认到底要不要退出,是--退出 否--不退出
            if (MsgBoxHelper.MsgBoxConfirm("退出系统", "您确定要退出系统吗") == DialogResult.Yes)
            {
                //Application.Exit();//有问题,弹出两次
                Application.ExitThread();
            }
            else
            {
                e.Cancel = true;//如果没有这一句,主页面仍然关闭,但没有退出应用程序
            }
        }
    }
}

 

WINFORM权限系统开发系列教程(六)主窗口

标签:child   tor   信息   win   parentId   cte   exit   drawing   creat   

原文地址:https://www.cnblogs.com/xiewenyu/p/13083563.html


评论


亲,登录后才可以留言!