FormCentralController.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. using centralController.model;
  2. using DevComponents.DotNetBar.Controls;
  3. using nettyCommunication;
  4. using parkMonitor.model;
  5. using PLC_Communication;
  6. using PLCConnector;
  7. using PLCS7;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Net;
  13. using System.Runtime.InteropServices;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. using Terminal;
  18. using Excel = Microsoft.Office.Interop.Excel;
  19. namespace centralController
  20. {
  21. public partial class centralController : Form
  22. {
  23. private List<TermModel> TermDispList;
  24. private bool closing = false;
  25. /// <summary>
  26. /// 窗体初始化
  27. /// </summary>
  28. public centralController()
  29. {
  30. InitializeComponent();
  31. TermDispList = new List<TermModel>();
  32. if (Monitor.Monitor.ins == null)
  33. {
  34. Monitor.Monitor.ins = new Monitor.Monitor(this, flowLayoutPanel1.Handle);
  35. }
  36. Monitor.Monitor.ins.Start();
  37. //进度条显示
  38. Task.Factory.StartNew(() =>
  39. {
  40. int temp = 0;
  41. while (true)
  42. {
  43. if (temp != Monitor.Monitor.initializeState)
  44. {
  45. temp = Monitor.Monitor.initializeState;
  46. progressBar1.Invoke(new Action(() => { progressBar1.Value = temp * 20; }));
  47. }
  48. if (temp == 5)
  49. {
  50. dgvx_parkingRecords.Invoke(new Action(() => { RefreshRecords(null, null); }));
  51. break;
  52. }
  53. Thread.Sleep(300);
  54. }
  55. });
  56. //定时器,定时更新停车记录
  57. System.Windows.Forms.Timer recordsTimer = new System.Windows.Forms.Timer();
  58. recordsTimer.Interval = 10000;
  59. recordsTimer.Tick += new EventHandler(RefreshRecords);
  60. recordsTimer.Start();
  61. ////定时器,定时更新提示信息
  62. //System.Windows.Forms.Timer NoteTimer = new System.Windows.Forms.Timer();
  63. //NoteTimer.Interval = 1000;
  64. //NoteTimer.Tick += new EventHandler(UpdateText);
  65. //NoteTimer.Start();
  66. //定时器,定时更新系统时间与剩余车位数
  67. System.Windows.Forms.Timer freeSpaceTimer = new System.Windows.Forms.Timer();
  68. freeSpaceTimer.Interval = 1000;
  69. freeSpaceTimer.Tick += new EventHandler(UpdateFreeSpace);
  70. freeSpaceTimer.Start();
  71. rtb_notification.Text = "";
  72. //地面车位显示
  73. Task.Factory.StartNew(() =>
  74. {
  75. UpdateAllTerms();
  76. });
  77. }
  78. /// <summary>
  79. /// 实时更新所有车位显示
  80. /// </summary>
  81. private void UpdateAllTerms()
  82. {
  83. while (!closing)
  84. {
  85. List<TerminalStru> tList = Terminal.Terminal.terminalInfo;
  86. this.Invoke(new Action(() =>
  87. {
  88. int currentDispID = 0;
  89. //出现终端丢失情况,必定是与PLC连接问题导致数据异常,重新初始化显示模块
  90. for (int i = 0; i < TermDispList.Count; i++)
  91. {
  92. bool finded = false;
  93. if (currentDispID < TermDispList[i].id)
  94. {
  95. currentDispID = TermDispList[i].id;
  96. for (int j = 0; j < tList.Count; j++)
  97. {
  98. if (currentDispID == tList[j].terminalID)
  99. {
  100. finded = true; break;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. finded = false;
  107. }
  108. if (!finded)
  109. {
  110. flp_Term.Controls.Clear();
  111. TermDispList.Clear();
  112. Console.WriteLine("******************************");
  113. break;
  114. }
  115. }
  116. for (int i = 0; i < tList.Count; i++)
  117. {
  118. TerminalStru ts = tList[i];
  119. bool updated = false;
  120. for (int j = 0; j < TermDispList.Count; j++)
  121. {
  122. if (ts.terminalID == TermDispList[j].id)
  123. {
  124. UpdateTermGP(TermDispList[j], ts.terminalID,
  125. ts.terminalStatus == (short)1 ? true : false,
  126. ts.cmd == ts.terminalStatus ? true : false,
  127. ts.groundStatus == (short)1 ? true : false,
  128. ts.numMachineLaunch == (short)1 ? true : false);
  129. updated = true;
  130. break;
  131. }
  132. }
  133. if (!updated)
  134. InitTermGP(ts.terminalID,
  135. ts.terminalStatus == (short)1 ? true : false,
  136. ts.cmd == ts.terminalStatus ? true : false,
  137. ts.groundStatus == (short)1 ? true : false,
  138. ts.numMachineLaunch == (short)1 ? true : false);
  139. }
  140. }));
  141. Thread.Sleep(5000);
  142. }
  143. }
  144. /// <summary>
  145. /// 初始化地面车位信息
  146. /// </summary>
  147. /// <param name="id"></param>
  148. /// <param name="park"></param>
  149. /// <param name="launched"></param>
  150. /// <param name="groundSensor"></param>
  151. /// <param name="numMachine"></param>
  152. private void InitTermGP(int id, bool park, bool launched, bool groundSensor, bool numMachine)
  153. {
  154. GroupPanel gpTerm = new GroupPanel();
  155. PictureBox pbCar = new PictureBox();
  156. PictureBox pbNum = new PictureBox();
  157. pbNum.Dock = DockStyle.Top;
  158. //pbNum.Anchor = AnchorStyles.Top;
  159. pbNum.Image = Properties.Resources.numMachine;
  160. pbNum.Location = new Point(27, 1);
  161. pbNum.Name = "term" + id + "pbNum";
  162. pbNum.Size = new Size(50, 29);
  163. pbNum.SizeMode = PictureBoxSizeMode.Zoom;
  164. pbNum.TabStop = false;
  165. pbCar.Dock = DockStyle.Bottom;
  166. //pbCar.Anchor = ((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right);
  167. pbCar.Image = Properties.Resources.car;
  168. pbCar.Location = new Point(23, 45);
  169. pbCar.Name = "term" + id + "pbCar";
  170. pbCar.Size = new Size(56, 43);
  171. pbCar.SizeMode = PictureBoxSizeMode.Zoom;
  172. gpTerm.BackColor = SystemColors.AppWorkspace;
  173. gpTerm.CanvasColor = SystemColors.Control;
  174. gpTerm.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2007;
  175. gpTerm.ColorTable = ePanelColorTable.Red;
  176. gpTerm.Controls.Add(pbCar);
  177. gpTerm.Controls.Add(pbNum);
  178. gpTerm.DisabledBackColor = Color.Empty;
  179. gpTerm.Name = "term" + id + "gp";
  180. gpTerm.Size = new Size(105, 112);
  181. gpTerm.Style.BackColor = Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(191)))), ((int)(((byte)(191)))));
  182. gpTerm.Style.BackColor2 = Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(150)))), ((int)(((byte)(150)))));
  183. gpTerm.Style.BackColorGradientAngle = 90;
  184. gpTerm.Style.BorderBottom = DevComponents.DotNetBar.eStyleBorderType.Solid;
  185. gpTerm.Style.BorderBottomWidth = 1;
  186. gpTerm.Style.BorderColor = Color.FromArgb(((int)(((byte)(149)))), ((int)(((byte)(55)))), ((int)(((byte)(52)))));
  187. gpTerm.Style.BorderLeft = DevComponents.DotNetBar.eStyleBorderType.Solid;
  188. gpTerm.Style.BorderLeftWidth = 1;
  189. gpTerm.Style.BorderRight = DevComponents.DotNetBar.eStyleBorderType.Solid;
  190. gpTerm.Style.BorderRightWidth = 1;
  191. gpTerm.Style.BorderTop = DevComponents.DotNetBar.eStyleBorderType.Solid;
  192. gpTerm.Style.BorderTopWidth = 1;
  193. gpTerm.Style.CornerDiameter = 4;
  194. gpTerm.Style.CornerType = DevComponents.DotNetBar.eCornerType.Rounded;
  195. gpTerm.Style.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
  196. gpTerm.Style.TextColor = Color.FromArgb(((int)(((byte)(99)))), ((int)(((byte)(36)))), ((int)(((byte)(35)))));
  197. gpTerm.Style.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Near;
  198. gpTerm.StyleMouseDown.CornerType = DevComponents.DotNetBar.eCornerType.Square;
  199. gpTerm.StyleMouseOver.CornerType = DevComponents.DotNetBar.eCornerType.Square;
  200. gpTerm.Text = "地面车位" + id;
  201. TermModel tm = new TermModel(gpTerm, pbNum, pbCar);
  202. UpdateTermGP(tm, id, park, launched, groundSensor, numMachine);
  203. TermDispList.Add(tm);
  204. flp_Term.Controls.Add(gpTerm);
  205. }
  206. /// <summary>
  207. /// 更新地面车位信息
  208. /// </summary>
  209. /// <param name="tm"></param>
  210. /// <param name="id"></param>
  211. /// <param name="park"></param>
  212. /// <param name="launched"></param>
  213. /// <param name="groundSensor"></param>
  214. /// <param name="numMachine"></param>
  215. private void UpdateTermGP(TermModel tm, int id, bool park, bool launched, bool groundSensor, bool numMachine)
  216. {
  217. tm.UpdateStatus(id, park, launched, groundSensor, numMachine);
  218. if (tm.gp != null)
  219. {
  220. //停取状态
  221. if (park)
  222. tm.gp.ColorTable = ePanelColorTable.Red;
  223. else
  224. tm.gp.ColorTable = ePanelColorTable.Green;
  225. //启动/空闲
  226. if (launched)
  227. tm.gp.BackColor = SystemColors.Highlight;
  228. else
  229. tm.gp.BackColor = SystemColors.AppWorkspace;
  230. //地感
  231. if (groundSensor && tm.pbCar != null)
  232. tm.pbCar.Visible = true;
  233. else
  234. tm.pbCar.Visible = false;
  235. //号牌机
  236. if (numMachine && tm.pbNum != null)
  237. tm.pbNum.Visible = true;
  238. else
  239. tm.pbNum.Visible = false;
  240. }
  241. }
  242. #region 测试
  243. /// <summary>
  244. /// 获取号牌测试
  245. /// </summary>
  246. /// <param name="sender"></param>
  247. /// <param name="e"></param>
  248. private void button1_Click(object sender, EventArgs e)
  249. {
  250. string ip = textBox1.Text;
  251. IPAddress temp;
  252. if (IPAddress.TryParse(ip, out temp))
  253. {
  254. string license = "";
  255. int id = 0;
  256. int.TryParse(textBox1.Text, out id);
  257. Task.Factory.StartNew(() =>
  258. {
  259. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(id);
  260. if (textBox2.InvokeRequired)
  261. {
  262. textBox2.Invoke(new Action(() => { textBox2.Text = license; }));
  263. }
  264. else
  265. {
  266. textBox2.Text = license;
  267. }
  268. });
  269. }
  270. }
  271. /// <summary>
  272. /// 屏显测试
  273. /// </summary>
  274. /// <param name="sender"></param>
  275. /// <param name="e"></param>
  276. private void button2_Click(object sender, EventArgs e)
  277. {
  278. string disp = textBox1.Text;
  279. string audio = textBox2.Text;
  280. Monitor.Monitor.allInOneMachine.DispForAWhile(0, disp, 10, 1, audio);
  281. }
  282. #endregion
  283. #region 系统
  284. /// <summary>
  285. /// 结束按钮
  286. /// </summary>
  287. /// <param name="sender"></param>
  288. /// <param name="e"></param>
  289. private void btn_exit_Click(object sender, EventArgs e)
  290. {
  291. //DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  292. //if (result.Equals(DialogResult.OK))
  293. //{
  294. // Monitor.Monitor.ins.Stop();
  295. // Close();
  296. //}
  297. Close();
  298. }
  299. #endregion
  300. #region 文件
  301. /// <summary>
  302. /// 打开日志
  303. /// </summary>
  304. /// <param name="sender"></param>
  305. /// <param name="e"></param>
  306. private void btn_openFile_Click(object sender, EventArgs e)
  307. {
  308. }
  309. /// <summary>
  310. /// 刷新停车记录按钮
  311. /// </summary>
  312. /// <param name="sender"></param>
  313. /// <param name="e"></param>
  314. private void btn_recordManagement_Click(object sender, EventArgs e)
  315. {
  316. FormRecordsManager formRecordsManager = new FormRecordsManager();
  317. formRecordsManager.ShowDialog();
  318. }
  319. /// <summary>
  320. /// 导出停车记录按钮
  321. /// </summary>
  322. /// <param name="sender"></param>
  323. /// <param name="e"></param>
  324. private void btn_exportRecords_Click(object sender, EventArgs e)
  325. {
  326. Export(dgvx_parkingRecords, Directory.GetCurrentDirectory() + "\\");
  327. Console.WriteLine(Directory.GetCurrentDirectory() + "\\");
  328. }
  329. /// <summary>
  330. /// 显示与设置收费策略
  331. /// </summary>
  332. /// <param name="sender"></param>
  333. /// <param name="e"></param>
  334. private void btn_setScheme_Click(object sender, EventArgs e)
  335. {
  336. FormPaymentScheme form_PaymentScheme = new FormPaymentScheme();
  337. form_PaymentScheme.ShowDialog();
  338. }
  339. /// <summary>
  340. /// 手动更新广告
  341. /// </summary>
  342. /// <param name="sender"></param>
  343. /// <param name="e"></param>
  344. private void btn_advertUpdate_Click(object sender, EventArgs e)
  345. {
  346. Task.Factory.StartNew(() =>
  347. {
  348. MessageBox.Show("更新中,请稍候。", "提示");
  349. string adAlert = "";
  350. if (Monitor.Monitor.advertMgr != null)
  351. {
  352. if (Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert))
  353. MessageBox.Show("更新成功\n" + adAlert, "提示");
  354. else
  355. MessageBox.Show("更新失败,请稍后重试", "提示");
  356. }
  357. });
  358. }
  359. #endregion
  360. #region 视图
  361. /// <summary>
  362. /// 车位视图
  363. /// </summary>
  364. /// <param name="sender"></param>
  365. /// <param name="e"></param>
  366. private void btn_parkingSpace_Click(object sender, EventArgs e)
  367. {
  368. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  369. if (dci_ParkingSpace.Visible == false) { dci_ParkingSpace.Visible = true; }
  370. }
  371. /// <summary>
  372. /// 显示号牌机视图
  373. /// </summary>
  374. /// <param name="sender"></param>
  375. /// <param name="e"></param>
  376. private void btn_numMachine_Click(object sender, EventArgs e)
  377. {
  378. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  379. if (dci_NumMachine.Visible == false) { dci_NumMachine.Visible = true; }
  380. }
  381. /// <summary>
  382. /// 支付视图
  383. /// </summary>
  384. /// <param name="sender"></param>
  385. /// <param name="e"></param>
  386. private void btn_payment_Click(object sender, EventArgs e)
  387. {
  388. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  389. if (dci_payment.Visible == false) { dci_payment.Visible = true; }
  390. }
  391. /// <summary>
  392. /// 显示测试选项卡
  393. /// </summary>
  394. /// <param name="sender"></param>
  395. /// <param name="e"></param>
  396. private void btn_test_Click(object sender, EventArgs e)
  397. {
  398. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  399. if (dci_Test.Visible == false) { dci_Test.Visible = true; }
  400. }
  401. /// <summary>
  402. /// 终端视图
  403. /// </summary>
  404. /// <param name="sender"></param>
  405. /// <param name="e"></param>
  406. private void btn_terminals_Click(object sender, EventArgs e)
  407. {
  408. if (bar_side.Visible == false) { bar_side.Visible = true; }
  409. if (dci_terminal.Visible == false) { dci_terminal.Visible = true; }
  410. }
  411. /// <summary>
  412. /// 停取记录
  413. /// </summary>
  414. /// <param name="sender"></param>
  415. /// <param name="e"></param>
  416. private void btn_records_Click(object sender, EventArgs e)
  417. {
  418. if (bar_bottom.Visible == false) { bar_bottom.Visible = true; }
  419. if (dci_ParkingRecords.Visible == false) { dci_ParkingRecords.Visible = true; }
  420. }
  421. #endregion
  422. #region 设备
  423. /// <summary>
  424. /// 手动连接PLC
  425. /// </summary>
  426. /// <param name="sender"></param>
  427. /// <param name="e"></param>
  428. private void btn_linkPLC_Click(object sender, EventArgs e)
  429. {
  430. if (Monitor.Monitor.PLC != null)
  431. {
  432. if (!Monitor.Monitor.PLC.isConnected)
  433. {
  434. Monitor.Monitor.PLC.PLCConnect();
  435. }
  436. else
  437. {
  438. List<object> received = Monitor.Monitor.PLC.ReadFromPLC(PLCDataType.terminal, 0);
  439. if (received.Count != Monitor.Monitor.plcTerminalCount)
  440. {
  441. Monitor.Monitor.SetNotification("检测到PLC数据异常", TextColor.Warning);
  442. Monitor.Monitor.PLC.PLCDisconnect();
  443. Monitor.Monitor.PLC.PLCConnect();
  444. }
  445. else
  446. {
  447. Monitor.Monitor.SetNotification("PLC连接正常,无需连接", TextColor.Info);
  448. }
  449. }
  450. }
  451. }
  452. /// <summary>
  453. /// 手动连接Web
  454. /// </summary>
  455. /// <param name="sender"></param>
  456. /// <param name="e"></param>
  457. private void btn_linkWeb_Click(object sender, EventArgs e)
  458. {
  459. Task.Factory.StartNew(() =>
  460. {
  461. Monitor.Monitor.webServer.Connect();
  462. });
  463. }
  464. /// <summary>
  465. /// PLC数据调试工具
  466. /// </summary>
  467. /// <param name="sender"></param>
  468. /// <param name="e"></param>
  469. private void btn_PLCConf_Click(object sender, EventArgs e)
  470. {
  471. FormPLCConf formPLCConf = new FormPLCConf();
  472. formPLCConf.Show();
  473. }
  474. /// <summary>
  475. /// 启动号牌机调试工具
  476. /// </summary>
  477. /// <param name="sender"></param>
  478. /// <param name="e"></param>
  479. private void btn_numMachineConf_Click(object sender, EventArgs e)
  480. {
  481. FormModbus form = new FormModbus();
  482. form.Show();
  483. }
  484. #endregion
  485. /// <summary>
  486. /// 更新提示信息,将被从外部调用
  487. /// </summary>
  488. /// <param name="sender"></param>
  489. /// <param name="e"></param>
  490. public void UpdateText(string str, TextColor tc)
  491. {
  492. if (str == "clear")
  493. {
  494. rtb_notification.Clear();
  495. }
  496. else
  497. {
  498. string appendingText = str;
  499. rtb_notification.AppendText(appendingText);
  500. string currentTxt = rtb_notification.Text;
  501. string temp = currentTxt.Substring(0, currentTxt.Length - appendingText.Length + 2);
  502. int index = temp.LastIndexOf("\n", temp.Length);
  503. //rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 1);
  504. rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 2);
  505. //Console.WriteLine("******************");
  506. //Console.WriteLine("选中 "+rtb_notification.SelectedText);
  507. //Console.WriteLine("******************");
  508. //rtb_notification.SelectionFont = new Font(rtb_notification.SelectionFont, FontStyle.Bold);
  509. switch (tc)
  510. {
  511. case TextColor.Log:
  512. rtb_notification.SelectionColor = Color.Gray;
  513. break;
  514. case TextColor.Info:
  515. rtb_notification.SelectionColor = Color.Black;
  516. break;
  517. case TextColor.Warning:
  518. rtb_notification.SelectionColor = Color.Orange;
  519. break;
  520. case TextColor.Error:
  521. rtb_notification.SelectionColor = Color.Red;
  522. break;
  523. }
  524. rtb_notification.Select(index + appendingText.Length - 1, 0);
  525. }
  526. //rtb_notification.Text = Monitor.Monitor.ins.GetNotification();
  527. if (!rtb_notification.Focused)
  528. rtb_notification.ScrollToCaret();
  529. }
  530. /// <summary>
  531. /// 定时更新时间与空闲车位显示,以及设备连接状态
  532. /// </summary>
  533. /// <param name="sender"></param>
  534. /// <param name="e"></param>
  535. private void UpdateFreeSpace(object sender, EventArgs e)
  536. {
  537. string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  538. string freeSpace = "剩余车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(0);
  539. string bookableSpace = "可预约车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(2);
  540. lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace;
  541. lbx_PLCStatus.SymbolColor = (Monitor.Monitor.PLC != null && Monitor.Monitor.PLC.isConnected) ? Color.Green : Color.OrangeRed;
  542. lbx_webStatus.SymbolColor = (Monitor.Monitor.webServer != null && Monitor.Monitor.webServer.GetConnectStatus()) ? Color.Green : Color.OrangeRed;
  543. }
  544. /// <summary>
  545. /// 刷新停车记录显示
  546. /// </summary>
  547. /// <param name="data"></param>
  548. private void RefreshRecords(object data, EventArgs e)
  549. {
  550. dgvx_parkingRecords.Rows.Clear();
  551. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  552. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  553. while (listEnumer.MoveNext())
  554. {
  555. dgvx_parkingRecords.Rows.Add(listEnumer.Current);
  556. }
  557. }
  558. /// <summary>
  559. /// 导出excel文件
  560. /// </summary>
  561. /// <param name="dataGridView1"></param>
  562. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  563. private void Export(DataGridViewX dataGridView1, string path)
  564. {
  565. try
  566. {
  567. if (dataGridView1.Rows.Count == 0)
  568. {
  569. return;
  570. }
  571. Excel.Application excel = new Excel.Application();
  572. excel.Visible = false;
  573. Excel.Workbook workbook = excel.Workbooks.Add(true);
  574. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  575. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  576. {
  577. if (dataGridView1.Columns[i].Visible == true)
  578. {
  579. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  580. }
  581. }
  582. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  583. {
  584. System.Windows.Forms.Application.DoEvents();
  585. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  586. {
  587. if (dataGridView1.Columns[j].Visible == true)
  588. {
  589. if (dataGridView1[j, i].ValueType == typeof(string))
  590. {
  591. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  592. }
  593. else
  594. {
  595. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  596. }
  597. }
  598. }
  599. }
  600. //设置禁止弹出保存和覆盖的询问提示框
  601. excel.DisplayAlerts = false;
  602. excel.AlertBeforeOverwriting = false;
  603. worksheet.Cells.Columns.AutoFit();
  604. //保存写入的数据,这里还没有保存到磁盘
  605. workbook.Saved = true;
  606. if (Directory.Exists(path))
  607. {
  608. //设置新建文件路径及名称
  609. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  610. //创建文件
  611. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  612. //关闭释放流,不然没办法写入数据
  613. file.Close();
  614. file.Dispose();
  615. //保存到指定的路径
  616. workbook.SaveCopyAs(savePath);
  617. }
  618. workbook.Close(false, Type.Missing, Type.Missing);
  619. //确保Excel进程关闭
  620. excel.Quit();
  621. //释放 COM 对象
  622. Marshal.ReleaseComObject(worksheet);
  623. Marshal.ReleaseComObject(workbook);
  624. Marshal.ReleaseComObject(excel);
  625. excel = null;
  626. worksheet = null;
  627. workbook = null;
  628. GC.Collect();//如果不使用这条语,excel会无法正常退出
  629. }
  630. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  631. }
  632. /// <summary>
  633. /// 动态调整号牌机界面
  634. /// </summary>
  635. /// <param name="sender"></param>
  636. /// <param name="e"></param>
  637. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  638. {
  639. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  640. try
  641. {
  642. int rows = (int)Math.Sqrt(flowLayoutPanel1.Controls.Count + 1);
  643. int columns = (int)(flowLayoutPanel1.Controls.Count * 1.0 / rows + 1);
  644. for (int i = 0; i < flpCC.Count; i++)
  645. {
  646. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 1.0 / (columns * 1.1));
  647. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 1.0 / (rows * 1.15));
  648. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  649. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  650. Control.ControlCollection tlpCC = flpCC[i].Controls;
  651. if (tlpCC.Count == 2)
  652. {
  653. tlpCC[0].Width = (int)(flpCC[i].Width * 0.95);
  654. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  655. tlpCC[1].Width = (int)(flpCC[i].Width * 0.95);
  656. tlpCC[1].Height = (int)(flpCC[i].Height * 0.20);
  657. }
  658. }
  659. }
  660. catch (Exception ex) { Console.WriteLine(ex.Message); }
  661. }
  662. /// <summary>
  663. /// 右上角关闭函数
  664. /// </summary>
  665. /// <param name="sender"></param>
  666. /// <param name="e"></param>
  667. private void centralController_FormClosing(object sender, FormClosingEventArgs e)
  668. {
  669. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  670. if (result.Equals(DialogResult.OK))
  671. {
  672. Monitor.Monitor.ins.Stop();
  673. closing = true;
  674. }
  675. else
  676. {
  677. e.Cancel = true;
  678. }
  679. }
  680. #region 帮助
  681. /// <summary>
  682. /// 系统信息
  683. /// </summary>
  684. /// <param name="sender"></param>
  685. /// <param name="e"></param>
  686. private void btn_sysInfo_Click(object sender, EventArgs e)
  687. {
  688. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  689. sysInfo.ShowDialog();
  690. }
  691. #endregion
  692. #region 快捷菜单栏
  693. /// <summary>
  694. /// 快捷显示系统信息
  695. /// </summary>
  696. /// <param name="sender"></param>
  697. /// <param name="e"></param>
  698. private void shortcut_sysInfo_Click(object sender, EventArgs e)
  699. {
  700. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  701. sysInfo.ShowDialog();
  702. }
  703. /// <summary>
  704. /// 快捷关闭
  705. /// </summary>
  706. /// <param name="sender"></param>
  707. /// <param name="e"></param>
  708. private void shortcut_close_Click(object sender, EventArgs e)
  709. {
  710. Close();
  711. }
  712. #endregion
  713. }
  714. }