AccordionMenu.cs 15 KB

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