CANDataPage.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Uestc.Auto6.Dso.Core;
  12. using Uestc.Auto6.Dso.Core.Decode;
  13. using Uestc.Auto6.Dso.ComModel;
  14. using Uni_Trend.MSO7000X.Common.Helper;
  15. using Uni_Trend.MSO7000X.UserControls;
  16. using Uni_Trend.MSO7000X.UserControls.Style;
  17. using Uni_Trend.MSO7000X.Language;
  18. namespace Uestc.Auto6.Dso.Protocol.CAN
  19. {
  20. public partial class CANDataPage : UserControl,ITriggerSerialView
  21. {
  22. public CANDataPage()
  23. {
  24. InitializeComponent();
  25. Controls.Cast<Control>().ToList().ForEach(x => StyleManager.Instance.RegisterControl(x));
  26. StyleManager.Instance.RegisterControl(this);
  27. }
  28. protected override void OnLoad(EventArgs e)
  29. {
  30. base.OnLoad(e);
  31. InitView();
  32. }
  33. private void CANDataPage_Load(object sender, EventArgs e)
  34. {
  35. }
  36. private void InitView()
  37. {
  38. this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<RowColAttribute>() != null && x.GetValue(this) is Control).Select(x =>
  39. {
  40. RowColAttribute attribute = x.GetCustomAttribute<RowColAttribute>();
  41. Control control = x.GetValue(this) as Control;
  42. return (attribute, control);
  43. }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x =>
  44. {
  45. Control lable = x.First(x => x is UestcLabel);
  46. Control control = x.First(x => !(x is UestcLabel));
  47. lable.Left = Width / _ColnumCount * x.Key.Colnum;
  48. lable.Width = control.Width;
  49. lable.Height = lable.Font.Height;
  50. lable.Top = 10 + _RowHeight * x.Key.Row;
  51. control.Left = lable.Left;
  52. control.Top = lable.Bottom + 2;
  53. lable.Width = 120;
  54. control.Width = 120;
  55. });
  56. SetDataByteCount();
  57. SetData();
  58. SetDataByteIndex();
  59. InitRelation();
  60. }
  61. private void InitRelation()
  62. {
  63. this.CbxRelation.DataSource = Presenter.DataRelation.GetEnumList();
  64. this.CbxRelation.ValueMember = "Value";
  65. this.CbxRelation.DisplayMember = "Key";
  66. this.CbxRelation.SelectedValueChanged += (_, _) =>
  67. {
  68. Presenter.DataRelation = (ComModel.ProtocolCAN.DataRelation)(ComModel.PulseCondition)this.CbxRelation.SelectedValue;
  69. };
  70. this.CbxRelation.SelectedValue = Presenter.DataRelation;
  71. }
  72. #region 属性
  73. private FloatForm _FloatForm;
  74. private Int32 _RowHeight = 66;
  75. private Int32 _ColnumCount = 2;
  76. public CANTrigSerialPrsnt Presenter { get; set; }
  77. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  78. {
  79. get => Presenter;
  80. set => Presenter = (CANTrigSerialPrsnt)value;
  81. }
  82. protected new Boolean DesignMode
  83. {
  84. get
  85. {
  86. Boolean rtnflag = false;
  87. #if DEBUG
  88. rtnflag = DesignTimeHelper.InDesignMode(this);
  89. #endif
  90. return rtnflag;
  91. }
  92. }
  93. #endregion 属性
  94. public void UpdateView(string propertyName)
  95. {
  96. if (string.IsNullOrEmpty(propertyName))
  97. {
  98. UpdateView();
  99. return;
  100. }
  101. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
  102. {
  103. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  104. return attribute != null && attribute.PropertyName == propertyName;
  105. }).FirstOrDefault()?.Invoke(this, null);
  106. }
  107. private void UpdateView()
  108. {
  109. this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  110. this.Refresh();
  111. }
  112. #region UpdateFunction
  113. [UpdateProperty(nameof(CANTrigSerialPrsnt.ByteCount))]
  114. private void SetDataByteCount()
  115. {
  116. this.BtnByteCount.Text = Presenter.ByteCount.ToString();
  117. this.BtnByteCount.Click += (_, _) =>
  118. {
  119. bool lastflag = true;
  120. if (this.FindForm() is FloatForm form)
  121. {
  122. lastflag = form.CanClose;
  123. form.CanClose = false;
  124. }
  125. if (_FloatForm != null && !_FloatForm.IsDisposed)
  126. _FloatForm?.Close();
  127. _FloatForm = new FloatForm();
  128. _FloatForm.BackColor = BackColor;
  129. _FloatForm.ForeColor = ForeColor;
  130. _FloatForm.Title = "设置字节号";
  131. _FloatForm.Width = 380;
  132. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  133. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  134. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  135. numberKeyboard.Top = _FloatForm.HeadHeight;
  136. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  137. numberKeyboard.Width = _FloatForm.Width;
  138. numberKeyboard.ForeColor = ForeColor;
  139. numberKeyboard.BackColor = BackColor;
  140. numberKeyboard.MaxValue = Presenter.ByteCount - 1;
  141. numberKeyboard.MinValue = 0;
  142. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec;
  143. numberKeyboard.Value = Presenter.ByteCount;
  144. numberKeyboard.OkClick += (sender, args) =>
  145. {
  146. Presenter.ByteCount = (int)args.Data;
  147. _FloatForm.Close();
  148. };
  149. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  150. _FloatForm.Controls.Add(numberKeyboard);
  151. _FloatForm.FormClosing += (_, _) =>
  152. {
  153. if (this.FindForm() is FloatForm form)
  154. form.CanClose = lastflag;
  155. };
  156. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  157. {
  158. Current = _FloatForm,
  159. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  160. });
  161. };
  162. }
  163. [UpdateProperty(nameof(CANTrigSerialPrsnt.Data))]
  164. private void SetData()
  165. {
  166. this.BtnData.Text = "0x" + Convert.ToString(Presenter.Data, 16).ToUpper().PadLeft(2, '0');
  167. this.BtnData.Click += (_, _) =>
  168. {
  169. bool lastflag = true;
  170. if (this.FindForm() is FloatForm form)
  171. {
  172. lastflag = form.CanClose;
  173. form.CanClose = false;
  174. }
  175. if (_FloatForm != null && !_FloatForm.IsDisposed)
  176. _FloatForm?.Close();
  177. _FloatForm = new FloatForm();
  178. _FloatForm.BackColor = BackColor;
  179. _FloatForm.ForeColor = ForeColor;
  180. _FloatForm.Title = "设置数据";
  181. _FloatForm.Width = 380;
  182. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  183. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  184. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  185. numberKeyboard.Top = _FloatForm.HeadHeight;
  186. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  187. numberKeyboard.Width = _FloatForm.Width;
  188. numberKeyboard.ForeColor = ForeColor;
  189. numberKeyboard.BackColor = BackColor;
  190. numberKeyboard.MaxValue = byte.MaxValue;
  191. numberKeyboard.MinValue = 0;
  192. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Hex;
  193. numberKeyboard.Value = Presenter.Data;
  194. numberKeyboard.OkClick += (sender, args) =>
  195. {
  196. Presenter.Data = (byte)args.Data;
  197. _FloatForm.Close();
  198. };
  199. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  200. _FloatForm.Controls.Add(numberKeyboard);
  201. _FloatForm.FormClosing += (_, _) =>
  202. {
  203. if (this.FindForm() is FloatForm form)
  204. form.CanClose = lastflag;
  205. };
  206. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  207. {
  208. Current = _FloatForm,
  209. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  210. });
  211. };
  212. }
  213. [UpdateProperty(nameof(CANTrigSerialPrsnt.ByteIndex))]
  214. private void SetDataByteIndex()
  215. {
  216. this.BtnByteIndex.Text = Presenter.ByteIndex.ToString();
  217. this.BtnByteIndex.Click += (_, _) =>
  218. {
  219. bool lastflag = true;
  220. if (this.FindForm() is FloatForm form)
  221. {
  222. lastflag = form.CanClose;
  223. form.CanClose = false;
  224. }
  225. if (_FloatForm != null && !_FloatForm.IsDisposed)
  226. _FloatForm?.Close();
  227. _FloatForm = new FloatForm();
  228. _FloatForm.BackColor = BackColor;
  229. _FloatForm.ForeColor = ForeColor;
  230. _FloatForm.Title = "设置字节号";
  231. _FloatForm.Width = 380;
  232. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  233. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  234. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  235. numberKeyboard.Top = _FloatForm.HeadHeight;
  236. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  237. numberKeyboard.Width = _FloatForm.Width;
  238. numberKeyboard.ForeColor = ForeColor;
  239. numberKeyboard.BackColor = BackColor;
  240. numberKeyboard.MaxValue = Presenter.ByteCount - 1;
  241. numberKeyboard.MinValue = 0;
  242. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec;
  243. numberKeyboard.Value = Presenter.ByteIndex;
  244. numberKeyboard.OkClick += (sender, args) =>
  245. {
  246. Presenter.ByteIndex = (int)args.Data;
  247. _FloatForm.Close();
  248. };
  249. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  250. _FloatForm.Controls.Add(numberKeyboard);
  251. _FloatForm.FormClosing += (_, _) =>
  252. {
  253. if (this.FindForm() is FloatForm form)
  254. form.CanClose = lastflag;
  255. };
  256. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  257. {
  258. Current = _FloatForm,
  259. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  260. });
  261. };
  262. }
  263. [UpdateProperty(nameof(CANTrigSerialPrsnt.DataRelation))]
  264. private void SetRelation()
  265. {
  266. this.CbxRelation.SelectedValue = Presenter.DataRelation;
  267. }
  268. #endregion UpdateFunction
  269. }
  270. }