FrmMain.cs 12 KB

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