using DllEapEntity; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DllXqManager { public class TEntityControl : Label { public int CurrTime = 0; public LayoutDetail CurrEntity = null; public MacStatus CurrStatus = null; private Color CurrColor = Color.Transparent;//Color.White; Color fontcolor = Color.White; Image img = null; //private Label lable1 = new Label(); public TEntityControl(LayoutDetail row) { CurrEntity = row; InitializeComponent(); this.BackColor = Color.Transparent; } public void SetBackColor(Color color) { //CurrColor = color; //CurrColor = Color.Transparent; } public void SetFontColor(Color color) { //CurrColor = color; fontcolor = color; } public void SetImage(Image image) { //CurrColor = color; img = image; } private void InitControl() { } private Point CurrPoint = new Point(0, 0); private void InitializeComponent() { this.SuspendLayout(); // // TEntityControl // //this.Text = "666666666"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.TEntityControl_Paint); this.ResumeLayout(false); } private void TEntityControl_MouseDown(object sender, MouseEventArgs e) { CurrPoint.X = e.X; CurrPoint.Y = e.Y; (sender as Control).BringToFront(); } private void TEntityControl_MouseMove(object sender, MouseEventArgs e) { (sender as Control).Left = (sender as Control).Left + e.X - CurrPoint.X; (sender as Control).Top = (sender as Control).Top + e.Y - CurrPoint.Y; } private void TEntityControl_Enter(object sender, EventArgs e) { } protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); //自己的代码 //this.Refresh();//不能加此语句,否则一直闪烁 Graphics g = e.Graphics; //string fcode = CurrRow["实体代码"].ToString(); //g.DrawString(fcode, new Font("arail", 10), new SolidBrush(Color.Black), 5, 5); //g.DrawString(CurrRow["实体名称"].ToString(), new Font("arail", 10), new SolidBrush(Color.Black), 5, 30); //g.Dispose(); string fcode = CurrEntity.FCode;// CurrRow["实体代码"].ToString(); string fname = CurrEntity.FName;// CurrRow["实体名称"].ToString(); float fontsize = float.Parse(CurrEntity.FontSize.ToString()); Font tempfont = new Font("arail", fontsize, FontStyle.Bold); int entitytype = CurrEntity.EntityTypeID; Pen p = new Pen(fontcolor); int w = this.Width; int h = this.Height; SizeF sizeF = g.MeasureString(fcode, tempfont);//获取字符串长和宽,像素单位 float x = 0; //Color fontcolor = Color.Black; if (entitytype == 2 || entitytype == 3) { FillRoundRectangle(g, new Rectangle(0, 0, w - 1, h - 1), CurrColor, 5); //fname = CurrEntity.TEntityFTypeFName+" "+ CurrEntity.FCode+ " " + CurrEntity.FName + " " + CurrEntity.Remark; fname = CurrEntity.FName; //g.DrawRectangle(p, 0, 0, w - 1, h - 1); int rowh = h;//行高 float rowl = (rowh - tempfont.Height) / 2;//子开始的纵坐标 sizeF = g.MeasureString(fname, tempfont); x = (w - sizeF.Width) / 2; g.DrawString(fname, tempfont, new SolidBrush(Color.White), x, rowl); this.SetBackColor(Color.Violet); } else if (entitytype == 3) { //g.DrawRectangle(p, 0, 0, w - 1, h - 1); //g.DrawLine(p, 0, h / 3, w, h / 3); //g.DrawLine(p, 0, h * 2 / 3, w, h * 2 / 3); //int rowh = h / 3;//行高 //float rowl = (rowh - tempfont.Height) / 2;//子开始的纵坐标 //fname = CurrEntity.TEntityFTypeFName + " " + CurrEntity.FCode + " " + CurrEntity.FName; //sizeF = g.MeasureString(fname, tempfont); //x = (w - sizeF.Width) / 2; //g.DrawString(fname, tempfont, new SolidBrush(Color.Black), 5, rowl); //fname = CurrEntity.FName; //sizeF = g.MeasureString(fname, tempfont); //x = (w - sizeF.Width) / 2; //g.DrawString(fname, tempfont, new SolidBrush(Color.Black), 5, h / 3 + rowl); //fname = CurrEntity.Remark; //sizeF = g.MeasureString(fname, tempfont); //x = (w - sizeF.Width) / 2; //g.DrawString(fname, tempfont, new SolidBrush(Color.Black), 5, h / 3 * 2 + rowl); } else { DateTime stime = DateTime.Now; if (CurrStatus != null) { stime = CurrStatus.STime;// CurrEntity.RecTime; } TimeSpan ts1 = new TimeSpan(stime.Ticks); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); int tlen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds; string showtime = tlen.ToString() + "S"; if (tlen > 60) { decimal minute = ts.Hours * 60 + ts.Minutes; showtime = minute.ToString() + "M"; } if (this.BackColor == Color.Gray) { fontcolor = Color.White; p.Color = Color.White; } //g.DrawRectangle(p, 0, 0, w - 1, h - 1); FillRoundRectangle(g, new Rectangle(0, 0, w - 1, h - 1), CurrColor, 5); //g.DrawLine(p, 0, h , w, h );//三分之一处画条横线 //g.DrawLine(p, 0, 0, w, 0);//三分之一处画条横线 //g.DrawLine(p, w - w / 3, 0, w - w / 3, h / 3);//第一行得三分之二处画竖线 float y = 0; //显示机种信息(状态的备注) if (CurrStatus != null) { string product = GetProduct(CurrStatus.Remark); sizeF = g.MeasureString(product, tempfont); x = (w - sizeF.Width) / 3;//第二行显示机种信息 y = h / 2 + (h / 2 - sizeF.Height) / 2; g.DrawString(product, tempfont, new SolidBrush(fontcolor), x, (int)(1.2*y)); } else { sizeF = g.MeasureString(fname, tempfont); x = (w - sizeF.Width) / 3;//第二行显示机种信息 y = h / 2 + (h / 2 - sizeF.Height) / 2; g.DrawString(fname, tempfont, new SolidBrush(fontcolor), x, (int)(1.2 * y)); } sizeF = g.MeasureString(fcode, tempfont); x = (w - w / 3 - sizeF.Width) / 2;//第一行的第二格填写机台编号 y = (h / 2 - sizeF.Height) / 2; g.DrawString(fcode, tempfont, new SolidBrush(fontcolor), ((w - sizeF.Width) / 3), y/4); if (CurrStatus != null && CurrStatus.StatusID > 1) { Font lenfont = new Font("arail", fontsize, FontStyle.Bold); sizeF = g.MeasureString(showtime, lenfont); x = (w / 3 - sizeF.Width) / 2 + (w - w / 3); y = (h / 2 - sizeF.Height) / 2; g.DrawString(showtime, lenfont, new SolidBrush(fontcolor), x, y+10); } x = (w / 3 - sizeF.Width) / 2 + (w - w / 3); if (img != null) //g.DrawImage(img, w/8, (int)(1.3*y),82,(h-2*y)); g.DrawImage(img, w/10, (int)(1.8*y),x, (h -4 * y)); //g.DrawImage(img, w/8, (int)(1.3*y),40,(h-2*y)); //g.DrawLine(p, 0, h / 3, w, h / 3);//三分之一处画条横线 //g.DrawLine(p, w/2, 0, w/2, h / 3);//中间到三分之一高画条竖线 //g.DrawLine(p, 0, h * 2 / 3, w, h * 2 / 3); ////g.DrawLine(p, w / 3, h * 2 / 3, w / 3, h); ////g.DrawLine(p, w * 2 / 3, h * 2 / 3, w * 2 / 3, h); //int rowh = h / 3;//行高 //float rowl = (rowh - tempfont.Height) / 2;//子开始的纵坐标 //sizeF = g.MeasureString(fcode, tempfont); //x = (w/2 - sizeF.Width) / 2+w/2;//第一行的第二格填写机台编号 //g.DrawString(fcode, tempfont, new SolidBrush(fontcolor), x, rowl); ////第一行的第二格填写机台厂商 //fcode = CurrEntity.SupplierFCode; //sizeF = g.MeasureString(fcode, tempfont); //x = (w / 2 - sizeF.Width) / 2; //g.DrawString(fcode, tempfont, new SolidBrush(fontcolor), x, rowl); //sizeF = g.MeasureString(fname, tempfont); //x = (w - sizeF.Width) / 2; //g.DrawString(fname, tempfont, new SolidBrush(fontcolor), x, h / 3 + rowl); ////g.DrawString(CurrTime.ToString(), tempfont, new SolidBrush(Color.Black), 5, h / 3 * 2 + rowl); //fname = "";// CurrEntity["报警代码"].ToString(); //if (CurrStatus != null) // fname = CurrStatus.AlarmCode; //fname =string.IsNullOrEmpty(fname)?"":"ERR:"+fname; //sizeF = g.MeasureString(fname, tempfont); //x = (w - sizeF.Width) / 2; //g.DrawString(fname, tempfont, new SolidBrush(fontcolor), x, h / 3 * 2 + rowl); } //lable1.BringToFront(); //lable1.SendToBack(); this.Text = "测试"; } private string GetProduct(string remark) { if (string.IsNullOrEmpty(remark)) return ""; string[] strs = remark.Split('-'); return strs[0].Split(' ')[0]; } private void TEntityControl_Paint(object sender, PaintEventArgs e) { } /// /// C# GDI+ 绘制圆角实心矩形 /// /// Graphics 对象 /// 要填充的矩形 /// 填充背景色 /// 圆角半径 public void FillRoundRectangle(Graphics g, Rectangle rectangle, Color backColor, int r) { rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1); Brush b = new SolidBrush(backColor); g.FillPath(b, GetRoundRectangle(rectangle, r)); } /// /// 根据普通矩形得到圆角矩形的路径 /// /// 原始矩形 /// 半径 /// 图形路径 private GraphicsPath GetRoundRectangle(Rectangle rectangle, int r) { int l = 2 * r; // 把圆角矩形分成八段直线、弧的组合,依次加到路径中 GraphicsPath gp = new GraphicsPath(); gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y)); gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F); gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r)); gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F); gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom)); gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F); gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r)); gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F); return gp; } } }