DO.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.Util;
  11. namespace Aitex.Core.Backend
  12. {
  13. public partial class DO : UserControl, IIOView
  14. {
  15. PeriodicJob _thread;
  16. public DO()
  17. {
  18. InitializeComponent();
  19. this.Load += new EventHandler(DO_Load);
  20. }
  21. void DO_Load(object sender, EventArgs e)
  22. {
  23. if (this.panel1.Controls.Count > 0)
  24. return;
  25. List<Tuple<int, int, string>> ioList = IO.GetIONameList(IOType.DO);
  26. int index = 0;
  27. foreach (var item in ioList)
  28. {
  29. Button button1 = new Button();
  30. button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  31. button1.Location = new System.Drawing.Point(index % 5 * 195, index / 5 * 22);
  32. //button1.Name = string.Format("DO_{0}", i);
  33. button1.Size = new System.Drawing.Size(194, 25);
  34. button1.Text = string.Format("{0} {1}", item.Item1, item.Item3);
  35. button1.Tag = item.Item3;
  36. button1.BackColor = Color.LightGray;
  37. button1.TextAlign = ContentAlignment.MiddleCenter;
  38. button1.Font = new System.Drawing.Font("Arial,SimSun", 6.1F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  39. button1.UseVisualStyleBackColor = false;
  40. button1.Click += (sender2, e2) =>
  41. {
  42. var btn = (Button)sender2;
  43. string name = (string)btn.Tag;
  44. string reason;
  45. if (!IO.DO[name].SetValue(!IO.DO[name].Value, out reason))
  46. {
  47. MessageBox.Show(reason);
  48. }
  49. };
  50. this.panel1.Controls.Add(button1);
  51. index++;
  52. }
  53. _thread = new PeriodicJob(500, OnTimer, "DOTimer", false, true);
  54. VisibleChanged += (sender1, e1) =>
  55. {
  56. if (Visible) _thread.Start(); else _thread.Pause();
  57. };
  58. }
  59. bool OnTimer()
  60. {
  61. Invoke(new Action(() =>
  62. {
  63. foreach (Button ctrl in this.panel1.Controls)
  64. {
  65. if (ctrl != null)
  66. {
  67. string name = (string)ctrl.Tag;
  68. ctrl.BackColor = IO.DO[(string)ctrl.Tag].Value ? Color.Lime : Color.LightGray;
  69. }
  70. }
  71. }));
  72. return true;
  73. }
  74. public void EnableTimer(bool enable)
  75. {
  76. if (enable)
  77. _thread.Start();
  78. else
  79. _thread.Pause();
  80. }
  81. public void Close()
  82. {
  83. if (_thread != null)
  84. _thread.Stop();
  85. }
  86. }
  87. }