From 7708ff1c9348c890a81fb31aaf81ba79fc0de516 Mon Sep 17 00:00:00 2001 From: lhiven <236881222@qq.com> Date: Tue, 13 Jun 2023 07:58:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=99=E7=9B=98=E6=A8=AA?= =?UTF-8?q?=E5=90=91=E6=8E=92=E5=BA=8F=E6=88=96=E8=80=85=E7=AB=96=E5=90=91?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Rs.Controls/RsTray.cs | 195 ++++++++++++++++++++++++++++++++---------- 1 file changed, 150 insertions(+), 45 deletions(-) diff --git a/Rs.Controls/RsTray.cs b/Rs.Controls/RsTray.cs index 8d045ed..737af92 100644 --- a/Rs.Controls/RsTray.cs +++ b/Rs.Controls/RsTray.cs @@ -11,19 +11,43 @@ using System.Xml.Serialization; namespace Rs.Controls { + public enum ESortDir + { + Horizontal, + Vertical + } + + public enum ESlotStatus + { + Have, + NotHave, + Ok, + Ng + } + public partial class RsTray : UserControl { + public ESortDir SortDir { get; set; } = ESortDir.Horizontal; public event Action SlotClickEvent; public RsTray() { InitializeComponent(); DoubleBuffered = true; - InitSlot(); + InitSlot(InitSlotStatus); this.SizeChanged += (s, e) => { UpdateSlotPos(); }; } + private ESlotStatus _InitSlotStatus = ESlotStatus.Have; + + public ESlotStatus InitSlotStatus + { + get { return _InitSlotStatus; } + set { _InitSlotStatus = value; InitSlot(InitSlotStatus); Invalidate(); } + } + + public bool CanDraw { get; set; } = true; private List traySlots = new List(); @@ -45,7 +69,7 @@ namespace Rs.Controls public int RowNum { get { return _RowNum; } - set { _RowNum = value; InitSlot(); Invalidate(); } + set { _RowNum = value; InitSlot(InitSlotStatus); Invalidate(); } } private int _ColumnNum = 10; @@ -56,7 +80,7 @@ namespace Rs.Controls public int ColumnNum { get { return _ColumnNum; } - set { _ColumnNum = value; InitSlot(); Invalidate(); } + set { _ColumnNum = value; InitSlot(InitSlotStatus); Invalidate(); } } private int _TopSpaceHeight = 20; @@ -80,6 +104,23 @@ namespace Rs.Controls set { _LeftSpaceWidth = value; } } + private int _RowSpace = 3; + + public int RowSpace + { + get { return _RowSpace; } + set { _RowSpace = value; } + } + + private int _ColSpace = 5; + + public int ColSpace + { + get { return _ColSpace; } + set { _ColSpace = value; } + } + + private bool _ShowText; public bool ShowText @@ -91,34 +132,78 @@ namespace Rs.Controls private Bitmap drawImage; - private void InitSlot() + private void InitSlot(ESlotStatus status) { int index = 0; traySlots.Clear(); traySlotsDic.Clear(); - for (int c = 0; c < ColumnNum; c++) + if(SortDir== ESortDir.Vertical) + { + for (int c = 0; c < ColumnNum; c++) + { + for (int r = 0; r < RowNum; r++) + { + index++; + TraySlot slot = new TraySlot(); + slot.Index = index; + slot.Row = r; + slot.Column = c; + slot.SlotStatus = status; + //slot.Bound = new RectangleF((LeftSpaceWidth + widthPer * c), (TopSpaceHeight + heightPer * r), (int)widthPer - 5, (int)heightPer - 5); + //slot.Draw(g2); + traySlots.Add(slot); + traySlotsDic.Add(index, slot); + } + } + } + else { for (int r = 0; r < RowNum; r++) { - index++; - TraySlot slot = new TraySlot(); - slot.Index = index; - slot.Row = r; - slot.Column = c; - //slot.Bound = new RectangleF((LeftSpaceWidth + widthPer * c), (TopSpaceHeight + heightPer * r), (int)widthPer - 5, (int)heightPer - 5); - //slot.Draw(g2); - traySlots.Add(slot); - traySlotsDic.Add(index, slot); + for (int c = 0; c < ColumnNum; c++) + { + index++; + TraySlot slot = new TraySlot(); + slot.Index = index; + slot.Row = r; + slot.Column = c; + slot.SlotStatus = status; + //slot.Bound = new RectangleF((LeftSpaceWidth + widthPer * c), (TopSpaceHeight + heightPer * r), (int)widthPer - 5, (int)heightPer - 5); + //slot.Draw(g2); + traySlots.Add(slot); + traySlotsDic.Add(index, slot); + } } } + UpdateSlotPos(); } - public void ChangeStatus(int index,int status) + public void Fill() { - traySlotsDic[index].Status = status; - traySlotsDic[index].Refresh(this.panel2.CreateGraphics(),ShowText); + foreach (TraySlot item in traySlots) + { + ChangeStatus(item.Index, ESlotStatus.Have); + } + } + + public void Clear() + { + foreach (TraySlot item in traySlots) + { + ChangeStatus(item.Index, ESlotStatus.NotHave); + } + } + + public void ChangeStatus(int index,ESlotStatus status) + { + if(traySlotsDic.ContainsKey(index)) + { + traySlotsDic[index].SlotStatus = status; + traySlotsDic[index].Refresh(this.panel2.CreateGraphics(), ShowText); + } + //panel2_Paint(null, null); } @@ -127,25 +212,35 @@ namespace Rs.Controls float slotWidth = (this.panel2.Width - LeftSpaceWidth) / ColumnNum; float slotHeight = (this.panel2.Height - TopSpaceHeight) / RowNum; int index = 0; - for (int c = 0; c < ColumnNum; c++) + if(SortDir== ESortDir.Vertical) + { + for (int c = 0; c < ColumnNum; c++) + { + for (int r = 0; r < RowNum; r++) + { + index++; + if (traySlotsDic.ContainsKey(index)) + { + traySlotsDic[index].Bound = new RectangleF((LeftSpaceWidth + slotWidth * c), (TopSpaceHeight + slotHeight * r), (int)slotWidth - ColSpace, (int)slotHeight - RowSpace); + } + } + } + } + else { for (int r = 0; r < RowNum; r++) { - index++; - if (traySlotsDic.ContainsKey(index)) + for (int c = 0; c < ColumnNum; c++) { - traySlotsDic[index].Bound = new RectangleF((LeftSpaceWidth + slotWidth * c), (TopSpaceHeight + slotHeight * r), (int)slotWidth - 5, (int)slotHeight - 3); + index++; + if (traySlotsDic.ContainsKey(index)) + { + traySlotsDic[index].Bound = new RectangleF((LeftSpaceWidth + slotWidth * c), (TopSpaceHeight + slotHeight * r), (int)slotWidth - ColSpace, (int)slotHeight - RowSpace); + } } - //TraySlot slot = new TraySlot(); - //slot.Index = index; - //slot.Row = r; - //slot.Column = c; - //slot.Bound = new RectangleF((LeftSpaceWidth + widthPer * c), (TopSpaceHeight + heightPer * r), (int)widthPer - 5, (int)heightPer - 5); - //slot.Draw(g2); - //traySlots.Add(slot); - //traySlotsDic.Add(index, slot); } } + panel2.Refresh(); } @@ -271,6 +366,7 @@ namespace Rs.Controls /// public class TraySlot { + public ESlotStatus SlotStatus { get; set; } = ESlotStatus.Have; private PointF _Position; @@ -280,14 +376,6 @@ namespace Rs.Controls set { _Position = value; } } - private int _Status; - - public int Status - { - get { return _Status; } - set { _Status = value; } - } - private int _Index; public int Index @@ -331,16 +419,33 @@ namespace Rs.Controls StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; - - if(Status==1) - { - g.FillRectangle(new SolidBrush(Color.Red), Bound.X, Bound.Y, Bound.Width - 1, Bound.Height - 1); - } - else if(Status==0) + switch (SlotStatus) { - g.FillRectangle(new SolidBrush(Color.FromArgb(11, 16, 36)), Bound.X, Bound.Y, Bound.Width-1, Bound.Height-1); - // g.FillRectangle(new SolidBrush(Color.Red), Bound.X, Bound.Y, Bound.Width , Bound.Height ); + case ESlotStatus.Have: + g.FillRectangle(new SolidBrush(Color.Red), Bound.X, Bound.Y, Bound.Width - 1, Bound.Height - 1); + break; + case ESlotStatus.NotHave: + g.FillRectangle(new SolidBrush(Color.FromArgb(11, 16, 36)), Bound.X, Bound.Y, Bound.Width - 1, Bound.Height - 1); + break; + case ESlotStatus.Ok: + g.FillRectangle(new SolidBrush(Color.Green), Bound.X, Bound.Y, Bound.Width - 1, Bound.Height - 1); + break; + case ESlotStatus.Ng: + g.FillRectangle(new SolidBrush(Color.Gray), Bound.X, Bound.Y, Bound.Width - 1, Bound.Height - 1); + break; + default: + break; } + + //if(Status==1) + //{ + // g.FillRectangle(new SolidBrush(Color.Red), Bound.X, Bound.Y, Bound.Width - 1, Bound.Height - 1); + //} + //else if(Status==0) + //{ + // g.FillRectangle(new SolidBrush(Color.FromArgb(11, 16, 36)), Bound.X, Bound.Y, Bound.Width-1, Bound.Height-1); + // // g.FillRectangle(new SolidBrush(Color.Red), Bound.X, Bound.Y, Bound.Width , Bound.Height ); + //} g.DrawRectangle(Pens.White, Bound.X, Bound.Y, Bound.Width, Bound.Height); if (isShowText) {