TEntityControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 
  2. using DllEapEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace DllXqManager
  13. {
  14. public class TEntityControl : Label
  15. {
  16. public int CurrTime = 0;
  17. public LayoutDetail CurrEntity = null;
  18. public MacStatus CurrStatus = null;
  19. private Color CurrColor = Color.Transparent;//Color.White;
  20. Color fontcolor = Color.White;
  21. Image img = null;
  22. //private Label lable1 = new Label();
  23. public TEntityControl(LayoutDetail row)
  24. {
  25. CurrEntity = row;
  26. InitializeComponent();
  27. this.BackColor = Color.Transparent;
  28. }
  29. public void SetBackColor(Color color)
  30. {
  31. //CurrColor = color;
  32. //CurrColor = Color.Transparent;
  33. }
  34. public void SetFontColor(Color color)
  35. {
  36. //CurrColor = color;
  37. fontcolor = color;
  38. }
  39. public void SetImage(Image image)
  40. {
  41. //CurrColor = color;
  42. img = image;
  43. }
  44. private void InitControl()
  45. {
  46. }
  47. private Point CurrPoint = new Point(0, 0);
  48. private void InitializeComponent()
  49. {
  50. this.SuspendLayout();
  51. //
  52. // TEntityControl
  53. //
  54. //this.Text = "666666666";
  55. this.Paint += new System.Windows.Forms.PaintEventHandler(this.TEntityControl_Paint);
  56. this.ResumeLayout(false);
  57. }
  58. private void TEntityControl_MouseDown(object sender, MouseEventArgs e)
  59. {
  60. CurrPoint.X = e.X;
  61. CurrPoint.Y = e.Y;
  62. (sender as Control).BringToFront();
  63. }
  64. private void TEntityControl_MouseMove(object sender, MouseEventArgs e)
  65. {
  66. (sender as Control).Left = (sender as Control).Left + e.X - CurrPoint.X;
  67. (sender as Control).Top = (sender as Control).Top + e.Y - CurrPoint.Y;
  68. }
  69. private void TEntityControl_Enter(object sender, EventArgs e)
  70. {
  71. }
  72. protected override void OnPaint(PaintEventArgs e)
  73. {
  74. //base.OnPaint(e); //自己的代码
  75. //this.Refresh();//不能加此语句,否则一直闪烁
  76. Graphics g = e.Graphics;
  77. //string fcode = CurrRow["实体代码"].ToString();
  78. //g.DrawString(fcode, new Font("arail", 10), new SolidBrush(Color.Black), 5, 5);
  79. //g.DrawString(CurrRow["实体名称"].ToString(), new Font("arail", 10), new SolidBrush(Color.Black), 5, 30);
  80. //g.Dispose();
  81. string fcode = CurrEntity.FCode;// CurrRow["实体代码"].ToString();
  82. string fname = CurrEntity.FName;// CurrRow["实体名称"].ToString();
  83. float fontsize = float.Parse(CurrEntity.FontSize.ToString());
  84. Font tempfont = new Font("arail", fontsize, FontStyle.Bold);
  85. int entitytype = CurrEntity.EntityTypeID;
  86. Pen p = new Pen(fontcolor);
  87. int w = this.Width;
  88. int h = this.Height;
  89. SizeF sizeF = g.MeasureString(fcode, tempfont);//获取字符串长和宽,像素单位
  90. float x = 0;
  91. //Color fontcolor = Color.Black;
  92. if (entitytype == 2 || entitytype == 3)
  93. {
  94. FillRoundRectangle(g, new Rectangle(0, 0, w - 1, h - 1), CurrColor, 5);
  95. //fname = CurrEntity.TEntityFTypeFName+" "+ CurrEntity.FCode+ " " + CurrEntity.FName + " " + CurrEntity.Remark;
  96. fname = CurrEntity.FName;
  97. //g.DrawRectangle(p, 0, 0, w - 1, h - 1);
  98. int rowh = h;//行高
  99. float rowl = (rowh - tempfont.Height) / 2;//子开始的纵坐标
  100. sizeF = g.MeasureString(fname, tempfont);
  101. x = (w - sizeF.Width) / 2;
  102. g.DrawString(fname, tempfont, new SolidBrush(Color.White), x, rowl);
  103. this.SetBackColor(Color.Violet);
  104. }
  105. else if (entitytype == 3)
  106. {
  107. //g.DrawRectangle(p, 0, 0, w - 1, h - 1);
  108. //g.DrawLine(p, 0, h / 3, w, h / 3);
  109. //g.DrawLine(p, 0, h * 2 / 3, w, h * 2 / 3);
  110. //int rowh = h / 3;//行高
  111. //float rowl = (rowh - tempfont.Height) / 2;//子开始的纵坐标
  112. //fname = CurrEntity.TEntityFTypeFName + " " + CurrEntity.FCode + " " + CurrEntity.FName;
  113. //sizeF = g.MeasureString(fname, tempfont);
  114. //x = (w - sizeF.Width) / 2;
  115. //g.DrawString(fname, tempfont, new SolidBrush(Color.Black), 5, rowl);
  116. //fname = CurrEntity.FName;
  117. //sizeF = g.MeasureString(fname, tempfont);
  118. //x = (w - sizeF.Width) / 2;
  119. //g.DrawString(fname, tempfont, new SolidBrush(Color.Black), 5, h / 3 + rowl);
  120. //fname = CurrEntity.Remark;
  121. //sizeF = g.MeasureString(fname, tempfont);
  122. //x = (w - sizeF.Width) / 2;
  123. //g.DrawString(fname, tempfont, new SolidBrush(Color.Black), 5, h / 3 * 2 + rowl);
  124. }
  125. else
  126. {
  127. DateTime stime = DateTime.Now;
  128. if (CurrStatus != null)
  129. {
  130. stime = CurrStatus.STime;// CurrEntity.RecTime;
  131. }
  132. TimeSpan ts1 = new TimeSpan(stime.Ticks);
  133. TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
  134. TimeSpan ts = ts1.Subtract(ts2).Duration();
  135. int tlen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
  136. string showtime = tlen.ToString() + "S";
  137. if (tlen > 60)
  138. {
  139. decimal minute = ts.Hours * 60 + ts.Minutes;
  140. showtime = minute.ToString() + "M";
  141. }
  142. if (this.BackColor == Color.Gray)
  143. {
  144. fontcolor = Color.White;
  145. p.Color = Color.White;
  146. }
  147. //g.DrawRectangle(p, 0, 0, w - 1, h - 1);
  148. FillRoundRectangle(g, new Rectangle(0, 0, w - 1, h - 1), CurrColor, 5);
  149. //g.DrawLine(p, 0, h , w, h );//三分之一处画条横线
  150. //g.DrawLine(p, 0, 0, w, 0);//三分之一处画条横线
  151. //g.DrawLine(p, w - w / 3, 0, w - w / 3, h / 3);//第一行得三分之二处画竖线
  152. float y = 0;
  153. //显示机种信息(状态的备注)
  154. if (CurrStatus != null)
  155. {
  156. string product = GetProduct(CurrStatus.Remark);
  157. sizeF = g.MeasureString(product, tempfont);
  158. x = (w - sizeF.Width) / 3;//第二行显示机种信息
  159. y = h / 2 + (h / 2 - sizeF.Height) / 2;
  160. g.DrawString(product, tempfont, new SolidBrush(fontcolor), x, (int)(1.2*y));
  161. }
  162. else
  163. {
  164. sizeF = g.MeasureString(fname, tempfont);
  165. x = (w - sizeF.Width) / 3;//第二行显示机种信息
  166. y = h / 2 + (h / 2 - sizeF.Height) / 2;
  167. g.DrawString(fname, tempfont, new SolidBrush(fontcolor), x, (int)(1.2 * y));
  168. }
  169. sizeF = g.MeasureString(fcode, tempfont);
  170. x = (w - w / 3 - sizeF.Width) / 2;//第一行的第二格填写机台编号
  171. y = (h / 2 - sizeF.Height) / 2;
  172. g.DrawString(fcode, tempfont, new SolidBrush(fontcolor), ((w - sizeF.Width) / 3), y/4);
  173. if (CurrStatus != null && CurrStatus.StatusID > 1)
  174. {
  175. Font lenfont = new Font("arail", fontsize, FontStyle.Bold);
  176. sizeF = g.MeasureString(showtime, lenfont);
  177. x = (w / 3 - sizeF.Width) / 2 + (w - w / 3);
  178. y = (h / 2 - sizeF.Height) / 2;
  179. g.DrawString(showtime, lenfont, new SolidBrush(fontcolor), x, y+10);
  180. }
  181. x = (w / 3 - sizeF.Width) / 2 + (w - w / 3);
  182. if (img != null)
  183. //g.DrawImage(img, w/8, (int)(1.3*y),82,(h-2*y));
  184. g.DrawImage(img, w/10, (int)(1.8*y),x, (h -4 * y));
  185. //g.DrawImage(img, w/8, (int)(1.3*y),40,(h-2*y));
  186. //g.DrawLine(p, 0, h / 3, w, h / 3);//三分之一处画条横线
  187. //g.DrawLine(p, w/2, 0, w/2, h / 3);//中间到三分之一高画条竖线
  188. //g.DrawLine(p, 0, h * 2 / 3, w, h * 2 / 3);
  189. ////g.DrawLine(p, w / 3, h * 2 / 3, w / 3, h);
  190. ////g.DrawLine(p, w * 2 / 3, h * 2 / 3, w * 2 / 3, h);
  191. //int rowh = h / 3;//行高
  192. //float rowl = (rowh - tempfont.Height) / 2;//子开始的纵坐标
  193. //sizeF = g.MeasureString(fcode, tempfont);
  194. //x = (w/2 - sizeF.Width) / 2+w/2;//第一行的第二格填写机台编号
  195. //g.DrawString(fcode, tempfont, new SolidBrush(fontcolor), x, rowl);
  196. ////第一行的第二格填写机台厂商
  197. //fcode = CurrEntity.SupplierFCode;
  198. //sizeF = g.MeasureString(fcode, tempfont);
  199. //x = (w / 2 - sizeF.Width) / 2;
  200. //g.DrawString(fcode, tempfont, new SolidBrush(fontcolor), x, rowl);
  201. //sizeF = g.MeasureString(fname, tempfont);
  202. //x = (w - sizeF.Width) / 2;
  203. //g.DrawString(fname, tempfont, new SolidBrush(fontcolor), x, h / 3 + rowl);
  204. ////g.DrawString(CurrTime.ToString(), tempfont, new SolidBrush(Color.Black), 5, h / 3 * 2 + rowl);
  205. //fname = "";// CurrEntity["报警代码"].ToString();
  206. //if (CurrStatus != null)
  207. // fname = CurrStatus.AlarmCode;
  208. //fname =string.IsNullOrEmpty(fname)?"":"ERR:"+fname;
  209. //sizeF = g.MeasureString(fname, tempfont);
  210. //x = (w - sizeF.Width) / 2;
  211. //g.DrawString(fname, tempfont, new SolidBrush(fontcolor), x, h / 3 * 2 + rowl);
  212. }
  213. //lable1.BringToFront();
  214. //lable1.SendToBack();
  215. this.Text = "测试";
  216. }
  217. private string GetProduct(string remark)
  218. {
  219. if (string.IsNullOrEmpty(remark))
  220. return "";
  221. string[] strs = remark.Split('-');
  222. return strs[0].Split(' ')[0];
  223. }
  224. private void TEntityControl_Paint(object sender, PaintEventArgs e)
  225. {
  226. }
  227. /// <summary>
  228. /// C# GDI+ 绘制圆角实心矩形
  229. /// </summary>
  230. /// <param name="g">Graphics 对象</param>
  231. /// <param name="rectangle">要填充的矩形</param>
  232. /// <param name="backColor">填充背景色</param>
  233. /// <param name="r">圆角半径</param>
  234. public void FillRoundRectangle(Graphics g, Rectangle rectangle, Color backColor, int r)
  235. {
  236. rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1);
  237. Brush b = new SolidBrush(backColor);
  238. g.FillPath(b, GetRoundRectangle(rectangle, r));
  239. }
  240. /// <summary>
  241. /// 根据普通矩形得到圆角矩形的路径
  242. /// </summary>
  243. /// <param name="rectangle">原始矩形</param>
  244. /// <param name="r">半径</param>
  245. /// <returns>图形路径</returns>
  246. private GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
  247. {
  248. int l = 2 * r;
  249. // 把圆角矩形分成八段直线、弧的组合,依次加到路径中
  250. GraphicsPath gp = new GraphicsPath();
  251. gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
  252. gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);
  253. gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
  254. gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);
  255. gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
  256. gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);
  257. gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
  258. gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
  259. return gp;
  260. }
  261. }
  262. }