123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- 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 变量
- /// <summary>
- /// 记录当前功能面板中的按钮
- /// </summary>
- private ArrayList ArrFunButton = new ArrayList();
- /// <summary>
- /// 记录当前功能面板中的listview
- /// </summary>
- private ArrayList ArrFunListView = new ArrayList();
- /// <summary>
- /// 已经打开的Form
- /// </summary>
- public static Dictionary<string, int> OpenedFormName = new Dictionary<string, int>();
- /// <summary>
- /// 功能面板的宽度
- /// </summary>
- private int m_nPanFunWidth = 160;
- //功能面板隐藏后的宽度
- private int m_nPanFunHideWidth = 0;
- //Tabpage的索引
- public static int WinIndex = 0;
- /// <summary>
- /// Button和ListView的个数,因为两者数量必相等,所以用一个变量代替
- /// </summary>
- 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<string, bool> dic = new Dictionary<string, bool>();
- 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);
- }
- }
- }
|