FormCentralController.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using DevComponents.DotNetBar.Controls;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Excel = Microsoft.Office.Interop.Excel;
  11. namespace centralController
  12. {
  13. public partial class centralController : Form
  14. {
  15. /// <summary>
  16. /// 窗体初始化
  17. /// </summary>
  18. public centralController()
  19. {
  20. InitializeComponent();
  21. if (Monitor.Monitor.ins == null)
  22. {
  23. Monitor.Monitor.ins = new Monitor.Monitor(flowLayoutPanel1.Handle);
  24. Monitor.Monitor.ins.Start();
  25. }
  26. //进度条测试
  27. Task.Factory.StartNew(() =>
  28. {
  29. int temp = 0;
  30. while (true)
  31. {
  32. if (temp != Monitor.Monitor.initializeState)
  33. {
  34. temp = Monitor.Monitor.initializeState;
  35. progressBar1.Invoke(new Action(() => { progressBar1.Value = temp * 20; }));
  36. }
  37. if (temp == 5) {
  38. dataGridViewX1.Invoke(new Action(() => { RefreshRecords(null, null); }));
  39. break;
  40. }
  41. Thread.Sleep(200);
  42. }
  43. });
  44. //定时器,定时更新提示信息
  45. System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
  46. timer.Interval = 1000;
  47. timer.Tick += new EventHandler(UpdateText);
  48. timer.Start();
  49. UpdateBasicInfo();
  50. tbx_notification.Text = "";
  51. }
  52. /// <summary>
  53. /// 定时更新提示信息
  54. /// </summary>
  55. /// <param name="sender"></param>
  56. /// <param name="e"></param>
  57. private void UpdateText(object sender, EventArgs e)
  58. {
  59. tbx_notification.Text = Monitor.Monitor.GetNotification();
  60. }
  61. /// <summary>
  62. /// 结束按钮
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void btn_exit_Click(object sender, EventArgs e)
  67. {
  68. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  69. if (result.Equals(DialogResult.OK))
  70. {
  71. Monitor.Monitor.ins.Stop();
  72. Close();
  73. }
  74. }
  75. /// <summary>
  76. /// 获取号牌测试
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  80. private void button1_Click(object sender, EventArgs e)
  81. {
  82. string ip = textBox1.Text;
  83. IPAddress temp;
  84. if (IPAddress.TryParse(ip, out temp))
  85. {
  86. string license = "";
  87. Task.Factory.StartNew(() =>
  88. {
  89. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(1);
  90. if (textBox2.InvokeRequired)
  91. {
  92. textBox2.Invoke(new Action(() => { textBox2.Text = license; }));
  93. }
  94. else
  95. {
  96. textBox2.Text = license;
  97. }
  98. });
  99. }
  100. }
  101. /// <summary>
  102. /// 手动连接PLC
  103. /// </summary>
  104. /// <param name="sender"></param>
  105. /// <param name="e"></param>
  106. private void btn_linkPLC_Click(object sender, EventArgs e)
  107. {
  108. if (!Monitor.Monitor.PLC.isConnected)
  109. {
  110. Monitor.Monitor.PLC.PLCConnect();
  111. }
  112. }
  113. /// <summary>
  114. /// 刷新停车记录按钮
  115. /// </summary>
  116. /// <param name="sender"></param>
  117. /// <param name="e"></param>
  118. private void btn_refreshRecords_Click(object sender, EventArgs e)
  119. {
  120. RefreshRecords(null, null);
  121. }
  122. /// <summary>
  123. /// 刷新停车记录显示
  124. /// </summary>
  125. /// <param name="data"></param>
  126. private void RefreshRecords(object data, EventArgs e)
  127. {
  128. dataGridViewX1.Rows.Clear();
  129. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  130. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  131. while (listEnumer.MoveNext())
  132. {
  133. dataGridViewX1.Rows.Add(listEnumer.Current);
  134. }
  135. }
  136. /// <summary>
  137. /// 导出excel文件
  138. /// </summary>
  139. /// <param name="dataGridView1"></param>
  140. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  141. private void Export(DataGridViewX dataGridView1, string path)
  142. {
  143. try
  144. {
  145. if (dataGridView1.Rows.Count == 0)
  146. {
  147. return;
  148. }
  149. Excel.Application excel = new Excel.Application();
  150. excel.Visible = false;
  151. Excel.Workbook workbook = excel.Workbooks.Add(true);
  152. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  153. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  154. {
  155. if (dataGridView1.Columns[i].Visible == true)
  156. {
  157. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  158. }
  159. }
  160. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  161. {
  162. System.Windows.Forms.Application.DoEvents();
  163. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  164. {
  165. if (dataGridView1.Columns[j].Visible == true)
  166. {
  167. if (dataGridView1[j, i].ValueType == typeof(string))
  168. {
  169. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  170. }
  171. else
  172. {
  173. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  174. }
  175. }
  176. }
  177. }
  178. //设置禁止弹出保存和覆盖的询问提示框
  179. excel.DisplayAlerts = false;
  180. excel.AlertBeforeOverwriting = false;
  181. worksheet.Cells.Columns.AutoFit();
  182. //保存写入的数据,这里还没有保存到磁盘
  183. workbook.Saved = true;
  184. if (Directory.Exists(path))
  185. {
  186. //设置新建文件路径及名称
  187. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  188. //创建文件
  189. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  190. //关闭释放流,不然没办法写入数据
  191. file.Close();
  192. file.Dispose();
  193. //保存到指定的路径
  194. workbook.SaveCopyAs(savePath);
  195. }
  196. workbook.Close(false, Type.Missing, Type.Missing);
  197. //确保Excel进程关闭
  198. excel.Quit();
  199. //释放 COM 对象
  200. Marshal.ReleaseComObject(worksheet);
  201. Marshal.ReleaseComObject(workbook);
  202. Marshal.ReleaseComObject(excel);
  203. excel = null;
  204. worksheet = null;
  205. workbook = null;
  206. GC.Collect();//如果不使用这条语,excel会无法正常退出
  207. }
  208. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  209. }
  210. /// <summary>
  211. /// 更新基本信息
  212. /// </summary>
  213. private void UpdateBasicInfo()
  214. {
  215. string sysInfo = Monitor.Monitor.GetSysInfo();
  216. tbx_sysInfo.Text = sysInfo;
  217. }
  218. /// <summary>
  219. /// 导出停车记录按钮
  220. /// </summary>
  221. /// <param name="sender"></param>
  222. /// <param name="e"></param>
  223. private void btn_exportRecords_Click(object sender, EventArgs e)
  224. {
  225. Export(dataGridViewX1, Directory.GetCurrentDirectory() + "\\");
  226. Console.WriteLine(Directory.GetCurrentDirectory() + "\\");
  227. }
  228. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  229. {
  230. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  231. try
  232. {
  233. for (int i = 0; i < flpCC.Count; i++)
  234. {
  235. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 0.3);
  236. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 0.49);
  237. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  238. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  239. Control.ControlCollection tlpCC = flpCC[i].Controls;
  240. if (tlpCC.Count == 2)
  241. {
  242. tlpCC[0].Width = flpCC[i].Width - 5;
  243. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  244. tlpCC[1].Width = flpCC[i].Width - 5;
  245. tlpCC[1].Height = (int)(flpCC[i].Height * 0.19);
  246. //Console.WriteLine(tlpCC[0].Width + "," + tlpCC[0].Height);
  247. //Console.WriteLine(tlpCC[1].Width + "," + tlpCC[1].Height);
  248. }
  249. }
  250. }
  251. catch (Exception ex) { Console.WriteLine(ex.Message); }
  252. }
  253. }
  254. }