提供灵活的权限定义及控制方案
在前面说到Common.Windows的时候,把权限这块给说漏了,今天说在这里补上。
首先,保存每个用户的权限的字段是binary,每个权限又可以分为16个子权限,即16的二次方。这比如一个列表页面,可以再分为添加、删除、修改等子权限。
我的权限处理流程大概是这样的:将预定义权限保存在xml文件里的,经过一个中间转换程序,将其序列化并压缩为bin文件,在程序启动时,读出权限配置,并通过指定的组别过滤该组别的权限集合,生成菜单栏及工具栏。
下面是权限定义类:
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace FaibClass.Common.Windows
{
/// <summary>
/// 权限控制基类
/// </summary>
[Serializable]
public class PurviewBase
{
private PurviewCollection items;
/// <summary>
/// 子项
/// </summary>
public PurviewCollection Items
{
get
{
if (items == null)
items = new PurviewCollection();
return items;
}
}
}
/// <summary>
/// 权限集合
/// </summary>
[Serializable]
public class PurviewCollection : List<PurviewItemBase>
{
public PurviewItemBase this[string key]
{
get
{
foreach(PurviewItemBase item in this)
{
if (item.Key.Equals(key))
{
return item;
}
}
return null;
}
}
/// <summary>
/// 深度查询权限
/// </summary>
/// <param name="path">查询的路径</param>
/// <returns></returns>
public PurviewItemBase SelectPath(string path)
{
PurviewCollection c = this;
PurviewItemBase item = null;
foreach(string p in path.Split('/'))
{
item = c[p];
if (item == null) break;
c = item.Items;
}
return item;
}
/// <summary>
/// 克隆一个集合
/// </summary>
/// <returns></returns>
public PurviewCollection Clone()
{
PurviewCollection pc = new PurviewCollection();
CloneItems(this, pc);
return pc;
}
/// <summary>
/// 克隆一个集合中的所有元素
/// </summary>
/// <param name="pc">源集合</param>
/// <param name="pc1">目标集合</param>
/// <returns></returns>
private void CloneItems(PurviewCollection pc, PurviewCollection pc1)
{
foreach (PurviewItemBase item in pc)
{
PurviewItemBase item1 = item.Clone();
pc1.Add(item1);
CloneItems(item.Items, item1.Items);
}
}
/// <summary>
/// 排序
/// </summary>
public void Sort()
{
base.Sort(new PurviewComparer());
foreach(PurviewItemBase pur in this)
if (pur.Items.Count > 0)
pur.Items.Sort();
}
}
/// <summary>
/// 权限基类
/// </summary>
[Serializable]
public class PurviewItemBase
{
private int m_value;
private string m_key;
private string m_name;
private bool m_user;
private bool m_menu;
private bool m_toolbar;
private int m_parentvalue;
private int m_sort;
private string m_shortcut;
private PurviewCollection m_items;
/// <summary>
/// 值控制
/// </summary>
public int Value
{
get { return m_value; }
set { m_value = value; }
}
/// <summary>
/// 是否用户控制
/// </summary>
public bool User
{
get { return m_user; }
set { m_user = value; }
}
/// <summary>
/// 是否显示在工具栏
/// </summary>
public bool Toolbar
{
get { return m_toolbar; }
set { m_toolbar = value; }
}
/// <summary>
/// 是否显示菜单
/// </summary>
public bool Menu
{
get { return m_menu; }
set { m_menu = value; }
}
/// <summary>
/// 父权限值
/// </summary>
public int ParentValue
{
get { return m_parentvalue; }
set { m_parentvalue = value; }
}
/// <summary>
/// 键
/// </summary>
public string Key
{
get { return m_key; }
set { m_key = value; }
}
/// <summary>
/// 模块名称
/// </summary>
public string Name
{
get { return m_name; }
set { m_name = value; }
}
/// <summary>
/// 排序
/// </summary>
public int Sort
{
get { return m_sort; }
set { m_sort = value; }
}
/// <summary>
/// 快捷键
/// </summary>
public string ShortCut
{
get { return m_shortcut; }
set { m_shortcut = value; }
}
/// <summary>
/// 子项
/// </summary>
public PurviewCollection Items
{
get
{
if (m_items == null)
m_items = new PurviewCollection();
return m_items;
}
}
/// <summary>
/// 克隆新权限
/// </summary>
public PurviewItemBase Clone()
{
PurviewItemBase p = new PurviewItemBase();
if(this is PurviewItemThree)
p = new PurviewItemThree();
else if (this is PurviewItemSingle)
p = new PurviewItemSingle();
p.Key = this.Key;
p.Menu = this.Menu;
p.Name = this.Name;
p.Sort = this.Sort;
p.User = this.User;
p.Toolbar = this.Toolbar;
p.Value = this.Value;
return p;
}
}
/// <summary>
/// 单态权限
/// </summary>
[Serializable]
public class PurviewItemSingle : PurviewItemBase
{
private bool allow;
/// <summary>
/// 是否允许
/// </summary>
public bool Allow
{
get { return allow; }
set { allow = value; }
}
}
/// <summary>
/// 三态权限
/// </summary>
[Serializable]
public class PurviewItemThree : PurviewItemSingle
{
private bool m_allowAdd;
private bool m_allowModify;
private bool m_allowDelete;
/// <summary>
/// 允许添加
/// </summary>
public bool AllowAdd
{
get { return m_allowAdd; }
set { m_allowAdd = value; }
}
/// <summary>
/// 允许修改
/// </summary>
public bool AllowModify
{
get { return m_allowModify; }
set { m_allowModify = value; }
}
/// <summary>
/// 允许删除
/// </summary>
public bool AllowDelete
{
get { return m_allowDelete; }
set { m_allowDelete = value; }
}
}
/// <summary>
/// 比较器
/// </summary>
public class PurviewComparer : IComparer<PurviewItemBase>
{
public int Compare(PurviewItemBase x, PurviewItemBase y)
{
if (x == y)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
return x.Sort.CompareTo(y.Sort);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace FaibClass.Common.Windows
{
/// <summary>
/// 权限控制基类
/// </summary>
[Serializable]
public class PurviewBase
{
private PurviewCollection items;
/// <summary>
/// 子项
/// </summary>
public PurviewCollection Items
{
get
{
if (items == null)
items = new PurviewCollection();
return items;
}
}
}
/// <summary>
/// 权限集合
/// </summary>
[Serializable]
public class PurviewCollection : List<PurviewItemBase>
{
public PurviewItemBase this[string key]
{
get
{
foreach(PurviewItemBase item in this)
{
if (item.Key.Equals(key))
{
return item;
}
}
return null;
}
}
/// <summary>
/// 深度查询权限
/// </summary>
/// <param name="path">查询的路径</param>
/// <returns></returns>
public PurviewItemBase SelectPath(string path)
{
PurviewCollection c = this;
PurviewItemBase item = null;
foreach(string p in path.Split('/'))
{
item = c[p];
if (item == null) break;
c = item.Items;
}
return item;
}
/// <summary>
/// 克隆一个集合
/// </summary>
/// <returns></returns>
public PurviewCollection Clone()
{
PurviewCollection pc = new PurviewCollection();
CloneItems(this, pc);
return pc;
}
/// <summary>
/// 克隆一个集合中的所有元素
/// </summary>
/// <param name="pc">源集合</param>
/// <param name="pc1">目标集合</param>
/// <returns></returns>
private void CloneItems(PurviewCollection pc, PurviewCollection pc1)
{
foreach (PurviewItemBase item in pc)
{
PurviewItemBase item1 = item.Clone();
pc1.Add(item1);
CloneItems(item.Items, item1.Items);
}
}
/// <summary>
/// 排序
/// </summary>
public void Sort()
{
base.Sort(new PurviewComparer());
foreach(PurviewItemBase pur in this)
if (pur.Items.Count > 0)
pur.Items.Sort();
}
}
/// <summary>
/// 权限基类
/// </summary>
[Serializable]
public class PurviewItemBase
{
private int m_value;
private string m_key;
private string m_name;
private bool m_user;
private bool m_menu;
private bool m_toolbar;
private int m_parentvalue;
private int m_sort;
private string m_shortcut;
private PurviewCollection m_items;
/// <summary>
/// 值控制
/// </summary>
public int Value
{
get { return m_value; }
set { m_value = value; }
}
/// <summary>
/// 是否用户控制
/// </summary>
public bool User
{
get { return m_user; }
set { m_user = value; }
}
/// <summary>
/// 是否显示在工具栏
/// </summary>
public bool Toolbar
{
get { return m_toolbar; }
set { m_toolbar = value; }
}
/// <summary>
/// 是否显示菜单
/// </summary>
public bool Menu
{
get { return m_menu; }
set { m_menu = value; }
}
/// <summary>
/// 父权限值
/// </summary>
public int ParentValue
{
get { return m_parentvalue; }
set { m_parentvalue = value; }
}
/// <summary>
/// 键
/// </summary>
public string Key
{
get { return m_key; }
set { m_key = value; }
}
/// <summary>
/// 模块名称
/// </summary>
public string Name
{
get { return m_name; }
set { m_name = value; }
}
/// <summary>
/// 排序
/// </summary>
public int Sort
{
get { return m_sort; }
set { m_sort = value; }
}
/// <summary>
/// 快捷键
/// </summary>
public string ShortCut
{
get { return m_shortcut; }
set { m_shortcut = value; }
}
/// <summary>
/// 子项
/// </summary>
public PurviewCollection Items
{
get
{
if (m_items == null)
m_items = new PurviewCollection();
return m_items;
}
}
/// <summary>
/// 克隆新权限
/// </summary>
public PurviewItemBase Clone()
{
PurviewItemBase p = new PurviewItemBase();
if(this is PurviewItemThree)
p = new PurviewItemThree();
else if (this is PurviewItemSingle)
p = new PurviewItemSingle();
p.Key = this.Key;
p.Menu = this.Menu;
p.Name = this.Name;
p.Sort = this.Sort;
p.User = this.User;
p.Toolbar = this.Toolbar;
p.Value = this.Value;
return p;
}
}
/// <summary>
/// 单态权限
/// </summary>
[Serializable]
public class PurviewItemSingle : PurviewItemBase
{
private bool allow;
/// <summary>
/// 是否允许
/// </summary>
public bool Allow
{
get { return allow; }
set { allow = value; }
}
}
/// <summary>
/// 三态权限
/// </summary>
[Serializable]
public class PurviewItemThree : PurviewItemSingle
{
private bool m_allowAdd;
private bool m_allowModify;
private bool m_allowDelete;
/// <summary>
/// 允许添加
/// </summary>
public bool AllowAdd
{
get { return m_allowAdd; }
set { m_allowAdd = value; }
}
/// <summary>
/// 允许修改
/// </summary>
public bool AllowModify
{
get { return m_allowModify; }
set { m_allowModify = value; }
}
/// <summary>
/// 允许删除
/// </summary>
public bool AllowDelete
{
get { return m_allowDelete; }
set { m_allowDelete = value; }
}
}
/// <summary>
/// 比较器
/// </summary>
public class PurviewComparer : IComparer<PurviewItemBase>
{
public int Compare(PurviewItemBase x, PurviewItemBase y)
{
if (x == y)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
return x.Sort.CompareTo(y.Sort);
}
}
}
Code
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters;
using System.Xml;
using System.Text;
namespace FaibClass.Common.Windows
{
/// <summary>
/// 权限辅助类
/// </summary>
public class PurviewHelper
{
/// <summary>
/// 载入预定义权限配置
/// </summary>
/// <param name="binfile"></param>
/// <returns></returns>
public static PurviewCollection LoadDefind(string binfile)
{
PurviewCollection collection = new PurviewCollection();
BinaryFormatter bin = new BinaryFormatter();
FileStream stream = new FileStream(binfile, FileMode.Open);
DeflateStream zip = new DeflateStream(stream, CompressionMode.Decompress);
MemoryStream mstream = new MemoryStream();
int b = 0;
while ((b = zip.ReadByte()) != -1)
mstream.WriteByte((byte)b);
mstream.Seek(0, SeekOrigin.Begin);
collection = (PurviewCollection)bin.Deserialize(mstream);
zip.Close();
collection.Sort();
return collection;
}
/// <summary>
/// 根据byte[]初始化权限集合
/// </summary>
/// <param name="pc">集合</param>
/// <param name="data">权限字节数组</param>
public static void Initialize(PurviewCollection pc, byte[] data)
{
int index = 0;
InitializeData(pc, data, ref index);
}
/// <summary>
/// 构造指定长度的默认权限数据。
/// </summary>
/// <param name="length"></param>
/// <param name="value"></param>
/// <returns></returns>
public static byte[] BuildDefault(int length, int value)
{
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) bytes[i] = (byte)value;
return bytes;
}
private static void InitializeData(PurviewCollection pc, byte[] data, ref int index)
{
foreach (PurviewItemSingle pitem in pc)
{
//三态
if (pitem is PurviewItemThree)
{
PurviewItemThree three = pitem as PurviewItemThree;
three.Allow = (data[index] & 1) > 0;
if ((data[index] & 2) > 0)
three.AllowAdd = true;
if ((data[index] & 4) > 0)
three.AllowModify = true;
if ((data[index] & 8) > 0)
three.AllowDelete = true;
}
else
{
pitem.Allow = (data[index] & 1) > 0;
}
index++;
InitializeData(pitem.Items, data, ref index);
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters;
using System.Xml;
using System.Text;
namespace FaibClass.Common.Windows
{
/// <summary>
/// 权限辅助类
/// </summary>
public class PurviewHelper
{
/// <summary>
/// 载入预定义权限配置
/// </summary>
/// <param name="binfile"></param>
/// <returns></returns>
public static PurviewCollection LoadDefind(string binfile)
{
PurviewCollection collection = new PurviewCollection();
BinaryFormatter bin = new BinaryFormatter();
FileStream stream = new FileStream(binfile, FileMode.Open);
DeflateStream zip = new DeflateStream(stream, CompressionMode.Decompress);
MemoryStream mstream = new MemoryStream();
int b = 0;
while ((b = zip.ReadByte()) != -1)
mstream.WriteByte((byte)b);
mstream.Seek(0, SeekOrigin.Begin);
collection = (PurviewCollection)bin.Deserialize(mstream);
zip.Close();
collection.Sort();
return collection;
}
/// <summary>
/// 根据byte[]初始化权限集合
/// </summary>
/// <param name="pc">集合</param>
/// <param name="data">权限字节数组</param>
public static void Initialize(PurviewCollection pc, byte[] data)
{
int index = 0;
InitializeData(pc, data, ref index);
}
/// <summary>
/// 构造指定长度的默认权限数据。
/// </summary>
/// <param name="length"></param>
/// <param name="value"></param>
/// <returns></returns>
public static byte[] BuildDefault(int length, int value)
{
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) bytes[i] = (byte)value;
return bytes;
}
private static void InitializeData(PurviewCollection pc, byte[] data, ref int index)
{
foreach (PurviewItemSingle pitem in pc)
{
//三态
if (pitem is PurviewItemThree)
{
PurviewItemThree three = pitem as PurviewItemThree;
three.Allow = (data[index] & 1) > 0;
if ((data[index] & 2) > 0)
three.AllowAdd = true;
if ((data[index] & 4) > 0)
three.AllowModify = true;
if ((data[index] & 8) > 0)
three.AllowDelete = true;
}
else
{
pitem.Allow = (data[index] & 1) > 0;
}
index++;
InitializeData(pitem.Items, data, ref index);
}
}
}
}
预定义权限xml的示例:
Code
<?xml version="1.0"?>
<!-- Kye 标识
Name 名称
Value 值,可对应不同的权限组
Menu 是否创建菜单
User 是否用户控制
Three 是否三态控制(添加,修改,删除)
-->
<PurviewDefind>
<Purview Key="Member" Name="会员" ShortCut="M" Value="7" User="1" Menu="1" Sort="2">
<Purview Key="MakeCard" Name="会员办卡" Value="7" User="1" Menu="1" Toolbar="1" Sort="1"></Purview>
<Purview Key="Archives" Name="会员档案" Value="7" User="1" Menu="1" Toolbar="1" Three="1" Sort="2">
<Purview Key="ModifyPassword" Name="修改会员密码" Value="7" User="1" Sort="0"></Purview>
<Purview Key="MoveToHistory" Name="移至历史档案" Value="7" User="1" Sort="1"></Purview>
<Purview Key="MemberImport" Name="数据导入" Value="7" User="1" Sort="2"></Purview>
</Purview>
<Purview Key="History" Name="历史档案" Value="7" User="1" Menu="1" Toolbar="1" Three="1" Sort="3">
<Purview Key="RestoreFromHistory" Name="从历史档案恢复" Value="7" User="1" Sort="0"></Purview>
</Purview>
<Purview Key="ReplaceCard" Name="会员换卡" Value="7" User="1" Menu="1" Sort="4"></Purview>
<Purview Key="Freeze" Name="会员卡挂失" Value="7" User="1" Menu="1" Sort="5"></Purview>
<Purview Key="UnFreeze" Name="会员卡解挂" Value="7" User="1" Menu="1" Sort="6"></Purview>
<Purview Key="UnDef01" Value="0">-</Purview>
<Purview Key="ReturnCash" Name="会员返现" Value="1" User="1" Menu="1" Toolbar="1" Sort="7"></Purview>
<Purview Key="Upgrade" Name="会员升级" Value="1" User="1" Menu="1" Toolbar="1" Sort="7"></Purview>
<Purview Key="UnDef07" Value="0"></Purview>
<Purview Key="UnDef08" Value="0"></Purview>
<Purview Key="UnDef09" Value="0"></Purview>
</Purview>
</PurviewDefind>
<?xml version="1.0"?>
<!-- Kye 标识
Name 名称
Value 值,可对应不同的权限组
Menu 是否创建菜单
User 是否用户控制
Three 是否三态控制(添加,修改,删除)
-->
<PurviewDefind>
<Purview Key="Member" Name="会员" ShortCut="M" Value="7" User="1" Menu="1" Sort="2">
<Purview Key="MakeCard" Name="会员办卡" Value="7" User="1" Menu="1" Toolbar="1" Sort="1"></Purview>
<Purview Key="Archives" Name="会员档案" Value="7" User="1" Menu="1" Toolbar="1" Three="1" Sort="2">
<Purview Key="ModifyPassword" Name="修改会员密码" Value="7" User="1" Sort="0"></Purview>
<Purview Key="MoveToHistory" Name="移至历史档案" Value="7" User="1" Sort="1"></Purview>
<Purview Key="MemberImport" Name="数据导入" Value="7" User="1" Sort="2"></Purview>
</Purview>
<Purview Key="History" Name="历史档案" Value="7" User="1" Menu="1" Toolbar="1" Three="1" Sort="3">
<Purview Key="RestoreFromHistory" Name="从历史档案恢复" Value="7" User="1" Sort="0"></Purview>
</Purview>
<Purview Key="ReplaceCard" Name="会员换卡" Value="7" User="1" Menu="1" Sort="4"></Purview>
<Purview Key="Freeze" Name="会员卡挂失" Value="7" User="1" Menu="1" Sort="5"></Purview>
<Purview Key="UnFreeze" Name="会员卡解挂" Value="7" User="1" Menu="1" Sort="6"></Purview>
<Purview Key="UnDef01" Value="0">-</Purview>
<Purview Key="ReturnCash" Name="会员返现" Value="1" User="1" Menu="1" Toolbar="1" Sort="7"></Purview>
<Purview Key="Upgrade" Name="会员升级" Value="1" User="1" Menu="1" Toolbar="1" Sort="7"></Purview>
<Purview Key="UnDef07" Value="0"></Purview>
<Purview Key="UnDef08" Value="0"></Purview>
<Purview Key="UnDef09" Value="0"></Purview>
</Purview>
</PurviewDefind>
前台创建菜单及工具栏(ContextArgs.Instance.PurviewGroup为用户组别,为FlagsAttribute类型):
Code
/// <summary>
/// 创建菜单项
/// </summary>
/// <param name="pc"></param>
/// <param name="mitems"></param>
private void CreateMenuItem(PurviewCollection pc, ToolStripItemCollection mitems)
{
bool hasBar = false;
foreach (PurviewItemSingle item in pc)
{
if (item.Value == 0) continue;
//控制用户权限
if (!item.Allow && item.User) continue;
//非菜单
if (item.Menu)
{
//权限组
if ((item.Value & (int)ContextArgs.Instance.PurviewGroup) == 0) continue;
if (item.Name == "-") //分隔条
{
mitems.Add(new ToolStripSeparator());
}
else
{
ToolStripMenuItem menu = new ToolStripMenuItem();
if (string.IsNullOrEmpty(item.ShortCut))
menu.Text = item.Name;
else //快捷键
menu.Text = item.Name + "(&" + item.ShortCut + ")";
menu.Tag = item.Key;
mitems.Add(menu);
if (menu.Enabled)
CreateMenuItem(item.Items, menu.DropDownItems);
if (menu.DropDownItems.Count == 0)
menu.Click += new EventHandler(menu_Click);
}
}
if (item.Toolbar)
{
//权限组
if ((item.Value & (int)ContextArgs.Instance.PurviewGroup) == 0) continue;
CreateToolbar(item);
hasBar = true;
}
}
if (hasBar)
{
ToolStripSeparator tlsSep = new ToolStripSeparator();
tspMain.Items.Add(tlsSep);
}
}
/// <summary>
/// 创建工具栏按钮
/// </summary>
/// <param name="item"></param>
private void CreateToolbar(PurviewItemSingle item)
{
ToolStripButton tlsBtn = new ToolStripButton();
tlsBtn.ToolTipText = tlsBtn.Text = item.Name;
string icon = Path.Combine(Application.StartupPath, "Images\\" + item.Key + ".ico");
if (File.Exists(icon))
{
tlsBtn.Image = new Icon(icon).ToBitmap();
}
tlsBtn.Tag = item.Key;
tlsBtn.TextImageRelation = TextImageRelation.ImageAboveText;
tspMain.Items.Add(tlsBtn);
//控制用户权限
if (!item.Allow && item.User) tlsBtn.Enabled = false;
else tlsBtn.Click += new EventHandler(menu_Click);
}
/// <summary>
/// 菜单工具栏事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menu_Click(object sender, EventArgs e)
{
FunctionHandler.Goto(dckMgrMain, ((ToolStripItem)sender).Tag.ToString());
}
/// <summary>
/// 创建菜单项
/// </summary>
/// <param name="pc"></param>
/// <param name="mitems"></param>
private void CreateMenuItem(PurviewCollection pc, ToolStripItemCollection mitems)
{
bool hasBar = false;
foreach (PurviewItemSingle item in pc)
{
if (item.Value == 0) continue;
//控制用户权限
if (!item.Allow && item.User) continue;
//非菜单
if (item.Menu)
{
//权限组
if ((item.Value & (int)ContextArgs.Instance.PurviewGroup) == 0) continue;
if (item.Name == "-") //分隔条
{
mitems.Add(new ToolStripSeparator());
}
else
{
ToolStripMenuItem menu = new ToolStripMenuItem();
if (string.IsNullOrEmpty(item.ShortCut))
menu.Text = item.Name;
else //快捷键
menu.Text = item.Name + "(&" + item.ShortCut + ")";
menu.Tag = item.Key;
mitems.Add(menu);
if (menu.Enabled)
CreateMenuItem(item.Items, menu.DropDownItems);
if (menu.DropDownItems.Count == 0)
menu.Click += new EventHandler(menu_Click);
}
}
if (item.Toolbar)
{
//权限组
if ((item.Value & (int)ContextArgs.Instance.PurviewGroup) == 0) continue;
CreateToolbar(item);
hasBar = true;
}
}
if (hasBar)
{
ToolStripSeparator tlsSep = new ToolStripSeparator();
tspMain.Items.Add(tlsSep);
}
}
/// <summary>
/// 创建工具栏按钮
/// </summary>
/// <param name="item"></param>
private void CreateToolbar(PurviewItemSingle item)
{
ToolStripButton tlsBtn = new ToolStripButton();
tlsBtn.ToolTipText = tlsBtn.Text = item.Name;
string icon = Path.Combine(Application.StartupPath, "Images\\" + item.Key + ".ico");
if (File.Exists(icon))
{
tlsBtn.Image = new Icon(icon).ToBitmap();
}
tlsBtn.Tag = item.Key;
tlsBtn.TextImageRelation = TextImageRelation.ImageAboveText;
tspMain.Items.Add(tlsBtn);
//控制用户权限
if (!item.Allow && item.User) tlsBtn.Enabled = false;
else tlsBtn.Click += new EventHandler(menu_Click);
}
/// <summary>
/// 菜单工具栏事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menu_Click(object sender, EventArgs e)
{
FunctionHandler.Goto(dckMgrMain, ((ToolStripItem)sender).Tag.ToString());
}
用户权限编辑控件: