MainForm.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using HslCommunication.LogNet;
  2. using ProjectManagementSystem.Common.Log;
  3. using ProjectManagementSystem.Common.Core;
  4. using ProjectManagementSystem.Enterance;
  5. using System.Windows.Forms;
  6. using ProjectManagementSystem.Device.CasunBox;
  7. using System;
  8. using System.Threading;
  9. using System.Linq;
  10. using System.Data;
  11. using System.Configuration;
  12. using ProjectManagementSystem.UI;
  13. using System.Collections.Generic;
  14. using ProjectManagementSystem.Common.Function;
  15. using System.Diagnostics;
  16. using AutoUpdaterDotNET;
  17. namespace ProjectManagementSystem
  18. {
  19. public partial class MainForm : Form
  20. {
  21. private UI.LogUI logUI1;
  22. private List<IDisplayUI> m_idisplayUIList = new List<IDisplayUI>();
  23. public MainForm()
  24. {
  25. InitializeComponent();
  26. ShowInTaskbar = false;
  27. this.Width = 1000;
  28. this.Height = 618;
  29. this.logUI1 = new UI.LogUI();
  30. this.logUI1.Dock = DockStyle.Fill;
  31. this.tabPageLog.Controls.Add(this.logUI1);
  32. }
  33. private void MainForm_Shown(object sender, System.EventArgs e)
  34. {
  35. AutoUpdater.Synchronous = true;
  36. AutoUpdater.ShowSkipButton = false;
  37. AutoUpdater.ShowRemindLaterButton = false;
  38. AutoUpdater.UpdateFormSize = new System.Drawing.Size(500, 300);
  39. AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;
  40. AutoUpdater.Start(ConfigurationManager.AppSettings["AutoUpdateUrl"].ToString());
  41. CLog.Instance.OnLog += Instance_OnLog;
  42. var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
  43. var dateTime = System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString("yyyyMMddHHmmss");
  44. this.Text = $"PMS{version} ({dateTime})";
  45. string link = ConfigurationManager.AppSettings["CasunAgvUrl"].ToString();
  46. this.linkLabel1.Links.Add(0, 2, link);
  47. this.linkLabel1.Links.Add(3, 7, $@"{link}/#/taskConfig/taskQuery/currentTask");
  48. CLog.Instance.GetLog("System").WriteInfo($"开始启动,版本:{this.Text}");
  49. InitTablePage();
  50. SystemEnterance systemEnterance = new SystemEnterance();
  51. CLog.Instance.GetLog("System").WriteInfo($"启动完毕");
  52. System.Threading.Tasks.Task.Factory.StartNew(DisplayThread);
  53. }
  54. private void AutoUpdater_ApplicationExitEvent()
  55. {
  56. Text = @"Closing application...";
  57. ShutDown();
  58. }
  59. private void InitTablePage()
  60. {
  61. try
  62. {
  63. string tabPages = ConfigurationManager.AppSettings["TabPage"].ToString();
  64. if (string.IsNullOrEmpty(tabPages))
  65. {
  66. return;
  67. }
  68. string[] tabPageArray = tabPages.Split(new char[] { '|' });
  69. for (int i = 0; i < tabPageArray.Length; i++)
  70. {
  71. Application.DoEvents();
  72. string pagInfoStr = tabPageArray[i];
  73. string[] pageInfos = pagInfoStr.Split(new char[] { ':' });
  74. string page = pageInfos[0];
  75. TabPage tabPage = new TabPage();
  76. tabPage.Name = "tabPage" + (i + 1);
  77. tabPage.TabIndex = i + 10;
  78. tabPage.Text = pageInfos[1];
  79. tabPage.UseVisualStyleBackColor = true;
  80. Control controlUI = GetControlUI(page);
  81. if (controlUI == null)
  82. {
  83. MessageBox.Show($"“{pagInfoStr}” 获取界面失败,程序退出!!!");
  84. ShutDown();
  85. return;
  86. }
  87. controlUI.Dock = DockStyle.Fill;
  88. IDisplayUI displayUI = controlUI as IDisplayUI;
  89. if (displayUI == null)
  90. {
  91. MessageBox.Show($"“{pagInfoStr}” 界面不满足接口:IDisplayUI,程序退出!!!");
  92. ShutDown();
  93. return;
  94. }
  95. tabPage.Controls.Add(controlUI);
  96. this.tabControl1.Controls.Add(tabPage);
  97. if (!displayUI.Init())
  98. {
  99. MessageBox.Show($"“{pagInfoStr}” 界面初始化失败,程序退出!!!");
  100. ShutDown();
  101. return;
  102. }
  103. m_idisplayUIList.Add(displayUI);
  104. CLog.Instance.GetLog("System").WriteDebug($"已加载界面:“{tabPage.Text}”");
  105. Application.DoEvents();
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. CLog.Instance.GetLog("System").WriteException("Exception", ex);
  111. string logContext = $"启动失败:{ex.Message}";
  112. System.Windows.Forms.MessageBox.Show(logContext);
  113. ShutDown();
  114. }
  115. }
  116. private Control GetControlUI(string page)
  117. {
  118. var name = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
  119. string fullClassName = $"{name}.UI.{page}";
  120. return InstanceConstructor.GetInstance<Control>(name, fullClassName);
  121. }
  122. public void DisplayThread()
  123. {
  124. byte couter = 0;
  125. while (true)
  126. {
  127. try
  128. {
  129. for (int i = 0; i < m_idisplayUIList.Count; i++)
  130. {
  131. m_idisplayUIList[i].UpdateDisplay();
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. CLog.Instance.GetLog("System").WriteException("Exception", ex);
  137. }
  138. couter++;
  139. Thread.Sleep(1000);
  140. }
  141. }
  142. private void Instance_OnLog(int degree, string obj)
  143. {
  144. if (degree <= logUI1.DisplayDegree)
  145. {
  146. logUI1.AddLog(obj);
  147. }
  148. }
  149. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  150. {
  151. e.Cancel = true;
  152. this.Invoke(new MethodInvoker(() =>
  153. {
  154. WindowState = FormWindowState.Minimized;
  155. ShowInTaskbar = false;
  156. }));
  157. }
  158. private void 关闭系统ToolStripMenuItem_Click(object sender, System.EventArgs e)
  159. {
  160. var dialogResult = MessageBox.Show("确定要关闭系统", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  161. if (dialogResult == DialogResult.OK)
  162. {
  163. CLog.Instance.GetLog("System").WriteInfo($"关闭系统");
  164. ShutDown();
  165. }
  166. }
  167. private void ShutDown()
  168. {
  169. Close();
  170. Dispose();
  171. Application.Exit();
  172. Process[] process = Process.GetProcesses();
  173. Process currentProcess = Process.GetCurrentProcess();
  174. for (int i = 0; i < process.Count(); i++)
  175. {
  176. if (process[i].ProcessName == currentProcess.ProcessName)
  177. {
  178. process[i].Kill();
  179. }
  180. }
  181. }
  182. private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
  183. {
  184. this.Visible = true;
  185. this.WindowState = FormWindowState.Normal;
  186. this.TopMost = true;
  187. this.TopMost = false;
  188. }
  189. private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  190. {
  191. //linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
  192. string targetUrl = e.Link.LinkData as string;
  193. if (string.IsNullOrEmpty(targetUrl))
  194. MessageBox.Show("没有链接地址!");
  195. else
  196. System.Diagnostics.Process.Start(targetUrl);
  197. }
  198. }
  199. }