From c2c4bb6ff92c85b5f9da1422cade8af355405404 Mon Sep 17 00:00:00 2001 From: wells.liu <wells.liu@broconcentric.com> Date: 星期二, 14 七月 2020 18:10:46 +0800 Subject: [PATCH] 测量结果 界面设计开发 --- src/Bro.M071.Process/UI/M071_MainForm.cs | 384 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 356 insertions(+), 28 deletions(-) diff --git a/src/Bro.M071.Process/UI/M071_MainForm.cs b/src/Bro.M071.Process/UI/M071_MainForm.cs index fbc7bd2..6aa59b8 100644 --- a/src/Bro.M071.Process/UI/M071_MainForm.cs +++ b/src/Bro.M071.Process/UI/M071_MainForm.cs @@ -1,4 +1,5 @@ -锘縰sing Bro.Common.Helper; +锘縰sing Bro.Common.Base; +using Bro.Common.Helper; using Bro.UI.Model.Winform; using System; using System.Collections.Generic; @@ -9,14 +10,16 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using static Bro.Common.Helper.EnumHelper; namespace Bro.M071.Process.UI { - [MenuNode("M071_MainForm", "閿洏妫�娴嬩富鐣岄潰", 3, "M071Node", true)] + [MenuNode("M071_MainForm", "杩愯鐣岄潰", 1, "M071Node", true)] public partial class M071_MainForm : MenuFrmBase { Canvas cvImage = new Canvas(); M071Config Config => Process?.IConfig as M071Config; + M071Process Process_M071 => Process as M071Process; public M071_MainForm() { @@ -26,9 +29,18 @@ tsmiShowStatusBar.Checked = cvImage.IsShowStatusBar = false; tsmiShowToolBar.Checked = cvImage.IsShowToolBar = false; cvImage.Dock = DockStyle.Fill; - this.Controls.Add(cvImage); + plImage.Controls.Add(cvImage); tscEditLocation.Visible = tsmiShowEditor.Checked = false; + + this.Load += async (s, e) => + { + await Task.Delay(300); + + cvImage.SetScreenSize(); + cvImage.OnElementChangedHandle -= CvImage_OnElementChangedHandle; + cvImage.OnElementChangedHandle += CvImage_OnElementChangedHandle; + }; } public override void OnProcessUpdated() @@ -43,40 +55,72 @@ try { - Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath); - cvImage.LoadImage(image); + this.Invoke(new Action(() => + { + try + { + Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath); + cvImage.LoadImage(image); + cvImage.SetScreenSize(); + } + catch (Exception ex) + { + LogAsync(new LogMsg(DateTime.Now, "杞藉叆鑳屾櫙鍥惧紓甯�", ex.Message)); + return; + } + + cvImage.Elements.Clear(); + lvMeasures.Items.Clear(); + + txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked; + + Config.MeasurementUnitCollection.ForEach(u => + { + if (!u.IsEnabled) + return; + + var ele = new KeyIndicator(u.Id, u.DisplayLocation); + cvImage.Elements.Add(ele); + + ListViewItem item = new ListViewItem(u.GetDisplayText()); + item.Tag = u.Id; + lvMeasures.Items.Add(item); + }); + })); } catch (Exception ex) { - LogAsync(new LogMsg(DateTime.Now, "杞藉叆鑳屾櫙鍥惧紓甯�", ex.Message)); - return; } - cvImage.Elements.Clear(); - lvMeasures.Items.Clear(); - Config.MeasurementUnitCollection.ForEach(u => - { - if (!u.IsEnabled) - return; + Process_M071.OnClearBarcode -= M071_MainForm_OnClearBarcode; + Process_M071.OnClearBarcode += M071_MainForm_OnClearBarcode; + Process_M071.OnUpdateResult -= M071_MainForm_UpdateResult; + Process_M071.OnUpdateResult += M071_MainForm_UpdateResult; + Process_M071.OnUpdateCT -= M071_MainForm_UpdateCT; + Process_M071.OnUpdateCT += M071_MainForm_UpdateCT; - var ele = new KeyIndicator(u.Id, u.DisplayLocation); - cvImage.Elements.Add(ele); + Process_M071.OnMeasureStart -= M071_MainForm_MeasureStart; + Process_M071.OnMeasureStart += M071_MainForm_MeasureStart; - ListViewItem item = new ListViewItem(u.GetDisplayText()); - item.Tag = u.Id; - lvMeasures.Items.Add(item); - }); + Process_M071.OnMachineStateChanged -= M071_MainForm_MachineStateChanged; + Process_M071.OnMachineStateChanged += M071_MainForm_MachineStateChanged; + + Process_M071.OnFullResetDone -= Process_M071_FullResetDone; + Process_M071.OnFullResetDone += Process_M071_FullResetDone; + + Config.PropertyChanged -= Config_PropertyChanged; + Config.PropertyChanged += Config_PropertyChanged; + + Process_M071.OnElementUpdated -= Process_M071_OnElementUpdated; + Process_M071.OnElementUpdated += Process_M071_OnElementUpdated; } - - private void lvMeasures_SelectedIndexChanged(object sender, EventArgs e) + + private void Config_PropertyChanged(object sender, PropertyChangedEventArgs e) { - if (lvMeasures.SelectedItems.Count <= 0) - return; - - var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString()); - propGridKeyIndicator.SelectedObject = ele; + txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked; } + #region 鍥剧墖鍖哄彸閿彍鍗� private void tsmiShowToolBar_CheckedChanged(object sender, EventArgs e) { cvImage.IsShowToolBar = tsmiShowToolBar.Checked; @@ -90,6 +134,53 @@ private void tsmiShowEditor_CheckedChanged(object sender, EventArgs e) { tscEditLocation.Visible = tsmiShowEditor.Checked; + tscEditLocation.BringToFront(); + } + #endregion + + #region 鏍囩缂栬緫鍖� + private async void CvImage_OnElementChangedHandle(Common.Interface.IShapeElement ele) + { + this.Invoke(new Action(() => + { + if (ele is KeyIndicator indicator) + { + if (indicator.State == ElementState.Selected) + { + foreach (ListViewItem item in lvMeasures.Items) + { + if (item.Tag.ToString() == indicator.ID) + { + item.Selected = true; + + lvMeasures.EnsureVisible(item.Index); + break; + } + } + } + } + })); + + await Task.Delay(100); + } + + private void lvMeasures_SelectedIndexChanged(object sender, EventArgs e) + { + if (lvMeasures.SelectedItems.Count <= 0) + return; + + foreach (ListViewItem item in lvMeasures.Items) + { + item.BackColor = SystemColors.Control; + } + + foreach (ListViewItem item in lvMeasures.SelectedItems) + { + item.BackColor = Color.Orange; + } + + var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString()); + propGridKeyIndicator.SelectedObject = ele; } private void btnCancelEdit_Click(object sender, EventArgs e) @@ -109,7 +200,7 @@ lvMeasures.Items.Add(item); }); - //tscEditLocation.Visible = false; + MessageBox.Show("鍙栨秷鏍囩淇敼"); } private void btnConfirmEdit_Click(object sender, EventArgs e) @@ -123,7 +214,244 @@ } }); - //tscEditLocation.Visible = false; + MessageBox.Show("鏍囩淇敼瀹屾垚"); } + + private void lvMeasures_DoubleClick(object sender, EventArgs e) + { + if (lvMeasures.SelectedItems.Count <= 0) + return; + + cvImage.Elements.ToList().ForEach(u => u.State = ElementState.Normal); + var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString()); + if (ele != null) + { + ele.State = ElementState.Selected; + //cvImage.Invalidate(); + } + } + #endregion + + #region 鏍囩缁撴灉鏄剧ず + private async void Process_M071_OnElementUpdated(Common.Interface.IShapeElement obj) + { + if (obj is KeyIndicator keyIndicator) + { + this.Invoke(new Action(() => + { + var ele = cvImage.Elements.FirstOrDefault(u => u.ID == keyIndicator.ID); + + (ele as KeyIndicator).Text = keyIndicator.Text; + (ele as KeyIndicator).ResultState = keyIndicator.ResultState; + + this.Invalidate(); + })); + } + + await Task.Delay(100); + } + #endregion + + #region 涓婃柟鐘舵�佸尯 + + #region 鏉$爜 + string _barcode = ""; + private void M071_MainForm_KeyUp(object sender, KeyEventArgs e) + { + string keyStr = e.KeyCode.ToString(); + if (keyStr.Length == 1) + { + _barcode += keyStr.ToUpper(); + } + + if (e.KeyValue == 13) + { + txtBarcode.Text = _barcode; + _barcode = ""; + } + + if (e.KeyValue == 27) + { + cvImage.Elements.ToList().ForEach(ele => ele.State = ElementState.Normal); + } + } + + private void txtBarcode_TextChanged(object sender, EventArgs e) + { + Process_M071.BarCode = txtBarcode.Text.Trim(); + } + + private void M071_MainForm_OnClearBarcode() + { + txtBarcode.BeginInvoke(new Action(() => txtBarcode.Clear())); + } + #endregion + + #region 鏈哄彴鐘舵�� + private async void M071_MainForm_MachineStateChanged(MachineState state) + { + if (lblMachineState.IsHandleCreated) + { + lblMachineState.BeginInvoke(new Action(() => + { + switch (state) + { + case MachineState.Alarm: + btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Red; + btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.White; + lblMachineState.Text = "璀︽姤"; + + btnStartMeasure.Text = "寮�濮嬫祴閲�"; + btnStartMeasure.Enabled = false; + break; + case MachineState.Pause: + btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Yellow; + btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.Black; + lblMachineState.Text = "鏆傚仠"; + + btnStartMeasure.Text = "缁х画娴嬮噺"; + btnStartMeasure.Enabled = true; + break; + case MachineState.Ready: + btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Lime; + btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.Black; + lblMachineState.Text = "灏辩华"; + + btnStartMeasure.Text = "寮�濮嬫祴閲�"; + btnStartMeasure.Enabled = true; + break; + case MachineState.Running: + btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Transparent; + btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.DarkGreen; + lblMachineState.Text = "杩愯"; + + btnStartMeasure.Text = "鏆傚仠娴嬮噺"; + btnStartMeasure.Enabled = true; + break; + case MachineState.Unknown: + btnStartMeasure.BackColor = lblMachineState.BackColor = SystemColors.Control; + btnStartMeasure.ForeColor = lblMachineState.ForeColor = SystemColors.ControlText; + lblMachineState.Text = "鏈煡"; + + btnStartMeasure.Enabled = false; + break; + } + })); + } + + await Task.Delay(100); + } + #endregion + + #region 浜у搧缁撴灉 + private async void M071_MainForm_UpdateCT(float ct) + { + lblCT.BeginInvoke(new Action(() => + { + lblCT.Text = ct.ToString("f2") + " 绉�"; + })); + await Task.Delay(100); + } + + private async void M071_MainForm_UpdateResult(DateTime arg1, int result) + { + this.BeginInvoke(new Action(() => + { + if (result == 1) + { + lblProductionState.BackColor = Color.Green; + lblProductionState.ForeColor = Color.White; + lblProductionState.Text = "OK"; + } + else + { + lblProductionState.BackColor = Color.Red; + lblProductionState.ForeColor = Color.White; + lblProductionState.Text = "NG"; + } + + //btnStartMeasure.Text = "寮�濮嬫祴閲�"; + })); + await Task.Delay(100); + } + + private async void M071_MainForm_MeasureStart() + { + this.BeginInvoke(new Action(() => + { + lblProductionState.BackColor = SystemColors.Control; + lblProductionState.ForeColor = SystemColors.ControlText; + lblProductionState.Text = "娴嬭瘯涓�"; + + lblCT.Text = ""; + + cvImage.Elements.ToList().ForEach(e => + { + if (e is KeyIndicator indicator) + { + indicator.Text = ""; + indicator.ResultState = null; + } + }); + })); + await Task.Delay(100); + } + #endregion + + #endregion + + private async void btnStartMeasure_Click(object sender, EventArgs e) + { + if (btnStartMeasure.Text == "寮�濮嬫祴閲�") + { + await Task.Run(() => + { + try + { + Process_M071.StartJob(null, null, null); + } + catch (Exception ex) + { + LogAsync(DateTime.Now, "娴佺▼寮傚父", ex.Message); + Process_M071.ExceptionRaisedInMonitor(ex); + } + }); + } + else if (btnStartMeasure.Text == "缁х画娴嬮噺") + { + Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 10 } }, null, null); + } + else + { + Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 11 } }, null, null); + } + } + + #region 澶嶄綅鎿嶄綔 + private async void Process_M071_FullResetDone() + { + btnStartMeasure.BeginInvoke(new Action(() => + { + btnStartMeasure.Enabled = true; + btnStartMeasure.Text = "寮�濮嬫祴閲�"; + })); + await Task.Delay(100); + } + + private void btnReset_MouseDown(object sender, MouseEventArgs e) + { + if (Process_M071.MachineState != MachineState.Running) + { + Process_M071.Reset(null, null, null); + } + + Process_M071.ResetTimer.Change(1000 * 5, -1); + } + + private void btnReset_MouseUp(object sender, MouseEventArgs e) + { + Process_M071.ResetTimer.Change(-1, -1); + } + #endregion } } -- Gitblit v1.8.0