using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Uestc.Auto6.Dso.Core; using Uestc.Auto6.Dso.Core.Decode; using Uestc.Auto6.Dso.ComModel; using Uni_Trend.MSO7000X.Common.Helper; using Uni_Trend.MSO7000X.Common.Structs; using Uni_Trend.MSO7000X.UserControls; using Uni_Trend.MSO7000X.Language; namespace Uestc.Auto6.Dso.Protocol.RS232 { [System.ComponentModel.ToolboxItem(false)] public partial class RS232TriggerSetControl : UserControl, ITriggerSerialView { public RS232TriggerSetControl() { InitializeComponent(); InitView(); this.Load += (_, _) => { Init(); this.BtnValue.Click += (_, _) => Action?.Invoke(); SetControl(Presenter.Conditions); this.SizeChanged += (_, _) => SetControl(Presenter.Conditions); }; } private void InitView() { Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize); } protected new Boolean DesignMode { get { Boolean rtnflag = false; #if DEBUG rtnflag = DesignTimeHelper.InDesignMode(this); #endif return rtnflag; } } private Int32 _RowHeight = 76; private void SetControl(ProtocolRS232.Conditions conditions) { switch (conditions) { case ProtocolRS232.Conditions.SpecialData: DataAutoLayout(); break; case ProtocolRS232.Conditions.PackageEnd: EOPAutoLayout(); break; case ProtocolRS232.Conditions.FrameStart: case ProtocolRS232.Conditions.CheckError: StartAutoLayout(); break; } } private Action Action; private void EditData() { Boolean lastflag = true; if(this.FindForm() is FloatForm form) { lastflag = form.CanClose; form.CanClose = false; } FloatForm floatForm = new FloatForm(); floatForm.BackColor = BackColor; floatForm.ForeColor = ForeColor; floatForm.Title = "设置数据"; floatForm.Width = 380; floatForm.Height = 500 + floatForm.HeadHeight; HexNumberKeyboard numberKeyboard = new HexNumberKeyboard(); Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(numberKeyboard, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize); //numberKeyboard.Controls.Cast().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize)); //numberKeyboard.Dock = DockStyle.Fill; numberKeyboard.Top = floatForm.HeadHeight; numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight; numberKeyboard.Width = floatForm.Width; numberKeyboard.ForeColor = ForeColor; numberKeyboard.BackColor = BackColor; numberKeyboard.MaxValue = 0xFF; numberKeyboard.MinValue = 0x00; numberKeyboard.ValueType = Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard.HexValueType.Hex; numberKeyboard.Value = Presenter.Data; numberKeyboard.OkClick += (sender, args) => { Presenter.Data = (Char)args.Data; this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.Data); floatForm.Close(); }; numberKeyboard.CancelClick += (_, _) => floatForm.Close(); floatForm.FormClosing += (_, _) => { if (this.FindForm() is FloatForm form) form.CanClose = lastflag; }; floatForm.Controls.Add(numberKeyboard); floatForm.ShowDialogByPosition(); } private void EditEOP() { FloatForm floatForm = new FloatForm(); Boolean lastflag = true; if (this.FindForm() is FloatForm form) { lastflag = form.CanClose; form.CanClose = false; } floatForm.BackColor = BackColor; floatForm.ForeColor = ForeColor; floatForm.Title = "设置EOP"; floatForm.Width = 380; floatForm.Height = 500 + floatForm.HeadHeight; HexNumberKeyboard numberKeyboard = new HexNumberKeyboard(); //numberKeyboard.Controls.Cast().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControl(x)); //numberKeyboard.Dock = DockStyle.Fill; numberKeyboard.Top = floatForm.HeadHeight; numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight; numberKeyboard.Width = floatForm.Width; numberKeyboard.ForeColor = ForeColor; numberKeyboard.BackColor = BackColor; numberKeyboard.MaxValue = 0xFF; numberKeyboard.MinValue = 0x00; numberKeyboard.ValueType = Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard.HexValueType.Hex; numberKeyboard.Value = Presenter.EOPChar; numberKeyboard.OkClick += (sender, args) => { Presenter.EOPChar = (Char)args.Data; String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar); if(String.IsNullOrEmpty(charstring)) this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar); else this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring); floatForm.Close(); }; numberKeyboard.CancelClick += (_, _) => floatForm.Close(); floatForm.FormClosing += (_, _) => { if (this.FindForm() is FloatForm form) form.CanClose = lastflag; }; floatForm.Controls.Add(numberKeyboard); floatForm.ShowDialogByPosition(); } private void Init() { SetControl(Presenter.Conditions); this.CbxCompare.DataSource = Enum.GetValues().Select(x => new KeyValuePair(x.GetDescription(), x)).ToList(); this.CbxCompare.DisplayMember = "Key"; this.CbxCompare.ValueMember = "Value"; this.CbxCompare.SelectedValueChanged += (sender, args) => { if (ParentForm == null) return; Presenter.Compare = (PulseCondition)this.CbxCompare.SelectedValue; }; this.CbxCompare.SelectedValue = Presenter.Compare; } public RS232TrigSerialPrsnt Presenter { get => (RS232TrigSerialPrsnt)(ParentForm as ITriggerView).Presenter; set => (ParentForm as ITriggerView).Presenter = value; } ITriggerPrsnt IView.Presenter { get => Presenter; set => Presenter = (RS232TrigSerialPrsnt)value; } private void EOPAutoLayout() { this.LblValue.Text = "结束标志"; String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar); if (String.IsNullOrEmpty(charstring)) this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar); else this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring); this.LblValue.Visible = true; this.BtnValue.Visible = true; this.LblCompare.Visible = false; this.CbxCompare.Visible = false; SetControlPosition(BtnValue, LblValue, 0, 0); this.Height = BtnValue.Bottom; Visible = true; Action = () => EditEOP(); } private void StartAutoLayout() { this.LblValue.Visible = false; this.BtnValue.Visible = false; this.CbxCompare.Visible = false; this.LblCompare.Visible = false; Height = 0; Visible = false; Action = null; } private void DataAutoLayout() { this.LblValue.Text = "数据(Hex)"; this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Byte)Presenter.Data); this.BtnValue.Visible = true; this.LblValue.Visible = true; this.CbxCompare.Visible = true; this.LblCompare.Visible = true; SetControlPosition(CbxCompare,LblCompare, 0, 0); SetControlPosition(BtnValue,LblValue, 0, 1); this.Height = BtnValue.Bottom; Visible = true; Action = () => EditData(); } private void SetControlPosition(TControl control, UestcLabel label, Int32 row = 0, Int32 col = 0) where TControl : Control { label.Height = label.Font.Height; Int32 tempheight = control.Height + label.Height; label.Left = Width / 2 * col + 10; label.Top = row * _RowHeight + (_RowHeight - tempheight) / 2; control.Left = label.Left; control.Top = label.Bottom + 6; } public void UpdateView(Object presenter, String propertyName) { if(String.IsNullOrEmpty(propertyName)) { UpdateView(); return; } switch(propertyName) { case nameof(RS232TrigSerialPrsnt.Compare): if(Presenter.Conditions == ProtocolRS232.Conditions.SpecialData) CbxCompare.SelectedValue = Presenter.Compare; break; case nameof(RS232TrigSerialPrsnt.Data): if(Presenter.Conditions == ProtocolRS232.Conditions.SpecialData) BtnValue.Text = "0x" + String.Format("{0:X2}", (Byte)Presenter.Data); break; //case nameof(RS232TrigSerialPrsnt.DataIndex): // if (Presenter.RS232Conditions == ProtocolRS232.Conditions.SpecialData) CbxDataIndex.SelectedValue = Presenter.DataIndex; // break; case nameof(RS232TrigSerialPrsnt.EOPChar): if(Presenter.Conditions == ProtocolRS232.Conditions.CheckError) { String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar); if (String.IsNullOrEmpty(charstring)) this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar); else this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring); } break; case nameof(RS232TrigSerialPrsnt.Conditions): SetControl(Presenter.Conditions); break; default: break; } } private void UpdateView() { } } }