You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
11 KiB
C#

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 System.Xml.Serialization;
namespace Rs.Controls
{
public partial class RsTray : UserControl
{
public event Action<TraySlot, MouseEventArgs> SlotClickEvent;
public RsTray()
{
InitializeComponent();
DoubleBuffered = true;
InitSlot();
this.SizeChanged += (s, e) => {
UpdateSlotPos();
};
}
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;
public string HeadText
{
get { return _HeadText; }
set { _HeadText = value; this.label1.Text = value; }
}
private int _RowNum = 26;
/// <summary>
/// 行数
/// </summary>
public int RowNum
{
get { return _RowNum; }
set { _RowNum = value; InitSlot(); Invalidate(); }
}
private int _ColumnNum = 10;
/// <summary>
/// 列数量
/// </summary>
public int ColumnNum
{
get { return _ColumnNum; }
set { _ColumnNum = value; InitSlot(); Invalidate(); }
}
private int _TopSpaceHeight = 20;
/// <summary>
/// 料盘上部留白高度
/// </summary>
public int TopSpaceHeight
{
get { return _TopSpaceHeight; }
set { _TopSpaceHeight = value; }
}
private int _LeftSpaceWidth = 20;
/// <summary>
/// 料盘左边留白宽度
/// </summary>
public int LeftSpaceWidth
{
get { return _LeftSpaceWidth; }
set { _LeftSpaceWidth = value; }
}
private bool _ShowText;
public bool ShowText
{
get { return _ShowText; }
set { _ShowText = value; }
}
private Bitmap drawImage;
private void InitSlot()
{
int index = 0;
traySlots.Clear();
traySlotsDic.Clear();
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.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)
{
traySlotsDic[index].Status = status;
traySlotsDic[index].Refresh(this.panel2.CreateGraphics(),ShowText);
//panel2_Paint(null, null);
}
private void UpdateSlotPos()
{
float slotWidth = (this.panel2.Width - LeftSpaceWidth) / ColumnNum;
float slotHeight = (this.panel2.Height - TopSpaceHeight) / RowNum;
int index = 0;
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 - 5, (int)slotHeight - 3);
}
//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();
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
this.DoubleBuffered = true;
Graphics g = this.panel2.CreateGraphics();
drawImage = new Bitmap(this.panel2.Width, this.panel2.Height);
Graphics g2 = Graphics.FromImage(drawImage);
g2.Clear(BackColor);
//先计算出每一个料仓中穴位占用的宽度和高度
float widthPer = (this.panel2.Width - LeftSpaceWidth) / ColumnNum;
float heightPer = (this.panel2.Height - TopSpaceHeight) / RowNum;
//先画列,再画行
int index = 0;
for (int c = 0; c < ColumnNum; c++)
{
for (int r = 0; r < RowNum; r++)
{
index++;
if (r == 0)
{
//画料盘的列号
g2.DrawString((c + 1).ToString(), new Font("宋体", 7.0F), new SolidBrush(Color.White), new PointF(LeftSpaceWidth + (widthPer * c) + 5, 5));
}
if (c == 0)
{
//画料盘的行号
g2.DrawString((r + 1).ToString(), new Font("宋体", 7.0F), new SolidBrush(Color.White), new PointF(5, TopSpaceHeight + (heightPer * r)));
}
//Rectangle rect = new Rectangle((int)(LeftSpaceWidth + widthPer * c), (int)(TopSpaceHeight + heightPer * r), (int)widthPer - 5, (int)heightPer - 5);
//g2.DrawRectangle(Pens.Red, (LeftSpaceWidth + widthPer * c), (TopSpaceHeight + heightPer * r), (int)widthPer - 5, (int)heightPer - 5);
//TraySlot slot = new TraySlot();
//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);
if (traySlotsDic.ContainsKey(index))
{
traySlotsDic[index].Draw(g2, ShowText);
}
}
}
g2.Flush();
g.DrawImage(drawImage, 0, 0, drawImage.Width, drawImage.Height);
if (drawRegion)
{
//drawRect.Width = 20;
//drawRect.Height = 20;
//g.DrawRectangle(Pens.Green, drawRect);
g.FillRectangle(new SolidBrush(Color.FromArgb(200, Color.Pink)), drawRect);
}
g.Flush();
g.Dispose();
g2.Dispose();
}
bool drawRegion = false;
Rectangle drawRect = new Rectangle();
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && drawRegion == false)
{
if(CanDraw)
{
drawRegion = true;
drawRect.X = e.X;
drawRect.Y = e.Y;
}
}
}
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
if (drawRegion)
{
drawRect.Width = e.X - drawRect.X;
drawRect.Height = e.Y - drawRect.Y;
panel2_Paint(null, null);
}
}
private void panel2_MouseUp(object sender, MouseEventArgs e)
{
drawRegion = false;
}
public TraySlot SelectSlot { get; set; }
private void panel2_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (!drawRect.IsEmpty && drawRect.Contains(e.Location))
{
contextMenuStrip1.Show(panel2, e.Location);
}
else
{
SelectSlot = null;
bool find = false;
foreach (TraySlot slot in this.traySlots)
{
if(slot.Bound.Contains(e.Location))
{
find = true;
SelectSlot = slot;
break;
}
}
if(find)
{
if (SlotClickEvent != null)
SlotClickEvent(SelectSlot,e);
}
}
}
}
}
/// <summary>
/// 料盘槽位
/// </summary>
public class TraySlot
{
private PointF _Position;
public PointF Position
{
get { return _Position; }
set { _Position = value; }
}
private int _Status;
public int Status
{
get { return _Status; }
set { _Status = value; }
}
private int _Index;
public int Index
{
get { return _Index; }
set { _Index = value; }
}
private int _Row;
public int Row
{
get { return _Row; }
set { _Row = value; }
}
private int _Column;
public int Column
{
get { return _Column; }
set { _Column = value; }
}
private RectangleF _Bound;
[XmlIgnore]
public RectangleF Bound
{
get { return _Bound; }
set { _Bound = value; }
}
public void Refresh(Graphics g,bool isShowText)
{
Draw(g, isShowText);
}
public void Draw(Graphics g, bool isShowText)
{
if (_Bound != null)
{
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)
{
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)
{
g.DrawString((Row + 1) + "-" + (Column + 1), new Font("宋体", 9), new SolidBrush(Color.Yellow), _Bound, sf);
}
}
}
}
}