FrmMain.cs 12 KB

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