建立权限树TreeView

来源:互联网 发布:淘宝假冒品牌仅退款 编辑:程序博客网 时间:2024/06/05 23:59

 EUserType.cs

 public enum EUserType    {        SystemManager = 1,        CompanyManager = 2,        DepartmentManager = 3,        Employeer = 4,    }


PermissionCtrl.cs

    public static class FunctionNO    {        public static readonly int SimInfo = 1;        public static readonly int DepartmentInfo = 2;        public static readonly int PermissionCtrl = 3;        public static readonly int UserInfo = 4;        public static readonly int EmployeeInfo = 5;        public static readonly int WorkGroupMember = 6;        public static readonly int ProjectInfo = 7;        public static readonly int WorkGroupInfo = 8;        public static readonly int ProjectProgress = 9;        public static readonly int GpsDeviceMaintain = 10;        public static readonly int GpsDeviceMaintainLog = 11;        public static readonly int GpsDeviceRepair = 12;        public static readonly int GpsDeviceInfo = 13;        public static readonly int GpsCustomerInfo = 14;        public static readonly int GpsDeviceRepairLog = 15;        public static readonly int GpsVehicleInfo = 16;        public static readonly int PartIn = 17;        public static readonly int PartInfo = 18;        public static readonly int PartInLog = 19;        public static readonly int PartOut = 20;        public static readonly int PartOutLog = 21;        public static readonly int PartSupplierInfo = 22;        public static readonly int PartTypeInfo = 23;        public static readonly int Job = 24;        public static readonly int Remind = 25;        public static readonly int WorkLog = 26;        public static readonly int Request = 27;        public static readonly int DiscussFamily = 28;        public static readonly int ViewDiscussSubject = 29;        public static readonly int Home = 30;    }    public static class OperateNO    {        public static readonly int Add = 0;        public static readonly int Update = 1;        public static readonly int Delete = 2;        public static readonly int Querry = 3;    }    //PermissionString:3个字符为一个功能界面的控制,第1个字符代表功能权限    //第2,3字符为十六进制字符串,共16位,每1位代表相应的操作权限,其中第0到3位为公用,分别代表添加、修改、删除    public class PermissionCtrl    {        private int _UserType;        private string _PermissionString;        public PermissionCtrl(string permissionString)        {            _UserType = EUserType.Employeer.GetHashCode();//默认普通操作员            _PermissionString = permissionString;        }        public int UserType        {            get { return _UserType; }            set { _UserType = value; }        }        public string PermissionString        {            get { return _PermissionString; }            set { _PermissionString = value; }        }        private bool NeedCheck()        {            if (_UserType == EUserType.SystemManager.GetHashCode() || _UserType == EUserType.CompanyManager.GetHashCode())            {                return false;            }            return true;        }        public bool FunctionPermission(int funcNO)        {            if (!NeedCheck())            {                return true;            }            if (funcNO < 0) return false;            if (funcNO == 0) return true;//0号功能,默认允许            try            {                if (_PermissionString[(funcNO - 1) * 3] == 0x30) //对应位置的字符为"0"                {                    return false;                }            }            catch (Exception)            {                return false;            }            return true;        }        public bool OperatePermission(int funcNO, int operateNO)        {            if (!NeedCheck())            {                return true;            }            try            {                string funcPermissionFullStr;                string funcPermissionStr;                string operatePermissionStr;                funcPermissionFullStr = _PermissionString.Substring((funcNO - 1) * 3, 3);                funcPermissionStr = funcPermissionFullStr.Substring(0, 1);                operatePermissionStr = funcPermissionFullStr.Substring(1, 2);                int funcPermission = Int32.Parse(funcPermissionStr, System.Globalization.NumberStyles.HexNumber);                if (funcPermission == 0)//功能权限不允许                {                    return false;                }                int opeartePermission = Int32.Parse(operatePermissionStr, System.Globalization.NumberStyles.HexNumber);                if ((opeartePermission & (1 << operateNO)) == 0)//操作权限不允许                {                    return false;                }            }            catch (Exception)            {                return false;            }            return true;        }    }


Prj.cs

public static class Prj    {        public static readonly string System = "MMS";        public static readonly string Program = "Client";        public static readonly string Version = "V1.0 R01";        public static PermissionCtrl PermissionCtrl;        public static void Init()        {            try            {                PermissionCtrl = new PermissionCtrl("");            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }    }


 

Tree.cs

    public class FunctionInfo    {        public int Level;        public string ParentName;        public string Name;        public string Text;        public int FuncNO;        public FunctionInfo(int level, string parentName, string name, string text, int funcNO)        {            Level = level;            ParentName = parentName;            Name = name;            Text = text;            FuncNO = funcNO;        }    }    public class FunctionTree    {        public List<FunctionInfo> FunctonInfos;        public List<AppTreeNode> TreeNodes;        public FunctionTree()        {            FunctonInfos = new List<FunctionInfo>();            FunctonInfos.Add(new FunctionInfo(0, "Root", "Root", "功能列表", 0));            FunctonInfos.Add(new FunctionInfo(1, "Root", "Home", "首页", 0));            FunctonInfos.Add(new FunctionInfo(1, "Root", "SimInfo", "Sim卡", FunctionNO.SimInfo));            FunctonInfos.Add(new FunctionInfo(1, "Root", "PersonMange", "人员管理", 0));            FunctonInfos.Add(new FunctionInfo(1, "Root", "ProjectManage", "项目管理", 0));            FunctonInfos.Add(new FunctionInfo(1, "Root", "GPSManage", "GPS管理", 0));            FunctonInfos.Add(new FunctionInfo(1, "Root", "Part", "库存", 0));            FunctonInfos.Add(new FunctionInfo(1, "Root", "WorkMangement", "工作管理", 0));            FunctonInfos.Add(new FunctionInfo(1, "Root", "Discuss", "讨论", 0));            //添加人员管理的子节点            FunctonInfos.Add(new FunctionInfo(2, "PersonMange", "DepartmentInfo", "部门管理", FunctionNO.DepartmentInfo));            FunctonInfos.Add(new FunctionInfo(2, "PersonMange", "EmployeeInfo", "员工", FunctionNO.EmployeeInfo));            FunctonInfos.Add(new FunctionInfo(2, "PersonMange", "UserInfo", "用户", FunctionNO.UserInfo));            FunctonInfos.Add(new FunctionInfo(2, "PersonMange", "PermissionCtrl", "权限设置", FunctionNO.PermissionCtrl));            //添加项目管理的子节点                        FunctonInfos.Add(new FunctionInfo(2, "ProjectManage", "ProjectInfo", "项目", FunctionNO.ProjectInfo));            FunctonInfos.Add(new FunctionInfo(2, "ProjectManage", "WorkGroupInfo", "项目小组", FunctionNO.WorkGroupInfo));            FunctonInfos.Add(new FunctionInfo(2, "ProjectManage", "WorkGroupMember", "小组成员", FunctionNO.WorkGroupMember));            FunctonInfos.Add(new FunctionInfo(2, "ProjectManage", "ProjectProgress", "项目进度", FunctionNO.ProjectProgress));            //添加GPS管理的子节点            FunctonInfos.Add(new FunctionInfo(2, "GPSManage", "GpsCustomerInfo", "GPS客户", FunctionNO.GpsCustomerInfo));            FunctonInfos.Add(new FunctionInfo(2, "GPSManage", "GpsVehicleInfo", "GPS车辆信息", FunctionNO.GpsVehicleInfo));            FunctonInfos.Add(new FunctionInfo(2, "GPSManage", "GpsDeviceInfo", "GPS终端", FunctionNO.GpsDeviceInfo));            FunctonInfos.Add(new FunctionInfo(2, "GPSManage", "GpsDeviceMaintain", "GPS终端维护", FunctionNO.GpsDeviceMaintain));            FunctonInfos.Add(new FunctionInfo(2, "GPSManage", "GpsDeviceRepair", "GPS终端维修", FunctionNO.GpsDeviceRepair));            FunctonInfos.Add(new FunctionInfo(2, "GPSManage", "GpsDeviceMaintainLog", "GPS终端维护记录", FunctionNO.GpsDeviceMaintainLog));            FunctonInfos.Add(new FunctionInfo(2, "GPSManage", "GpsDeviceRepairLog", "GPS终端维修记录", FunctionNO.GpsDeviceRepairLog));            //添加库存管理的子节点            FunctonInfos.Add(new FunctionInfo(2, "Part", "PartTypeInfo", "零件类型信息", FunctionNO.PartTypeInfo));            FunctonInfos.Add(new FunctionInfo(2, "Part", "PartSupplierInfo", "零件供应商信息", FunctionNO.PartSupplierInfo));            FunctonInfos.Add(new FunctionInfo(2, "Part", "PartInfo", "零件信息", FunctionNO.PartInfo));            FunctonInfos.Add(new FunctionInfo(2, "Part", "PartIn", "零件入库", FunctionNO.PartIn));            FunctonInfos.Add(new FunctionInfo(2, "Part", "PartOut", "零件出库", FunctionNO.PartOut));            FunctonInfos.Add(new FunctionInfo(2, "Part", "PartInLog", "零件入库记录", FunctionNO.PartInLog));            FunctonInfos.Add(new FunctionInfo(2, "Part", "PartOutLog", "零件出库记录", FunctionNO.PartOutLog));            //添加工作管理的子节点            FunctonInfos.Add(new FunctionInfo(2, "WorkMangement", "Job", "日常工作安排", FunctionNO.Job));            FunctonInfos.Add(new FunctionInfo(2, "WorkMangement", "Remind", "提醒", FunctionNO.Remind));            FunctonInfos.Add(new FunctionInfo(2, "WorkMangement", "Request", "请求", FunctionNO.Request));            FunctonInfos.Add(new FunctionInfo(2, "WorkMangement", "WorkLog", "工作日志", FunctionNO.WorkLog));            //添加讨论的子节点            FunctonInfos.Add(new FunctionInfo(2, "Discuss", "DiscussFamily", "讨论板块", FunctionNO.DiscussFamily));            FunctonInfos.Add(new FunctionInfo(2, "Discuss", "ViewDiscussSubject", "讨论", FunctionNO.ViewDiscussSubject));        }        public List<AppTreeNode> BuildTreeNodes(PermissionCtrl permissionCtrl)        {            TreeNodes = new List<AppTreeNode>();            if (permissionCtrl == null)            {                //默认全部允许                foreach (var item in FunctonInfos)                {                    TreeNodes.Add(new AppTreeNode(item.Level, item.ParentName, item.Name, item.Text, true, item));                }            }            else            {                //根据权限字符串设置允许                foreach (var item in FunctonInfos)                {                    TreeNodes.Add(new AppTreeNode(item.Level, item.ParentName, item.Name, item.Text, permissionCtrl.FunctionPermission(item.FuncNO), item));                }            }            return TreeNodes;        }    }    public class AppTreeNode    {        public int Level;        public string ParentName;        public string Name;        public string Text;        public bool Enable;        public object Info;        public AppTreeNode(int level, string parentName, string name, string text, bool enable, object info)        {            Level = level;            ParentName = parentName;            Name = name;            Enable = enable;            Info = info;            Text = text;        }    }


 

 

TreeViewHelper.cs

 public static class TreeViewHelper    {        private static void BuildChildNode(TreeNode parentNode, int level, List<AppTreeNode> appTreeNodes)        {            TreeNode node;            foreach (var item in appTreeNodes)            {                if (item.Level != level)//不是本级                {                    continue;                }                if (item.ParentName != parentNode.Name)//父节点不匹配                {                    continue;                }                node = new TreeNode();                node.Name = item.Name;                node.Text = item.Text;                node.Tag = item.Info;                if (!item.Enable)                {                    node.ForeColor = Color.Gray;                }                parentNode.Nodes.Add(node);                BuildChildNode(parentNode.Nodes[item.Name], level + 1, appTreeNodes);//Build新加节点的下一级            }        }        public static void BuildTree(TreeView treeView, bool checkBoxs, List<AppTreeNode> appTreeNodes)        {            treeView.CheckBoxes = checkBoxs;            treeView.Nodes.Clear();            TreeNode node;            //level 0            foreach (var item in appTreeNodes)            {                if (item.Level != 0)                {                    continue;                }                node = new TreeNode();                node.Name = item.Name;                node.Text = item.Text;                node.Tag = item.Info;                if (!item.Enable)                {                    node.ForeColor = Color.Gray;                }                treeView.Nodes.Add(node);                BuildChildNode(treeView.Nodes[item.Name], 1, appTreeNodes);//建立第一级子节点            }        }        /// <summary>        /// 将TreeNodeCollection中的节点存入 List<TreeNode>        /// </summary>        /// <param name="treeNodes"></param>        /// <param name="tc"></param>        public static void GetTreeNodes(ref List<TreeNode> treeNodes, TreeNodeCollection tc)        {            if (tc == null) return;            foreach (TreeNode tn in tc)            {                if (tn != null)                {                    treeNodes.Add(tn);                    GetTreeNodes(ref treeNodes, tn.Nodes);                }            }        }    }


 

 

Form1.cs

 public partial class Form1 : Form    {        TreeView treeView1;        public Form1()        {            InitializeComponent();            treeView1 = new TreeView();            this.Controls.Add(this.treeView1);        }        private void Form1_Load(object sender, EventArgs e)        {            this.WindowState = FormWindowState.Maximized;            this.treeView1.Size = new System.Drawing.Size(200, SystemInformation.WorkingArea.Height);            FunctionTree functionTree = new FunctionTree();            List<AppTreeNode> appTreeNodes=functionTree.BuildTreeNodes(Prj.PermissionCtrl);            TreeViewHelper.BuildTree(treeView1, false, appTreeNodes);            this.treeView1.ExpandAll();        }    }


 

原创粉丝点击