DataShowForm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using CCDCount.DLL;
  2. using LogClass;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace CCDCount.Forms
  12. {
  13. public partial class DataShowForm : Form
  14. {
  15. bool IsShow = false;
  16. #region 实例
  17. //MultiPanel中的展示面板
  18. List<Panel> CamerasPanel = null;
  19. //主线程实例队列
  20. public List<MainThreadClass> LsMainThread = null;
  21. #endregion
  22. #region 窗口事件
  23. public DataShowForm(List<MainThreadClass> lsMainThread)
  24. {
  25. InitializeComponent();
  26. LsMainThread = lsMainThread;
  27. this.WindowState = FormWindowState.Maximized;
  28. InitPanelUIFunction();
  29. InitShowDataKongJian();
  30. StartUpdataShowDataThread();
  31. }
  32. /// <summary>
  33. /// Panel大小改变事件
  34. /// </summary>
  35. /// <param name="sender"></param>
  36. /// <param name="e"></param>
  37. private void MultiDataPanel_Resize(object sender, EventArgs e)
  38. {
  39. if (CamerasPanel == null) return;
  40. for (int i = 0; i < 2; i++)
  41. {
  42. for (int j = 0; j < 2; j++)
  43. {
  44. CamerasPanel[i + j * 2].Size = new Size(MultiDataPanel.Width / 2, MultiDataPanel.Height / 2);
  45. CamerasPanel[i + j * 2].Location = new Point(MultiDataPanel.Width / 2 * i, MultiDataPanel.Height / 2 * j);
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// 窗口关闭
  51. /// </summary>
  52. /// <param name="sender"></param>
  53. /// <param name="e"></param>
  54. private void DataShowForm_FormClosing(object sender, FormClosingEventArgs e)
  55. {
  56. StopUpdataShowDataThread();
  57. this.Dispose();
  58. base.OnClosing(e);
  59. }
  60. #endregion
  61. #region 私有方法
  62. /// <summary>
  63. /// 初始化面板
  64. /// </summary>
  65. private void InitPanelUIFunction()
  66. {
  67. CamerasPanel = new List<Panel>();
  68. for (int i = 0; i < 2; i++)
  69. {
  70. for (int j = 0; j < 2; j++)
  71. {
  72. CamerasPanel.Add(new Panel()
  73. {
  74. Size = new Size(MultiDataPanel.Width / 2, MultiDataPanel.Height / 2),
  75. Location = new Point(MultiDataPanel.Width / 2 * i, MultiDataPanel.Height / 2 * j),
  76. BorderStyle = BorderStyle.FixedSingle
  77. });
  78. }
  79. }
  80. foreach (var item in CamerasPanel)
  81. {
  82. MultiDataPanel.Controls.Add(item);
  83. }
  84. }
  85. /// <summary>
  86. /// 初始化显示数据控件
  87. /// </summary>
  88. private void InitShowDataKongJian()
  89. {
  90. if (LsMainThread.Count > CamerasPanel.Count)
  91. return;
  92. for (int i = 0;i < LsMainThread.Count;i++)
  93. {
  94. Label label = new Label()
  95. {
  96. Text = LsMainThread[i].ThisCamerName == string.Empty ? LsMainThread[i].ThisCameraDevice: LsMainThread[i].ThisCamerName,
  97. Name = LsMainThread[i].ThisCameraSN + "CamNameLab",
  98. Font = new Font("微软雅黑", 12),
  99. Location = new Point(5, 5),
  100. ForeColor = Color.White,
  101. AutoSize = true
  102. };
  103. CamerasPanel[i].Controls.Add(label);
  104. Label labe2 = new Label()
  105. {
  106. Text = "总数:",
  107. Font = new Font("微软雅黑", 10),
  108. Location = new Point(5, 40),
  109. ForeColor = Color.White,
  110. AutoSize = true
  111. };
  112. CamerasPanel[i].Controls.Add(labe2);
  113. Label labe5 = new Label()
  114. {
  115. Text = "0",
  116. Name = LsMainThread[i].ThisCameraSN + "CamCountLab",
  117. Font = new Font("微软雅黑", 10),
  118. Location = new Point(100, 40),
  119. ForeColor = Color.White,
  120. AutoSize = true
  121. };
  122. CamerasPanel[i].Controls.Add(labe5);
  123. Label labe3 = new Label()
  124. {
  125. Text = "合格:",
  126. Font = new Font("微软雅黑", 10),
  127. Location = new Point(5, 80),
  128. ForeColor = Color.White,
  129. AutoSize = true
  130. };
  131. CamerasPanel[i].Controls.Add(labe3);
  132. Label labe7 = new Label()
  133. {
  134. Text = "0",
  135. Name = LsMainThread[i].ThisCameraSN + "CamOkLab",
  136. Font = new Font("微软雅黑", 10),
  137. Location = new Point(100, 80),
  138. ForeColor = Color.White,
  139. AutoSize = true
  140. };
  141. CamerasPanel[i].Controls.Add(labe7);
  142. Label labe4 = new Label()
  143. {
  144. Text = "不合格:",
  145. Font = new Font("微软雅黑", 10),
  146. Location = new Point(5, 120),
  147. ForeColor = Color.White,
  148. AutoSize = true
  149. };
  150. CamerasPanel[i].Controls.Add(labe4);
  151. Label labe6 = new Label()
  152. {
  153. Text = "0",
  154. Name = LsMainThread[i].ThisCameraSN + "CamNgLab",
  155. Font = new Font("微软雅黑", 10),
  156. Location = new Point(100, 120),
  157. ForeColor = Color.White,
  158. AutoSize = true
  159. };
  160. CamerasPanel[i].Controls.Add(labe6);
  161. Label label7 = new Label()
  162. {
  163. Text = LsMainThread[i].CameraRunStatic == true ? "运行中" : "未运行",
  164. Name = LsMainThread[i].ThisCameraSN + "CamRunLab",
  165. ForeColor = LsMainThread[i].CameraRunStatic == true ? Color.Green : Color.Red,
  166. Location = new Point(CamerasPanel[i].Size.Width - 120, 10),
  167. Font = new Font("微软雅黑", 10),
  168. AutoSize = true,
  169. Anchor = AnchorStyles.Right | AnchorStyles.Top,
  170. };
  171. CamerasPanel[i].Controls.Add(label7);
  172. }
  173. }
  174. /// <summary>
  175. /// 更新显示数据
  176. /// </summary>
  177. private void UpdataShowData()
  178. {
  179. int AllActiveNum = 0;
  180. int AllOkNum = 0;
  181. int AllNgNum = 0;
  182. foreach (var item in LsMainThread)
  183. {
  184. Label RunMessageLab = FindLabel(this, item.ThisCameraSN + "CamRunLab"); if (item.ShuLiState)
  185. {
  186. if (RunMessageLab != null)
  187. RunMessageLab.ForeColor = item.CameraRunStatic == true ? Color.Green : Color.Red;
  188. UpdateLabelText(this, item.ThisCameraSN + "CamRunLab", item.CameraRunStatic == true ? "运行中" : "未运行");
  189. }
  190. else
  191. {
  192. if (RunMessageLab != null)
  193. RunMessageLab.ForeColor = Color.Yellow;
  194. UpdateLabelText(this, item.ThisCameraSN + "CamRunLab", "视野遮挡");
  195. }
  196. UpdateLabelText(this, item.ThisCameraSN + "CamCountLab", item.HistoryActiveNum.ToString());
  197. UpdateLabelText(this, item.ThisCameraSN + "CamNgLab", item.NgHistoryNum.ToString());
  198. UpdateLabelText(this, item.ThisCameraSN + "CamOkLab", item.OkHistoryNum.ToString());
  199. AllActiveNum += item.HistoryActiveNum;
  200. AllOkNum += item.OkHistoryNum;
  201. AllNgNum += item.NgHistoryNum;
  202. }
  203. UpdateLabelText(this, "AllCamCountLab", AllActiveNum.ToString());
  204. UpdateLabelText(this, "AllCamNgLab", AllNgNum.ToString());
  205. UpdateLabelText(this, "AllCamOkLab", AllOkNum.ToString());
  206. }
  207. /// <summary>
  208. /// 启动更新显示数据线程
  209. /// </summary>
  210. private void StartUpdataShowDataThread()
  211. {
  212. IsShow = true;
  213. Task.Run(() =>
  214. {
  215. while (IsShow)
  216. {
  217. UpdataShowData();
  218. Thread.Sleep(50);
  219. }
  220. });
  221. }
  222. /// <summary>
  223. /// 停止更新显示数据线程
  224. /// </summary>
  225. private void StopUpdataShowDataThread()
  226. {
  227. IsShow = false;
  228. }
  229. /// <summary>
  230. /// 控件广度优先搜索
  231. /// </summary>
  232. /// <param name="root"></param>
  233. /// <param name="condition"></param>
  234. /// <returns></returns>
  235. private Control FindControlsBFS(Control root, Func<Control, bool> condition)
  236. {
  237. var results = new List<Control>();
  238. var queue = new Queue<Control>();
  239. queue.Enqueue(root);
  240. while (queue.Count > 0)
  241. {
  242. var current = queue.Dequeue();
  243. if (condition(current)) results.Add(current);
  244. foreach (Control child in current.Controls)
  245. {
  246. queue.Enqueue(child);
  247. }
  248. }
  249. if (results.Count == 0) return null;
  250. return results.First();
  251. }
  252. public static bool UpdateLabelText(Control container, string labelName, string newText)
  253. {
  254. // 遍历容器控件中的所有子控件
  255. foreach (Control control in container.Controls)
  256. {
  257. try
  258. {
  259. if (control != null &&!control.IsDisposed && control.IsHandleCreated)
  260. {
  261. // 匹配目标控件
  262. if (control.Name == labelName && control is Label targetLabel)
  263. {
  264. targetLabel.Invoke(new Action(() =>
  265. {
  266. targetLabel.Text = newText;
  267. }));
  268. return true; // 更新成功
  269. }
  270. // 递归查找子容器中的控件
  271. if (control.HasChildren)
  272. {
  273. bool found = UpdateLabelText(control, labelName, newText);
  274. if (found) return true;
  275. }
  276. }
  277. }
  278. catch(Exception ex)
  279. {
  280. LOG.log(ex.Message.ToString());
  281. }
  282. }
  283. return false; // 未找到目标控件
  284. }
  285. public Label FindLabel(Control container,string labelName)
  286. {
  287. Label label = null;
  288. // 遍历容器控件中的所有子控件
  289. foreach (Control control in container.Controls)
  290. {
  291. try
  292. {
  293. if (control != null && !control.IsDisposed && control.IsHandleCreated)
  294. {
  295. // 匹配目标控件
  296. if (control.Name == labelName && control is Label targetLabel)
  297. {
  298. label = targetLabel;
  299. return label;
  300. }
  301. // 递归查找子容器中的控件
  302. if (control.HasChildren)
  303. {
  304. label = FindLabel(control, labelName);
  305. if (label!=null) return label;
  306. }
  307. }
  308. }
  309. catch (Exception ex)
  310. {
  311. LOG.log(ex.Message.ToString());
  312. }
  313. }
  314. return label;
  315. }
  316. #endregion
  317. }
  318. }