MainForm.cs 9.7 KB

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