版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

124 wiersze
4.3 KiB

  1. using MaiMuAOI.SysCtrl;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using System.Xml.Linq;
  16. namespace ProductionControl.UI
  17. {
  18. public partial class UIForLib : UserControl
  19. {
  20. SynchronizationContext SyncContext = null;
  21. public Action<string> GetParamsEvent;
  22. //
  23. private ForProp prop = new ForProp();
  24. public UIForLib()
  25. {
  26. InitializeComponent();
  27. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  28. //获取UI线程同步上下文
  29. SyncContext = SynchronizationContext.Current;
  30. init();
  31. }
  32. public void init()
  33. {
  34. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  35. txtLog.Text = "";
  36. this.propertyGrid1.SelectedObject = prop;
  37. }
  38. public string getParamsData()
  39. {
  40. return prop.serialize();
  41. }
  42. public void setParamsData(string json)
  43. {
  44. if (json == "") return;
  45. prop.deserialize(json);
  46. this.propertyGrid1.Refresh();
  47. }
  48. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  49. {
  50. //其中包含了两个重要的属性:OldValue和ChangeItem。
  51. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  52. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  53. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  54. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  55. var oldValue = e.OldValue;
  56. var newValue = e.ChangedItem.Value;
  57. switch (propertyName)
  58. {
  59. case "GotoStepIndex":
  60. if ((int)newValue < 1)
  61. {
  62. prop.GotoStepIndex = 1;
  63. this.refreshUI();
  64. }
  65. break;
  66. case "LimitNum":
  67. if ((int)newValue < 1)
  68. {
  69. prop.LimitNum = 1;
  70. this.refreshUI();
  71. }
  72. break;
  73. }
  74. }
  75. private void refreshUI()
  76. {
  77. SyncContext.Post(m =>
  78. {
  79. var result = m as string;
  80. propertyGrid1.Refresh();
  81. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  82. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  83. }, "异步操作完成结果");
  84. }
  85. private void tbtnSave_Click(object sender, EventArgs e)
  86. {
  87. GetParamsEvent?.Invoke(prop.serialize());
  88. }
  89. private void tbtnExport_Click(object sender, EventArgs e)
  90. {
  91. prop.UniqueId = DateTime.Now.Ticks;
  92. string filePath = ConfMgr.SaveAsFile($"For配置.json", "JSON|*.json");
  93. if (filePath != "")
  94. {
  95. string jsonText = prop.serialize();
  96. File.WriteAllText(filePath, jsonText);
  97. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  98. }
  99. }
  100. private void tbtnImport_Click(object sender, EventArgs e)
  101. {
  102. string filePath = ConfMgr.SelectFile("JSON|*.json");
  103. if (filePath != "")
  104. {
  105. string jsonText = File.ReadAllText(filePath);
  106. prop.deserialize(jsonText);
  107. this.propertyGrid1.Refresh();
  108. //this.propertyGrid1.SelectedObject= prop;
  109. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  110. }
  111. }
  112. }
  113. }