using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
using System.Data;
using System.Text;
using UAS_MES_NEW.DataOperate;
using UAS_MES_NEW.Entity;
using System.Collections.Generic;
namespace UAS_MES_NEW.CustomControl.AccordionMenu
{
public partial class AccordionMenu : UserControl
{
#region 变量
///
/// 记录当前功能面板中的按钮
///
private ArrayList ArrFunButton = new ArrayList();
///
/// 记录当前功能面板中的listview
///
private ArrayList ArrFunListView = new ArrayList();
///
/// 已经打开的Form
///
public static Dictionary OpenedFormName = new Dictionary();
///
/// 功能面板的宽度
///
private int m_nPanFunWidth = 160;
//功能面板隐藏后的宽度
private int m_nPanFunHideWidth = 0;
//Tabpage的索引
public static int WinIndex = 0;
///
/// Button和ListView的个数,因为两者数量必相等,所以用一个变量代替
///
private string[] Module;
#endregion
private DataHelper dh;
private StringBuilder sql = new StringBuilder();
public AccordionMenu()
{
InitializeComponent();
}
private void AccordionMenu_Load(object sender, EventArgs e)
{
//设置功能面板的位置和宽带
this.Width = m_nPanFunWidth;
//查询该用户所在的组别有权限查看的模块
dh = SystemInf.dh;
if (User.UserAccountType == "admin")
{
sql.Clear();
sql.Append("select distinct sn_detno,sn_module,sn_modulecode,sn_caller ugp_caller,1 ugp_ifdelete,1 ugp_ifread,1 ugp_ifspecial,1 ugp_ifall,");
sql.Append("1 ugp_ifwrite from CS$SYSNAVATION left join CS$USERGROUPPOWER on sn_caller=ugp_caller where sn_using=1 order by sn_modulecode,sn_detno");
}
else
{
sql.Clear();
sql.Append("select sn_detno,sn_module,sn_modulecode,ugp_caller,max(ugp_ifdelete)ugp_ifdelete,max(ugp_ifread)ugp_ifread");
sql.Append(",max(ugp_ifspecial)ugp_ifspecial,max(ugp_ifwrite)ugp_ifwrite,max(ugp_ifall)ugp_ifall from CS$USERGROUPPOWER ");
sql.Append("left join CS$SYSNAVATION on sn_caller=ugp_caller where sn_using=1 and ugp_groupcode in ( select eg_groupcode from cs$empgroup ");
sql.Append("left join cs$userresource on ur_groupcode = eg_groupcode where eg_emcode = '" + User.UserCode + "') group by sn_detno,sn_module,sn_modulecode,ugp_caller order by sn_modulecode,sn_detno");
}
DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
//查询到没有结果直接返回,避免后面处理空值
if (dt.Rows.Count == 0 && User.UserAccountType != "admin")
{
return;
}
//设置ListView的样式
foreach (Control c in this.Controls)
{
//设置ListView的格式和记录控件的个数
if (c is ListView)
{
ListView ls = c as ListView;
ls.View = View.SmallIcon;
ls.SmallImageList = new ImageList();
ls.SmallImageList.ImageSize = new Size(10, 30);
ls.Height = 200;
}
}
//用于存储模块的数组
Module = new string[dt.Rows.Count];
//用于拼接模块名称的字符串 例如(a,b,c)
string Modules = "";
//查询出的用户可见的Caller
string[] Caller_List = new string[dt.Rows.Count];
string Callers = "";
//将查询出的数据添加到队列中
for (int i = 0; i < dt.Rows.Count; i++)
{
//在这个Caller范围内的是用户可见的导航
if (dt.Rows[i]["UGP_IFREAD"].ToString() == "1" || dt.Rows[i]["UGP_IFALL"].ToString() == "1")
{
Callers += "'" + dt.Rows[i]["ugp_caller"].ToString() + "',";
}
//用一个数组记录所有的模块
Module[i] = dt.Rows[i]["sn_module"].ToString();
if (!Modules.Contains(dt.Rows[i]["sn_module"].ToString()))
Modules += "'" + dt.Rows[i]["sn_module"].ToString() + "',";
this.Controls[dt.Rows[i]["sn_module"].ToString()].Visible = true;
this.Controls[dt.Rows[i]["sn_module"].ToString() + "_lsv"].Visible = true;
//添加权限
Dictionary dic = new Dictionary();
dic.Add("IFDELETE", dt.Rows[i]["UGP_IFDELETE"].ToString() != "1" ? false : true);
dic.Add("IFREAD", dt.Rows[i]["UGP_IFREAD"].ToString() != "1" ? false : true);
dic.Add("IFWRITE", dt.Rows[i]["UGP_IFWRITE"].ToString() != "1" ? false : true);
dic.Add("IFSPECIAL", dt.Rows[i]["UGP_IFSPECIAL"].ToString() != "1" ? false : true);
dic.Add("IFALL", dt.Rows[i]["UGP_IFALL"].ToString() != "1" ? false : true);
//之后通过Call来获取页面操作的权限
if (SystemInf.Caller.ContainsKey(dt.Rows[i]["ugp_caller"].ToString()))
{
SystemInf.Caller.Remove(dt.Rows[i]["ugp_caller"].ToString());
}
SystemInf.Caller.Add(dt.Rows[i]["ugp_caller"].ToString(), dic);
//重复项目会导致导航栏出现展示空缺,如果包含有该项则不进行添加
//添加导航的按钮和对应的ListView
if (!ArrFunButton.Contains(this.Controls[dt.Rows[i]["sn_module"].ToString()]))
{
ArrFunButton.Add(this.Controls[dt.Rows[i]["sn_module"].ToString()]);
ArrFunListView.Add(this.Controls[dt.Rows[i]["sn_module"].ToString() + "_lsv"]);
}
}
if (Callers != "")
{
Callers = Callers.Substring(0, Callers.Length - 1);
}
else
{
Callers = "''";
}
//根据模块的名称查询出所有的子模块
sql.Clear();
sql.Append("select sn_id,sn_displayname,sn_classname,sn_module,sn_modulecode,sn_using,sn_caller,sn_detno from CS$SYSNAVATION where sn_using=1 and sn_caller in(" + Callers + ") order by sn_modulecode,sn_detno");
dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
sql.Clear();
//模块的数量,需要单独控制这个变量的增加
int Module_count = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
//如果查询的子栏目的所属模块和按钮之前的模块相等,并且启用了导航
if (Module[Module_count] == dt.Rows[i]["sn_module"].ToString())
{
ListView lsv = this.Controls[Module[Module_count] + "_lsv"] as ListView;
ListViewItem lsvi = new ListViewItem();
lsvi.Tag = dt.Rows[i]["sn_classname"].ToString();
lsvi.Text = dt.Rows[i]["sn_displayname"].ToString();
lsvi.ToolTipText = dt.Rows[i]["sn_caller"].ToString();
lsvi.Name = dt.Rows[i]["sn_id"].ToString();
lsv.Items.Add(lsvi);
}
else
{
//条件不相等的i+1用来做判断了,所以需要-1
i = i - 1;
Module_count++;
}
}
int nCount = ArrFunButton.Count;
//布置各功能按钮的位置和ListView的属性
for (int i = nCount - 1; i >= 0; i--)
{
Button btn = ArrFunButton[i] as Button;
btn.Width = this.Width - 4;
btn.Left = 0;
//将按钮的单击事件和具体代码对应起来
btn.Click += new EventHandler(Btn_Click);
if (i == 0)
{
btn.Top = 0;
btn.Anchor = AnchorStyles.Left | AnchorStyles.Top;
}
else
{
if (i == nCount - 1)
btn.Top = this.Height - btn.Height - 4;
else
btn.Top = (ArrFunButton[i + 1] as Button).Top - btn.Height;
btn.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
}
//鼠标在按钮上移动时,同样判断当前工具条是否隐藏
btn.MouseMove += new MouseEventHandler(FunListView_MouseMove);
//设置listview的anchor属性
ListView lsv = ArrFunListView[i] as ListView;
if (lsv != null)
{
lsv.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
//隐藏功能listview
lsv.Visible = false;
//设置listview双击事件
lsv.Click += lsvFun_DoubleClick;
lsv.DoubleClick += new EventHandler(lsvFun_DoubleClick);
lsv.MouseMove += new MouseEventHandler(FunListView_MouseMove);
}
}
//将第一个功能按钮点一下
(ArrFunButton[0] as Button).PerformClick();
//别忘了打开记时器
this.timer1.Enabled = true;
}
public void Btn_Click(object sender, EventArgs e)
{
Button btnNow = sender as Button;
if (btnNow == null)
return;
int nIndex = this.ArrFunButton.IndexOf(btnNow);
//将该按钮前面的置顶
for (int i = 1; i <= nIndex; i++)
{
Button btn = ArrFunButton[i] as Button;
btn.Top = ((Button)ArrFunButton[i - 1]).Bottom;
btn.Anchor = AnchorStyles.Left | AnchorStyles.Top;
}
//将下面的按钮下移
for (int i = ArrFunButton.Count - 1; i > nIndex; i--)
{
Button btn = ArrFunButton[i] as Button;
if (i == ArrFunButton.Count - 1)//最后一个
btn.Top = this.Height - btn.Height;
else
btn.Top = ((Button)ArrFunButton[i + 1]).Top - btn.Height;
btn.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
}
//显示对应的listview
for (int i = 0; i < ArrFunButton.Count; i++)
{
ListView lsv = ArrFunListView[i] as ListView;
//当前按钮对应的ListView
if (i == nIndex)
{
lsv.Left = 0;
lsv.Width = btnNow.Width;
lsv.Top = btnNow.Bottom;
for (int j = 0; j < lsv.Items.Count; j++)
{
lsv.Items[j].ImageIndex = 0;
}
if (nIndex == ArrFunListView.Count - 1)//最后一个
lsv.Height = this.Height - btnNow.Bottom - 4;
else
lsv.Height = (ArrFunButton[i + 1] as Button).Top - btnNow.Bottom;
//将当前ListView显示出来
if (!lsv.Visible)
lsv.Visible = true;
}
else //隐藏其他listview
{
if (lsv.Visible)
lsv.Visible = false;
}
}
}
//隐藏后鼠标在本工具条上移动时,将它显示出来
private void FunListView_MouseMove(object sender, MouseEventArgs e)
{
if (this.Width == m_nPanFunHideWidth)
{
this.Width = m_nPanFunWidth;
this.timer1.Enabled = true;
}
}
//双击listview后根据当前项执行操作
private void lsvFun_DoubleClick(object sender, EventArgs e)
{
//双击后执行一个功能
ListView lsv = sender as ListView;
if (lsv == null)
return;
if (lsv.SelectedItems.Count == 0)
return;
//ListView点击的数据
ListViewItem item = lsv.SelectedItems[0];
//利用反射根据类的名称创建实例
Form OpenForm = (Form)Assembly.GetExecutingAssembly().CreateInstance("UAS_MES." + item.Tag);
OpenForm.Text = item.Text;
//用Tag保存Caller
OpenForm.Tag = item.ToolTipText;
TabControl tc = (TabControl)this.Parent.Parent.Controls["MainTabControl"];
//如果窗体不存在进行打开,item.ToolTip是Caller
if (!OpenedFormName.ContainsKey(item.ToolTipText))
{
TabPage tb = new TabPage { Name = item.Name, Tag = item.ToolTipText, Text = OpenForm.Text };
OpenForm.TopLevel = false;
tc.TabPages.Add(tb);
//直接选中新打开的界面
tc.SelectedTab = tb;
tb.Controls.Add(OpenForm);
OpenedFormName.Add(item.ToolTipText, WinIndex);
tc.TabIndex = WinIndex;
WinIndex++;
OpenForm.Show();
}
//如果窗体已存在展示该窗体
else
tc.SelectTab(item.Name);
}
}
}