FrmMain.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using DevComponents.DotNetBar;
  2. using FreeRedis;
  3. using MessagePack;
  4. using MessagePack.Resolvers;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Runtime.InteropServices;
  8. using System.Windows.Forms;
  9. using WCS_Client.Frm;
  10. using WCS_Client.UC;
  11. using WCS_Client.Utility;
  12. namespace WCS_Client
  13. {
  14. public partial class FrmMain : Form
  15. {
  16. public FrmMain()
  17. {
  18. InitializeComponent();
  19. InitFrm();
  20. }
  21. private void InitFrm()
  22. {
  23. #region 初始化任务栏
  24. string sX = "";
  25. try
  26. {
  27. DateTime D = CurrentHelper.User.Use_LoginTime;
  28. switch (D.DayOfWeek)
  29. {
  30. case DayOfWeek.Sunday:
  31. sX = "日";
  32. break;
  33. case DayOfWeek.Monday:
  34. sX = "一";
  35. break;
  36. case DayOfWeek.Tuesday:
  37. sX = "二";
  38. break;
  39. case DayOfWeek.Wednesday:
  40. sX = "三";
  41. break;
  42. case DayOfWeek.Thursday:
  43. sX = "四";
  44. break;
  45. case DayOfWeek.Friday:
  46. sX = "五";
  47. break;
  48. case DayOfWeek.Saturday:
  49. sX = "六";
  50. break;
  51. }
  52. }
  53. catch
  54. {
  55. }
  56. Btn_LoginUserTime.Text = string.Format("【{0}】【{1}】【{2}】【星期{3}】", CurrentHelper.User.Use_Name, CurrentHelper.User.Use_RoleName, CurrentHelper.User.Use_LoginTime, sX);
  57. Btn_Copyrignt.Text = "苏州博众精工科技有限公司版权所有";
  58. #endregion 初始化任务栏
  59. RefreshMenu();
  60. SetMdiForm("系统主页", "系统主页", typeof(FrmHome), false);
  61. if (eqpData.Redis == null)
  62. {
  63. RedisClient Redis = new RedisClient("192.168.249.150,password=123456,database=10");
  64. //new RedisClient("212.64.66.35,database=10");
  65. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  66. Redis.Serialize = obj =>
  67. {
  68. var bytes = MessagePackSerializer.Serialize(obj);
  69. return bytes;
  70. };
  71. Redis.DeserializeRaw = (bytes, type) =>
  72. {
  73. var obj = MessagePackSerializer.Deserialize(type, bytes);
  74. return obj;
  75. };
  76. eqpData.init(Redis);
  77. }
  78. }
  79. private void RefreshMenu()
  80. {
  81. try
  82. {
  83. Bar_Menu.Items.Clear();
  84. AddMenu("", "MBtn_File", "文件", null, null);
  85. if (CurrentHelper.User.Use_RoleId == 1)
  86. {
  87. AddMenu("", "MBtn_User", "用户管理", null, null);
  88. }
  89. AddMenu("", "MBtn_Task", "任务管理", null, null);
  90. AddMenu("", "MBtn_Monitor", "设备监控", null, null);
  91. AddMenu("", "MBtn_Sys", "系统管理", null, null);
  92. AddMenu("", "MBtn_Help", "系统帮助", null, null);
  93. //MBtn_File
  94. AddMenu("MBtn_File", "MBtn_File_Calc", "计算器", null, Btn_Calc_Click);
  95. AddMenu("MBtn_File", "MBtn_File_Book", "记事本", null, Btn_Book_Click);
  96. AddMenu("MBtn_File", "MBtn_File_SysReset", "系统注销", null, Btn_SysReset_Click);
  97. AddMenu("MBtn_File", "MBtn_File_SysExit", "系统退出", null, Btn_SysExit_Click);
  98. //MBtn_User
  99. if (CurrentHelper.User.Use_RoleId == 1)
  100. {
  101. AddMenu("MBtn_User", "MBtn_User_User", "用户管理", typeof(Frm.FrmUser), Btn_Click);
  102. }
  103. //MBtn_Task
  104. AddMenu("MBtn_Task", "MBtn_CurrentTask", "当前任务管理", typeof(Frm.FrmWCS_Task), Btn_Click);
  105. AddMenu("MBtn_Task", "MBtn_DisTask", "历史任务查询", typeof(Frm.FrmWCS_TaskDIS), Btn_Click);
  106. AddMenu("MBtn_Task", "MBtn_AgvTask", "Agv任务查询", typeof(Frm.MBtn_AgvTask), Btn_Click);
  107. AddMenu("MBtn_Task", "MBtn_AgvTask_Dis", "Agv历史任务查询", typeof(Frm.MBtn_AgvTaskDis.MBtn_AgvTask_Dis), Btn_Click);
  108. ////MBtn_Monitor
  109. AddMenu("MBtn_Monitor", "MBtn_SCANN", "扫码信息查询", typeof(Frm.MBtn_SCANN), Btn_Click);
  110. AddMenu("MBtn_Monitor", "MBtn_EQUMsg", "设备信号查询", typeof(Frm.FrmEquMsg), Btn_Click);
  111. AddMenu("MBtn_Monitor", "MBtn_ALARAMRE", "历史报警记录", typeof(Frm.Frm_ALARAMRE), Btn_Click);
  112. AddMenu("MBtn_Monitor", "MBtn_ALARAMERStatistics", "历史报警记录统计", typeof(Frm.Frm_ALARAMERStatistics), Btn_Click);
  113. AddMenu("MBtn_Monitor", "MBtn_Mont", "设备监控查看", typeof(Frm.Frm_YongGuanMontior), Btn_Click);
  114. //MBtn_Sys
  115. AddMenu("MBtn_Sys", "MBtn_Interface_OPOrder", "日志管理", typeof(Frm.FrmLog), Btn_Click);
  116. //AddMenu("MBtn_Sys", "MBtn_Interface_OPUP", "系统设置", typeof(Frm.MBtn_Sys.FormSet), Btn_Click);
  117. AddMenu("MBtn_Sys", "MBtn_Interface_SrmInfoSet", "系统设置", typeof(Frm.FrmSrmInfoSet), Btn_Click);
  118. AddMenu("MBtn_Sys", "MBtn_Interface_FrmAGVSet", "AGV设置(车间设置)", typeof(Frm.FrmAGVSet), Btn_Click);
  119. }
  120. catch (Exception ex)
  121. {
  122. MessageUtil.ShowError(ex.Message);
  123. this.Close();
  124. }
  125. }
  126. private List<ButtonItem> mbtn = new List<ButtonItem>();
  127. private void AddMenu(string PBtnName, string BtnName, string DisplayText, Type t, System.EventHandler BtnItem_Click)
  128. {
  129. ButtonItem BtnItem = mbtn.Find(a => a.Name == BtnName);
  130. if (BtnItem == null)
  131. {
  132. BtnItem = new ButtonItem();
  133. mbtn.Add(BtnItem);
  134. }
  135. BtnItem.Tag = t;
  136. BtnItem.Name = BtnName;
  137. BtnItem.Text = DisplayText;
  138. BtnItem.Visible = true;
  139. BtnItem.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
  140. if (BtnItem_Click != null)
  141. {
  142. BtnItem.Click += new EventHandler(BtnItem_Click);
  143. }
  144. ButtonItem PBtnItem = mbtn.Find(a => a.Name == PBtnName);
  145. if (PBtnItem == null)
  146. {
  147. Bar_Menu.Items.Add(BtnItem);
  148. }
  149. else
  150. {
  151. PBtnItem.SubItems.Add(BtnItem);
  152. }
  153. }
  154. private void Btn_Calc_Click(object sender, EventArgs e)
  155. {
  156. #region [ 启动计算器 ]
  157. System.Diagnostics.Process Proc;
  158. try
  159. {
  160. // 启动计算器
  161. Proc = new System.Diagnostics.Process();
  162. Proc.StartInfo.FileName = "calc.exe";
  163. Proc.StartInfo.UseShellExecute = false;
  164. Proc.StartInfo.RedirectStandardInput = true;
  165. Proc.StartInfo.RedirectStandardOutput = true;
  166. Proc.Start();
  167. }
  168. catch
  169. {
  170. Proc = null;
  171. }
  172. #endregion [ 启动计算器 ]
  173. }
  174. #region [ API: 记事本 ]
  175. /// <summary>
  176. /// 传递消息给记事本
  177. /// </summary>
  178. /// <param name="hWnd"></param>
  179. /// <param name="Msg"></param>
  180. /// <param name="wParam"></param>
  181. /// <param name="lParam"></param>
  182. /// <returns></returns>
  183. [DllImport("User32.DLL")]
  184. public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
  185. /// <summary>
  186. /// 查找句柄
  187. /// </summary>
  188. /// <param name="hwndParent"></param>
  189. /// <param name="hwndChildAfter"></param>
  190. /// <param name="lpszClass"></param>
  191. /// <param name="lpszWindow"></param>
  192. /// <returns></returns>
  193. [DllImport("User32.DLL")]
  194. public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  195. /// <summary>
  196. /// 记事本需要的常量
  197. /// </summary>
  198. public const uint WM_SETTEXT = 0x000C;
  199. #endregion [ API: 记事本 ]
  200. private void Btn_Book_Click(object sender, EventArgs e)
  201. {
  202. #region [ 启动记事本 ]
  203. System.Diagnostics.Process Proc;
  204. try
  205. {
  206. // 启动记事本
  207. Proc = new System.Diagnostics.Process();
  208. Proc.StartInfo.FileName = "notepad.exe";
  209. Proc.StartInfo.UseShellExecute = false;
  210. Proc.StartInfo.RedirectStandardInput = true;
  211. Proc.StartInfo.RedirectStandardOutput = true;
  212. Proc.Start();
  213. }
  214. catch
  215. {
  216. Proc = null;
  217. }
  218. #endregion [ 启动记事本 ]
  219. #region [ 传递数据给记事本 ]
  220. if (Proc != null)
  221. {
  222. // 调用 API, 传递数据
  223. while (Proc.MainWindowHandle == IntPtr.Zero)
  224. {
  225. Proc.Refresh();
  226. }
  227. IntPtr vHandle = FindWindowEx(Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);
  228. // 传递数据给记事本
  229. SendMessage(vHandle, WM_SETTEXT, 0, "");
  230. }
  231. #endregion [ 传递数据给记事本 ]
  232. }
  233. private void Btn_SysReset_Click(object sender, EventArgs e)
  234. {
  235. Current.PlcSet = new List<WCS_PLC>();
  236. Application.Restart();
  237. }
  238. private void Btn_SysExit_Click(object sender, EventArgs e)
  239. {
  240. Environment.Exit(0);
  241. Application.Exit();
  242. }
  243. private void Btn_Click(object sender, EventArgs e)
  244. {
  245. ButtonItem BtnItem = sender as ButtonItem;
  246. if (BtnItem == null)
  247. {
  248. return;
  249. }
  250. Type t = BtnItem.Tag as Type;
  251. if (t == null)
  252. {
  253. return;
  254. }
  255. SetMdiForm(BtnItem.Name, BtnItem.Text, t, true);
  256. }
  257. public void SetMdiForm(string TabName, string TabText, Type formType, bool CloseButtonVisible)
  258. {
  259. bool IsOpened = false;
  260. //遍历现有的Tab页面,如果存在,那么设置为选中即可
  261. foreach (SuperTabItem tabitem in STab_Main.Tabs)
  262. {
  263. if (tabitem.Name == TabName)
  264. {
  265. STab_Main.SelectedTab = tabitem;
  266. IsOpened = true;
  267. break;
  268. }
  269. }
  270. //如果在现有Tab页面中没有找到,那么就要初始化了Tab页面了
  271. if (!IsOpened)
  272. {
  273. //为了方便管理,调用LoadMdiForm函数来创建一个新的窗体,并作为MDI的子窗体
  274. //然后分配给SuperTab控件,创建一个SuperTabItem并显示
  275. Form form = Activator.CreateInstance(formType) as Form;
  276. form.FormBorderStyle = FormBorderStyle.None;
  277. form.TopLevel = false;
  278. form.Visible = true;
  279. SuperTabItem tabItem = STab_Main.CreateTab(TabName);
  280. tabItem.Name = TabName;
  281. tabItem.Text = TabText;
  282. tabItem.AttachedControl.Controls.Add(form);
  283. STab_Main.SelectedTab = tabItem;
  284. tabItem.CloseButtonVisible = CloseButtonVisible;
  285. form.Dock = DockStyle.Fill;
  286. }
  287. }
  288. }
  289. }