AccordionMenu.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Reflection;
  6. using System.Data;
  7. using System.Text;
  8. using UAS_MES_NEW.DataOperate;
  9. using UAS_MES_NEW.Entity;
  10. using System.Collections.Generic;
  11. namespace UAS_MES_NEW.CustomControl.AccordionMenu
  12. {
  13. public partial class AccordionMenu : UserControl
  14. {
  15. #region 变量
  16. /// <summary>
  17. /// 记录当前功能面板中的按钮
  18. /// </summary>
  19. private ArrayList ArrFunButton = new ArrayList();
  20. /// <summary>
  21. /// 记录当前功能面板中的listview
  22. /// </summary>
  23. private ArrayList ArrFunListView = new ArrayList();
  24. /// <summary>
  25. /// 已经打开的Form
  26. /// </summary>
  27. public static Dictionary<string, int> OpenedFormName = new Dictionary<string, int>();
  28. /// <summary>
  29. /// 功能面板的宽度
  30. /// </summary>
  31. private int m_nPanFunWidth = 160;
  32. //功能面板隐藏后的宽度
  33. private int m_nPanFunHideWidth = 0;
  34. //Tabpage的索引
  35. public static int WinIndex = 0;
  36. /// <summary>
  37. /// Button和ListView的个数,因为两者数量必相等,所以用一个变量代替
  38. /// </summary>
  39. private string[] Module;
  40. #endregion
  41. private DataHelper dh;
  42. private StringBuilder sql = new StringBuilder();
  43. public AccordionMenu()
  44. {
  45. InitializeComponent();
  46. }
  47. private void AccordionMenu_Load(object sender, EventArgs e)
  48. {
  49. //设置功能面板的位置和宽带
  50. this.Width = m_nPanFunWidth;
  51. //查询该用户所在的组别有权限查看的模块
  52. dh = SystemInf.dh;
  53. if (User.UserAccountType == "admin")
  54. {
  55. sql.Clear();
  56. 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,");
  57. 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");
  58. }
  59. else
  60. {
  61. sql.Clear();
  62. sql.Append("select sn_detno,sn_module,sn_modulecode,ugp_caller,max(ugp_ifdelete)ugp_ifdelete,max(ugp_ifread)ugp_ifread");
  63. sql.Append(",max(ugp_ifspecial)ugp_ifspecial,max(ugp_ifwrite)ugp_ifwrite,max(ugp_ifall)ugp_ifall from CS$USERGROUPPOWER ");
  64. 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 ");
  65. 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");
  66. }
  67. DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  68. //查询到没有结果直接返回,避免后面处理空值
  69. if (dt.Rows.Count == 0 && User.UserAccountType != "admin")
  70. {
  71. return;
  72. }
  73. //设置ListView的样式
  74. foreach (Control c in this.Controls)
  75. {
  76. //设置ListView的格式和记录控件的个数
  77. if (c is ListView)
  78. {
  79. ListView ls = c as ListView;
  80. ls.View = View.SmallIcon;
  81. ls.SmallImageList = new ImageList();
  82. ls.SmallImageList.ImageSize = new Size(10, 30);
  83. ls.Height = 200;
  84. }
  85. }
  86. //用于存储模块的数组
  87. Module = new string[dt.Rows.Count];
  88. //用于拼接模块名称的字符串 例如(a,b,c)
  89. string Modules = "";
  90. //查询出的用户可见的Caller
  91. string[] Caller_List = new string[dt.Rows.Count];
  92. string Callers = "";
  93. //将查询出的数据添加到队列中
  94. for (int i = 0; i < dt.Rows.Count; i++)
  95. {
  96. //在这个Caller范围内的是用户可见的导航
  97. if (dt.Rows[i]["UGP_IFREAD"].ToString() == "1" || dt.Rows[i]["UGP_IFALL"].ToString() == "1")
  98. {
  99. Callers += "'" + dt.Rows[i]["ugp_caller"].ToString() + "',";
  100. }
  101. //用一个数组记录所有的模块
  102. Module[i] = dt.Rows[i]["sn_module"].ToString();
  103. if (!Modules.Contains(dt.Rows[i]["sn_module"].ToString()))
  104. Modules += "'" + dt.Rows[i]["sn_module"].ToString() + "',";
  105. this.Controls[dt.Rows[i]["sn_module"].ToString()].Visible = true;
  106. this.Controls[dt.Rows[i]["sn_module"].ToString() + "_lsv"].Visible = true;
  107. //添加权限
  108. Dictionary<string, bool> dic = new Dictionary<string, bool>();
  109. dic.Add("IFDELETE", dt.Rows[i]["UGP_IFDELETE"].ToString() != "1" ? false : true);
  110. dic.Add("IFREAD", dt.Rows[i]["UGP_IFREAD"].ToString() != "1" ? false : true);
  111. dic.Add("IFWRITE", dt.Rows[i]["UGP_IFWRITE"].ToString() != "1" ? false : true);
  112. dic.Add("IFSPECIAL", dt.Rows[i]["UGP_IFSPECIAL"].ToString() != "1" ? false : true);
  113. dic.Add("IFALL", dt.Rows[i]["UGP_IFALL"].ToString() != "1" ? false : true);
  114. //之后通过Call来获取页面操作的权限
  115. if (SystemInf.Caller.ContainsKey(dt.Rows[i]["ugp_caller"].ToString()))
  116. {
  117. SystemInf.Caller.Remove(dt.Rows[i]["ugp_caller"].ToString());
  118. }
  119. SystemInf.Caller.Add(dt.Rows[i]["ugp_caller"].ToString(), dic);
  120. //重复项目会导致导航栏出现展示空缺,如果包含有该项则不进行添加
  121. //添加导航的按钮和对应的ListView
  122. if (!ArrFunButton.Contains(this.Controls[dt.Rows[i]["sn_module"].ToString()]))
  123. {
  124. ArrFunButton.Add(this.Controls[dt.Rows[i]["sn_module"].ToString()]);
  125. ArrFunListView.Add(this.Controls[dt.Rows[i]["sn_module"].ToString() + "_lsv"]);
  126. }
  127. }
  128. if (Callers != "")
  129. {
  130. Callers = Callers.Substring(0, Callers.Length - 1);
  131. }
  132. else
  133. {
  134. Callers = "''";
  135. }
  136. //根据模块的名称查询出所有的子模块
  137. sql.Clear();
  138. 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");
  139. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  140. sql.Clear();
  141. //模块的数量,需要单独控制这个变量的增加
  142. int Module_count = 0;
  143. for (int i = 0; i < dt.Rows.Count; i++)
  144. {
  145. //如果查询的子栏目的所属模块和按钮之前的模块相等,并且启用了导航
  146. if (Module[Module_count] == dt.Rows[i]["sn_module"].ToString())
  147. {
  148. ListView lsv = this.Controls[Module[Module_count] + "_lsv"] as ListView;
  149. ListViewItem lsvi = new ListViewItem();
  150. lsvi.Tag = dt.Rows[i]["sn_classname"].ToString();
  151. lsvi.Text = dt.Rows[i]["sn_displayname"].ToString();
  152. lsvi.ToolTipText = dt.Rows[i]["sn_caller"].ToString();
  153. lsvi.Name = dt.Rows[i]["sn_id"].ToString();
  154. lsv.Items.Add(lsvi);
  155. }
  156. else
  157. {
  158. //条件不相等的i+1用来做判断了,所以需要-1
  159. i = i - 1;
  160. Module_count++;
  161. }
  162. }
  163. int nCount = ArrFunButton.Count;
  164. //布置各功能按钮的位置和ListView的属性
  165. for (int i = nCount - 1; i >= 0; i--)
  166. {
  167. Button btn = ArrFunButton[i] as Button;
  168. btn.Width = this.Width - 4;
  169. btn.Left = 0;
  170. //将按钮的单击事件和具体代码对应起来
  171. btn.Click += new EventHandler(Btn_Click);
  172. if (i == 0)
  173. {
  174. btn.Top = 0;
  175. btn.Anchor = AnchorStyles.Left | AnchorStyles.Top;
  176. }
  177. else
  178. {
  179. if (i == nCount - 1)
  180. btn.Top = this.Height - btn.Height - 4;
  181. else
  182. btn.Top = (ArrFunButton[i + 1] as Button).Top - btn.Height;
  183. btn.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
  184. }
  185. //鼠标在按钮上移动时,同样判断当前工具条是否隐藏
  186. btn.MouseMove += new MouseEventHandler(FunListView_MouseMove);
  187. //设置listview的anchor属性
  188. ListView lsv = ArrFunListView[i] as ListView;
  189. if (lsv != null)
  190. {
  191. lsv.Anchor = AnchorStyles.Left | AnchorStyles.Top |
  192. AnchorStyles.Right | AnchorStyles.Bottom;
  193. //隐藏功能listview
  194. lsv.Visible = false;
  195. //设置listview双击事件
  196. lsv.Click += lsvFun_DoubleClick;
  197. lsv.DoubleClick += new EventHandler(lsvFun_DoubleClick);
  198. lsv.MouseMove += new MouseEventHandler(FunListView_MouseMove);
  199. }
  200. }
  201. //将第一个功能按钮点一下
  202. (ArrFunButton[0] as Button).PerformClick();
  203. //别忘了打开记时器
  204. this.timer1.Enabled = true;
  205. }
  206. public void Btn_Click(object sender, EventArgs e)
  207. {
  208. Button btnNow = sender as Button;
  209. if (btnNow == null)
  210. return;
  211. int nIndex = this.ArrFunButton.IndexOf(btnNow);
  212. //将该按钮前面的置顶
  213. for (int i = 1; i <= nIndex; i++)
  214. {
  215. Button btn = ArrFunButton[i] as Button;
  216. btn.Top = ((Button)ArrFunButton[i - 1]).Bottom;
  217. btn.Anchor = AnchorStyles.Left | AnchorStyles.Top;
  218. }
  219. //将下面的按钮下移
  220. for (int i = ArrFunButton.Count - 1; i > nIndex; i--)
  221. {
  222. Button btn = ArrFunButton[i] as Button;
  223. if (i == ArrFunButton.Count - 1)//最后一个
  224. btn.Top = this.Height - btn.Height;
  225. else
  226. btn.Top = ((Button)ArrFunButton[i + 1]).Top - btn.Height;
  227. btn.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
  228. }
  229. //显示对应的listview
  230. for (int i = 0; i < ArrFunButton.Count; i++)
  231. {
  232. ListView lsv = ArrFunListView[i] as ListView;
  233. //当前按钮对应的ListView
  234. if (i == nIndex)
  235. {
  236. lsv.Left = 0;
  237. lsv.Width = btnNow.Width;
  238. lsv.Top = btnNow.Bottom;
  239. for (int j = 0; j < lsv.Items.Count; j++)
  240. {
  241. lsv.Items[j].ImageIndex = 0;
  242. }
  243. if (nIndex == ArrFunListView.Count - 1)//最后一个
  244. lsv.Height = this.Height - btnNow.Bottom - 4;
  245. else
  246. lsv.Height = (ArrFunButton[i + 1] as Button).Top - btnNow.Bottom;
  247. //将当前ListView显示出来
  248. if (!lsv.Visible)
  249. lsv.Visible = true;
  250. }
  251. else //隐藏其他listview
  252. {
  253. if (lsv.Visible)
  254. lsv.Visible = false;
  255. }
  256. }
  257. }
  258. //隐藏后鼠标在本工具条上移动时,将它显示出来
  259. private void FunListView_MouseMove(object sender, MouseEventArgs e)
  260. {
  261. if (this.Width == m_nPanFunHideWidth)
  262. {
  263. this.Width = m_nPanFunWidth;
  264. this.timer1.Enabled = true;
  265. }
  266. }
  267. //双击listview后根据当前项执行操作
  268. private void lsvFun_DoubleClick(object sender, EventArgs e)
  269. {
  270. //双击后执行一个功能
  271. ListView lsv = sender as ListView;
  272. if (lsv == null)
  273. return;
  274. if (lsv.SelectedItems.Count == 0)
  275. return;
  276. //ListView点击的数据
  277. ListViewItem item = lsv.SelectedItems[0];
  278. //利用反射根据类的名称创建实例
  279. Form OpenForm = (Form)Assembly.GetExecutingAssembly().CreateInstance("UAS_MES." + item.Tag);
  280. OpenForm.Text = item.Text;
  281. //用Tag保存Caller
  282. OpenForm.Tag = item.ToolTipText;
  283. TabControl tc = (TabControl)this.Parent.Parent.Controls["MainTabControl"];
  284. //如果窗体不存在进行打开,item.ToolTip是Caller
  285. if (!OpenedFormName.ContainsKey(item.ToolTipText))
  286. {
  287. TabPage tb = new TabPage { Name = item.Name, Tag = item.ToolTipText, Text = OpenForm.Text };
  288. OpenForm.TopLevel = false;
  289. tc.TabPages.Add(tb);
  290. //直接选中新打开的界面
  291. tc.SelectedTab = tb;
  292. tb.Controls.Add(OpenForm);
  293. OpenedFormName.Add(item.ToolTipText, WinIndex);
  294. tc.TabIndex = WinIndex;
  295. WinIndex++;
  296. OpenForm.Show();
  297. }
  298. //如果窗体已存在展示该窗体
  299. else
  300. tc.SelectTab(item.Name);
  301. }
  302. }
  303. }