MainForm.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using CCDCount.DLL;
  2. using CCDCount.DLL.Tools;
  3. using CCDCount.MODEL.ConfigModel;
  4. using CCDCount.MODEL.ShuLiClass;
  5. using LogClass;
  6. using MvCameraControl;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Windows.Forms;
  14. namespace CCDCount.Forms
  15. {
  16. public partial class MainForm : Form
  17. {
  18. #region 变量
  19. //菜单栏状态,true为展开,false为收起
  20. bool meunStatic = false;
  21. //当前显示窗体
  22. Form showFrom = null;
  23. List<(CameraConfig CameraConfigValue, ShuLiConfigClass ShuLiConfigValue)> Configs =
  24. new List<(CameraConfig CameraConfigValue, ShuLiConfigClass ShuLiConfigValue)>();
  25. #endregion
  26. #region 实例
  27. //主线程实例队列
  28. public List<MainThreadClass> LsMainThread = new List<MainThreadClass>();
  29. public ModbusTcpClient modbusTcpClient = new ModbusTcpClient();
  30. #endregion
  31. #region 窗体事件
  32. /// <summary>
  33. /// 窗体加载事件
  34. /// </summary>
  35. public MainForm()
  36. {
  37. InitializeComponent();
  38. //modbusTcpClient.Connect("192.168.1.88");
  39. modbusTcpClient.Connect("127.0.0.1");
  40. if (File.Exists(".\\Config\\CCDCountConfig.xml"))
  41. {
  42. Configs = XmlStorage.DeserializeFromXml<List<(CameraConfig CameraConfigValue, ShuLiConfigClass ShuLiConfigValue)>>(".\\Config\\CCDCountConfig.xml");
  43. }
  44. SDKSystem.Initialize();
  45. FormKongjianInit();
  46. RunAllCameraIdentify();
  47. }
  48. /// <summary>
  49. /// 菜单切换按钮点击事件
  50. /// </summary>
  51. /// <param name="sender"></param>
  52. /// <param name="e"></param>
  53. private void MenuSwitchBtn_Click(object sender, EventArgs e)
  54. {
  55. if (meunStatic)
  56. {
  57. MenuSwitchBtn.Image = ScaleImage(Properties.Resources.进入__白_, 32, 32);
  58. HistoryFormBtn.Image = ScaleImage(Properties.Resources.数据_白, 32, 32);
  59. HistoryFormBtn.Text = "";
  60. IdentifyFormBtn.Image = ScaleImage(Properties.Resources.相机小_白, 32, 32);
  61. IdentifyFormBtn.Text = "";
  62. SettingFromBtn.Image = ScaleImage(Properties.Resources.设置_白, 32, 32);
  63. SettingFromBtn.Text = "";
  64. menupanel.Width = 70;
  65. meunStatic = false;
  66. }
  67. else
  68. {
  69. MenuSwitchBtn.Image = ScaleImage(Properties.Resources.返回__白_, 32, 32);
  70. IdentifyFormBtn.Image = null;
  71. IdentifyFormBtn.Text = "识 别 界 面";
  72. HistoryFormBtn.Image = null;
  73. HistoryFormBtn.Text = "数 据 界 面";
  74. SettingFromBtn.Image = null;
  75. SettingFromBtn.Text = "参 数 设 置";
  76. menupanel.Width = 200;
  77. meunStatic = true;
  78. }
  79. }
  80. /// <summary>
  81. /// 识别界面按钮点击事件
  82. /// </summary>
  83. /// <param name="sender"></param>
  84. /// <param name="e"></param>
  85. private void IdentifyFormBtn_Click(object sender, EventArgs e)
  86. {
  87. LoadFormContent(new IdentifyCameraForm(Configs.Select(o=>o.CameraConfigValue).ToList(),LsMainThread));
  88. }
  89. private void HistoryFormBtn_Click(object sender, EventArgs e)
  90. {
  91. LoadFormContent(new DataShowForm(LsMainThread));
  92. }
  93. private void MainForm_Resize(object sender, EventArgs e)
  94. {
  95. LoadFormContent(showFrom);
  96. }
  97. // <summary>
  98. /// 窗体关闭事件
  99. /// </summary>
  100. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  101. {
  102. SDKSystem.Finalize();
  103. StopAllCameraIdentify();
  104. this.Dispose();
  105. if(showFrom != null) showFrom.Dispose();
  106. Environment.Exit(0);
  107. }
  108. private void SettingFromBtn_Click(object sender, EventArgs e)
  109. {
  110. LoadFormContent(new SettingForm(LsMainThread));
  111. }
  112. private void MainForm_Load(object sender, EventArgs e)
  113. {
  114. HistoryFormBtn_Click(this, EventArgs.Empty);
  115. }
  116. #endregion
  117. #region 私有方法
  118. /// <summary>
  119. /// 图像缩放方法(保持宽高比)
  120. /// </summary>
  121. /// <param name="image">图像</param>
  122. /// <param name="maxWidth">目标宽度</param>
  123. /// <param name="maxHeight">目标高度</param>
  124. /// <returns></returns>
  125. private Image ScaleImage(Image image, int maxWidth, int maxHeight)
  126. {
  127. var ratioX = (double)maxWidth / image.Width;
  128. var ratioY = (double)maxHeight / image.Height;
  129. var ratio = Math.Min(ratioX, ratioY);
  130. var newWidth = (int)(image.Width * ratio);
  131. var newHeight = (int)(image.Height * ratio);
  132. var newImage = new Bitmap(newWidth, newHeight);
  133. using (var graphics = Graphics.FromImage(newImage))
  134. {
  135. graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  136. graphics.DrawImage(image, 0, 0, newWidth, newHeight);
  137. }
  138. return newImage;
  139. }
  140. /// <summary>
  141. /// 窗体控件初始化
  142. /// </summary>
  143. private void FormKongjianInit()
  144. {
  145. IdentifyFormBtn.Image = ScaleImage(Properties.Resources.相机小_白, 32, 32);
  146. IdentifyFormBtn.Text = "";
  147. HistoryFormBtn.Image = ScaleImage(Properties.Resources.数据_白, 32, 32);
  148. HistoryFormBtn.Text = "";
  149. MenuSwitchBtn.Image = ScaleImage(Properties.Resources.进入__白_, 32, 32);
  150. menupanel.Width = 70;
  151. SettingFromBtn.Image = ScaleImage(Properties.Resources.设置_白, 32, 32);
  152. SettingFromBtn.Text = "";
  153. //this.FormBorderStyle = FormBorderStyle.None;
  154. // 全屏显示
  155. this.WindowState = FormWindowState.Maximized;
  156. // 窗口置顶
  157. //this.TopMost = true;
  158. }
  159. /// <summary>
  160. /// 加载目标窗体内容到当前窗体
  161. /// </summary>
  162. /// <param name="targetForm">要加载的窗体实例</param>
  163. private void LoadFormContent(Form targetForm)
  164. {
  165. if (targetForm == null)
  166. return;
  167. if (showFrom == targetForm) { }
  168. else if (showFrom != null)
  169. {
  170. showFrom.Close();
  171. }
  172. showFrom = targetForm;
  173. targetForm.TopLevel = false; // 关键设置
  174. targetForm.FormBorderStyle = FormBorderStyle.None;
  175. targetForm.Dock = DockStyle.Fill; // 新增关键设置
  176. Mainpanel.Controls.Clear();
  177. Mainpanel.Controls.Add(targetForm);
  178. targetForm.Show();
  179. }
  180. /// <summary>
  181. /// 启动所有相机识别
  182. /// </summary>
  183. private void RunAllCameraIdentify()
  184. {
  185. if(Configs.Count == 0)
  186. {
  187. MessageBox.Show("尚未添加相机,请前往配置页面配置相机");
  188. return;
  189. }
  190. foreach (var item in Configs.Select(o => o.ShuLiConfigValue).ToList())
  191. {
  192. LsMainThread.Add(new MainThreadClass(item, Configs.Select(o => o.CameraConfigValue).ToList().Where(o=>o.CameraSNNum == item.CameraSN).First()));
  193. }
  194. for(int i = 0; i < LsMainThread.Count; i++)
  195. {
  196. //判断是否添加线程
  197. if (!LsMainThread[i].IsOpenLoadThread)
  198. return;
  199. LsMainThread[i].SetModbusClient(modbusTcpClient);
  200. //启动单相机实例的全部线程
  201. if(!LsMainThread[i].StartMianThread(Configs.Select(o => o.CameraConfigValue).ToList().Where(o=>o.CameraSNNum == LsMainThread[i].ThisCameraSN).First()))
  202. {
  203. LOG.error(LsMainThread[i].ThisCameraDevice+ "_"+ LsMainThread[i].ThisCameraSN + "启动失败");
  204. MessageBox.Show(LsMainThread[i].ThisCameraDevice + "_" + LsMainThread[i].ThisCameraSN + "启动失败");
  205. return;
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// 停止所有相机识别
  211. /// </summary>
  212. private void StopAllCameraIdentify()
  213. {
  214. foreach (var item in LsMainThread)
  215. {
  216. item.StopMianThread();
  217. }
  218. }
  219. #endregion
  220. }
  221. }