优化tray控件,增减序号从左到右、从右到左

master
lhiven 2 years ago
parent c6357341bd
commit c788db5362

@ -95,7 +95,6 @@
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(137, 36);
this.lblStatus.TabIndex = 2;
this.lblStatus.Text = "IDLE";
this.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
@ -108,7 +107,6 @@
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(137, 36);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// contextMenuStrip1
@ -139,7 +137,6 @@
this.ToolStripMenuItem.Name = "禁用ToolStripMenuItem";
this.ToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.ToolStripMenuItem.Text = "禁用";
this.ToolStripMenuItem.Visible = false;
this.ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
//
// RsTray

@ -15,10 +15,11 @@ using System.Xml.Serialization;
namespace Rs.Controls
{
public enum ESortDir
{
Horizontal,
Vertical
LeftToRight,
RightToLeft
}
public enum ESlotStatus
@ -32,14 +33,26 @@ namespace Rs.Controls
public partial class RsTray : UserControl
{
private int CurrentSlot { get; set; } = 1;
public string ItemName { get; set; }
public ESortDir SortDir { get; set; } = ESortDir.Horizontal;
public event Action<TraySlot, MouseEventArgs> SlotClickEvent;
public string ItemName { get; set; }
private ESortDir sortDir = ESortDir.LeftToRight;
public ESortDir SortDir
{
get { return sortDir; }
set { sortDir = value;
UpdateSlotPos();
}
}
public event Action<TraySlot, MouseEventArgs> SlotClickEvent;
#region 是否显示状态
private bool mShowStatus = true;
/// <summary>
/// 是否显示状态
/// </summary>
public bool ShowStatus
{
get { return mShowStatus; }
@ -47,6 +60,7 @@ namespace Rs.Controls
lblStatus.Visible = mShowStatus;
}
}
#endregion
private string mStatus;
@ -72,15 +86,7 @@ namespace Rs.Controls
public void ResetTray()
{
CurrentSlot = 1;
}
public bool IsLastSlot()
{
if(CurrentSlot == RowNum*ColumnNum) {
return true;
}
return false;
}
private ESlotStatus _InitSlotStatus = ESlotStatus.Have;
@ -101,20 +107,7 @@ namespace Rs.Controls
return null;
}
public List<TraySlot> GetSlots(ESlotStatus status)
{
List<TraySlot> slots = new List<TraySlot>();
foreach (KeyValuePair<int, TraySlot> kv in traySlotsDic)
{
if (kv.Value.SlotStatus == status)
slots.Add(kv.Value);
}
return slots;
}
public bool CanDraw { get; set; } = true;
private List<TraySlot> traySlots = new List<TraySlot>();
private Dictionary<int, TraySlot> traySlotsDic = new Dictionary<int, TraySlot>();
private string _HeadText;
@ -245,59 +238,36 @@ namespace Rs.Controls
private void InitSlot(ESlotStatus status)
{
int index = 0;
traySlots.Clear();
traySlotsDic.Clear();
if (SortDir == ESortDir.Vertical)
for (int r = 0; r < RowNum; r++)
{
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;
traySlots.Add(slot);
traySlotsDic.Add(index, slot);
}
}
}
else
{
for (int r = 0; r < RowNum; r++)
{
for (int c = 0; c < ColumnNum; c++)
{
index++;
TraySlot slot = new TraySlot();
slot.Index = index;
slot.Row = r;
slot.Column = c;
slot.SlotStatus = status;
traySlots.Add(slot);
traySlotsDic.Add(index, slot);
}
index++;
TraySlot slot = new TraySlot();
slot.Index = index;
slot.Row = r;
slot.Column = c;
slot.SlotStatus = status;
traySlotsDic.Add(index, slot);
}
}
UpdateSlotPos();
}
public void Fill()
{
foreach (TraySlot item in traySlots)
foreach (KeyValuePair<int, TraySlot> kv in traySlotsDic)
{
ChangeStatus(item.Index, ESlotStatus.Have);
ChangeStatus(kv.Value.Index, ESlotStatus.Have);
}
}
public void Clear()
{
foreach (TraySlot item in traySlots)
foreach (KeyValuePair<int, TraySlot> kv in traySlotsDic)
{
ChangeStatus(item.Index, ESlotStatus.NotHave);
ChangeStatus(kv.Value.Index, ESlotStatus.NotHave);
}
}
@ -315,11 +285,11 @@ namespace Rs.Controls
float slotWidth = (this.panel2.Width - LeftSpaceWidth) / ColumnNum;
float slotHeight = (this.panel2.Height - TopSpaceHeight) / RowNum;
int index = 0;
if (SortDir == ESortDir.Vertical)
if (SortDir == ESortDir.LeftToRight)
{
for (int c = 0; c < ColumnNum; c++)
for (int r = 0; r < RowNum; r++)
{
for (int r = 0; r < RowNum; r++)
for (int c = 0; c < ColumnNum; c++)
{
index++;
if (traySlotsDic.ContainsKey(index))
@ -329,7 +299,7 @@ namespace Rs.Controls
}
}
}
else
else if (SortDir == ESortDir.RightToLeft)
{
for (int r = 0; r < RowNum; r++)
{
@ -338,7 +308,7 @@ namespace Rs.Controls
index++;
if (traySlotsDic.ContainsKey(index))
{
traySlotsDic[index].Bound = new RectangleF((LeftSpaceWidth + slotWidth * c), (TopSpaceHeight + slotHeight * r), (int)slotWidth - ColSpace, (int)slotHeight - RowSpace);
traySlotsDic[index].Bound = new RectangleF((LeftSpaceWidth + slotWidth * (ColumnNum-1-c)), (TopSpaceHeight + slotHeight * r), (int)slotWidth - ColSpace, (int)slotHeight - RowSpace);
}
}
}
@ -367,7 +337,14 @@ namespace Rs.Controls
if (r == 0)
{
//画料盘的列号
g2.DrawString((c + 1).ToString(), new Font("宋体", 7.0F), new SolidBrush(Color.White), new PointF(LeftSpaceWidth + (widthPer * c) + 5, 5));
if (SortDir == ESortDir.LeftToRight)
{
g2.DrawString((c+ 1).ToString(), new Font("宋体", 7.0F), new SolidBrush(Color.White), new PointF(LeftSpaceWidth + (widthPer * c) + 5, 5));
}
else if(SortDir== ESortDir.RightToLeft)
{
g2.DrawString((ColumnNum - c).ToString(), new Font("宋体", 7.0F), new SolidBrush(Color.White), new PointF(LeftSpaceWidth + (widthPer * c) + 5, 5));
}
}
if (c == 0)
{
@ -435,12 +412,12 @@ namespace Rs.Controls
{
SelectSlot = null;
bool find = false;
foreach (TraySlot slot in this.traySlots)
foreach (KeyValuePair<int,TraySlot> kvSlot in traySlotsDic)
{
if (slot.Bound.Contains(e.Location))
if (kvSlot.Value.Bound.Contains(e.Location))
{
find = true;
SelectSlot = slot;
SelectSlot = kvSlot.Value;
break;
}
}
@ -460,11 +437,11 @@ namespace Rs.Controls
private void ChangeSlotStatus(ESlotStatus status)
{
foreach (TraySlot slot in this.traySlots)
foreach (KeyValuePair<int, TraySlot> kvSlot in traySlotsDic)
{
if (drawRect.IntersectsWith(slot.Bound))
if (drawRect.IntersectsWith(kvSlot.Value.Bound))
{
slot.SlotStatus = status;
kvSlot.Value.SlotStatus = status;
}
}
UpdateSlotPos();
@ -488,14 +465,6 @@ namespace Rs.Controls
{
public ESlotStatus SlotStatus { get; set; } = ESlotStatus.Have;
private PointF _Position;
public PointF Position
{
get { return _Position; }
set { _Position = value; }
}
private int _Index;
public int Index
@ -562,7 +531,7 @@ namespace Rs.Controls
g.DrawRectangle(Pens.White, Bound.X, Bound.Y, Bound.Width, Bound.Height);
if (isShowText)
{
g.DrawString((Row + 1) + "-" + (Column + 1), new Font("宋体", 8), new SolidBrush(Color.Yellow), _Bound, sf);
g.DrawString(Index.ToString(), new Font("宋体", 8), new SolidBrush(Color.Yellow), _Bound, sf);
}
}
}

Loading…
Cancel
Save