UCGroupItems.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace winTest
  10. {
  11. public partial class UCGroupItems : Control
  12. {
  13. public Color ProssBarColor = Color.Green;
  14. public UCGroupItems()
  15. {
  16. this.Padding = new Padding(0, 24, 0, 0);
  17. this.Dock = DockStyle.Top;
  18. this.Width = 900;
  19. this.Height = 50;
  20. this.Paint += UCGroupItems_Paint;
  21. this.MouseMove += UCGroupItems_MouseMove;
  22. this.MouseDown += UCGroupItems_MouseDown;
  23. this.MouseUp += UCGroupItems_MouseUp;
  24. this.MouseLeave += UCGroupItems_MouseLeave;
  25. this._GroupMark ="Group_" +GenerateId().ToString();
  26. this.BackColor = Color.White;
  27. }
  28. void UCGroupItems_MouseLeave(object sender, EventArgs e)
  29. {
  30. foreach (UCItems ucItem in this.UCItemsList)
  31. {
  32. if (ucItem.LbItemTextBrush != Brushes.Black)
  33. {
  34. ucItem.LbItemTextBrush = Brushes.Black;
  35. this.Invalidate(ucItem.GetLbItemTextRectangle());
  36. break;
  37. }
  38. }
  39. }
  40. public UCGroupItems(string groupName, string groupMark=null)
  41. : this()
  42. {
  43. this._GroupNameText = groupName;
  44. if (groupMark != null && groupMark.Trim().Length>0)
  45. {
  46. this._GroupMark = groupMark;
  47. }
  48. }
  49. /// <summary>
  50. /// 全局ID
  51. /// </summary>
  52. /// <returns></returns>
  53. public static long GenerateId()
  54. {
  55. byte[] buffer = Guid.NewGuid().ToByteArray();
  56. return BitConverter.ToInt64(buffer, 0);
  57. }
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. /// <param name="e"></param>
  62. public delegate void EventGroupItemClickHandler(EventGroupItemClick e);
  63. /// <summary>
  64. /// 申明委托
  65. /// </summary>
  66. /// <param name="e"></param>
  67. /// <returns></returns>
  68. public delegate void EventItemClickHandler(EventItemClick e);
  69. /// <summary>
  70. /// UCItemOperating事件
  71. /// </summary>
  72. public event EventItemClickHandler UCItemOperatingClick;
  73. /// <summary>
  74. /// UCItemDelete事件
  75. /// </summary>
  76. public event EventItemClickHandler UCItemDeleteClick;
  77. ///<summary>
  78. /// GroupUCItemOperating事件
  79. /// </summary>
  80. public event EventGroupItemClickHandler GroupUCItemOperatingClick;
  81. /// <summary>
  82. /// GroupUCItemDelete事件
  83. /// </summary>
  84. public event EventGroupItemClickHandler GroupUCItemDeleteClick;
  85. void UCGroupItems_MouseUp(object sender, MouseEventArgs e)
  86. {
  87. if (e.X > this.Width - 95)
  88. {
  89. if (this.ContainsOperating(e.X, e.Y))
  90. {
  91. this.GroupLinkOperatingBrush = Brushes.DodgerBlue;
  92. this.Invalidate(getOperatingRectangle());
  93. if (this.GroupUCItemOperatingClick != null)
  94. {
  95. this.GroupUCItemOperatingClick(new EventGroupItemClick(this));
  96. }
  97. }
  98. else if (this.ContainsDelete(e.X, e.Y))
  99. {
  100. this.GroupLinkDeleteBrush = Brushes.DodgerBlue;
  101. this.Invalidate(getDeleteRectangle());
  102. if (this.GroupUCItemDeleteClick != null)
  103. {
  104. this.GroupUCItemDeleteClick(new EventGroupItemClick(this));
  105. }
  106. }
  107. foreach (UCItems ucItem in this.UCItemsList)
  108. {
  109. if (ucItem.ContainsOperating(e.X, e.Y))
  110. {
  111. ucItem.LinkOperatingBrush = Brushes.RoyalBlue;
  112. this.Invalidate(ucItem.GetOperatingRectangle());
  113. if (this.UCItemOperatingClick != null)
  114. {
  115. this.UCItemOperatingClick(new EventItemClick(ucItem));
  116. }
  117. break;
  118. }
  119. else if (ucItem.ContainsDelete(e.X, e.Y))
  120. {
  121. ucItem.LinkDeleteBrush = Brushes.RoyalBlue;
  122. this.Invalidate(ucItem.GetDeleteRectangle());
  123. if (this.UCItemDeleteClick != null)
  124. {
  125. this.UCItemDeleteClick(new EventItemClick(ucItem));
  126. }
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. void UCGroupItems_MouseDown(object sender, MouseEventArgs e)
  133. {
  134. if (e.X > this.Width - 95)
  135. {
  136. if (this.ContainsOperating(e.X, e.Y))
  137. {
  138. this.GroupLinkOperatingBrush = Brushes.Red;
  139. this.Invalidate(getOperatingRectangle());
  140. }
  141. else if (this.ContainsDelete(e.X, e.Y))
  142. {
  143. this.GroupLinkDeleteBrush = Brushes.Red;
  144. this.Invalidate(getDeleteRectangle());
  145. }
  146. foreach (UCItems ucItem in this.UCItemsList)
  147. {
  148. if (ucItem.ContainsOperating(e.X, e.Y))
  149. {
  150. ucItem.LinkOperatingBrush = Brushes.Red;
  151. this.Invalidate(ucItem.GetOperatingRectangle());
  152. break;
  153. }
  154. else if (ucItem.ContainsDelete(e.X, e.Y))
  155. {
  156. ucItem.LinkDeleteBrush = Brushes.Red;
  157. this.Invalidate(ucItem.GetDeleteRectangle());
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. void UCGroupItems_MouseMove(object sender, MouseEventArgs e)
  164. {
  165. if (this.ContainsOperating(e.X, e.Y)
  166. || this.ContainsDelete(e.X, e.Y))
  167. {
  168. this.Cursor = Cursors.Hand;
  169. }
  170. else
  171. {
  172. if (this.UCItemsList.Count > 0)
  173. {
  174. bool isContains = false;
  175. foreach (UCItems ucItem in this.UCItemsList)
  176. {
  177. if (ucItem.ContainsOperating(e.X, e.Y)
  178. || ucItem.ContainsDelete(e.X, e.Y))
  179. {
  180. isContains = true;
  181. break;
  182. }
  183. else if (ucItem.ContainsLbItemText(e.X, e.Y))
  184. {
  185. if (ucItem.LbItemTextBrush != Brushes.Red)
  186. {
  187. ucItem.LbItemTextBrush = Brushes.Red;
  188. this.Invalidate(ucItem.GetLbItemTextRectangle());
  189. }
  190. }
  191. else
  192. {
  193. if (ucItem.LinkOperatingBrush != Brushes.RoyalBlue)
  194. {
  195. ucItem.LinkOperatingBrush = Brushes.RoyalBlue;
  196. this.Invalidate(ucItem.GetOperatingRectangle());
  197. }
  198. if (ucItem.LinkDeleteBrush != Brushes.RoyalBlue)
  199. {
  200. ucItem.LinkDeleteBrush = Brushes.RoyalBlue;
  201. this.Invalidate(ucItem.GetDeleteRectangle());
  202. }
  203. if (ucItem.LbItemTextBrush != Brushes.Black)
  204. {
  205. ucItem.LbItemTextBrush = Brushes.Black;
  206. this.Invalidate(ucItem.GetLbItemTextRectangle());
  207. }
  208. }
  209. }
  210. if (isContains)
  211. {
  212. this.Cursor = Cursors.Hand;
  213. }
  214. else {
  215. this.Cursor = Cursors.Default;
  216. }
  217. }
  218. else
  219. {
  220. this.Cursor = Cursors.Default;
  221. }
  222. }
  223. }
  224. Rectangle getOperatingRectangle()
  225. {
  226. return new Rectangle(this.Width - 95, 0, 35, 24);
  227. }
  228. Rectangle getDeleteRectangle()
  229. {
  230. return new Rectangle(this.Width - 52, 0, 35, 24);
  231. }
  232. bool ContainsOperating(int x, int y)
  233. {
  234. return getOperatingRectangle().Contains(x, y);
  235. }
  236. bool ContainsDelete(int x, int y)
  237. {
  238. return getDeleteRectangle().Contains(x, y);
  239. }
  240. void UCGroupItems_Paint(object sender, PaintEventArgs e)
  241. {
  242. #region 画组名称
  243. Pen p = new Pen(Brushes.WhiteSmoke);
  244. p.Width = 24;
  245. e.Graphics.DrawLine(p, new Point(0, 12), new Point(this.Width, 12));
  246. e.Graphics.DrawString(this.GroupNameText, this.GroupNameFont, Brushes.Black, new PointF(0, 2));
  247. p.Dispose();
  248. #endregion
  249. this.DrawOperatingButton(e.Graphics, this.GroupLinkOperatingBrush);
  250. this.DrawDeleteButton(e.Graphics, this.GroupLinkDeleteBrush);
  251. for (int i = 0; i < UCItemsList.Count; i++)
  252. {
  253. UCItems ucItem = UCItemsList[i];
  254. ucItem.SetIndex(i);
  255. ucItem.Width = this.Width;
  256. int y = 24 + ucItem.Height * i;
  257. e.Graphics.DrawString(ucItem.LbItemText, ucItem.LbItemTextFont, ucItem.LbItemTextBrush, new PointF(0, y + 3));
  258. Pen pBack = new Pen(Brushes.GhostWhite);
  259. pBack.Width = 26f;
  260. e.Graphics.DrawLine(pBack, new Point(this.Width - (100 + ucItem.PlProgressWidth), y + 13), new Point(this.Width - 100, y + 13));
  261. pBack.Color = ProssBarColor;
  262. int ProgressValue = this.Width - 100 - ucItem.PlProgressWidth + (ucItem.UCProgressValue*2);
  263. if (ProgressValue >= this.Width - 100)
  264. {
  265. ProgressValue = this.Width - 100;
  266. }
  267. e.Graphics.DrawLine(pBack, new Point(this.Width - (100 + ucItem.PlProgressWidth), y + 13), new Point(ProgressValue, y + 13));
  268. pBack.Dispose();
  269. e.Graphics.DrawString(ucItem.UCProgressValue.ToString() + "%", ucItem.LbItemTextFont, Brushes.Black, new PointF(this.Width - (100 + ucItem.PlProgressWidth) + ucItem.PlProgressWidth / 2 - 10, y + 3));
  270. ucItem.DrawOperatingButton(e.Graphics, ucItem.LinkOperatingBrush);
  271. ucItem.DrawDeleteButton(e.Graphics, ucItem.LinkDeleteBrush);
  272. Pen pline = new Pen(Brushes.LightGray);
  273. pline.Width = 1f;
  274. e.Graphics.DrawLine(pline, new Point(0, y + 26), new Point(this.Width, y + 26));
  275. pline.Dispose();
  276. }
  277. e.Graphics.Dispose();
  278. }
  279. void DrawOperatingButton(Graphics g, Brush b = null)
  280. {
  281. if (b == null)
  282. {
  283. b = Brushes.DodgerBlue;
  284. }
  285. g.DrawString(this.GroupLinkOperatingText, this.GroupOperatingFont, b, new PointF(this.Width - 90, 3));
  286. }
  287. void DrawDeleteButton(Graphics g, Brush b = null)
  288. {
  289. if (b == null)
  290. {
  291. b = Brushes.DodgerBlue;
  292. }
  293. g.DrawString(this.GroupLinkDeleteText, this.GroupOperatingFont, b, new PointF(this.Width - 50, 3));
  294. }
  295. string _GroupLinkOperatingText = "开始";
  296. /// <summary>
  297. /// 组LinkOperating文本
  298. /// </summary>
  299. public string GroupLinkOperatingText
  300. {
  301. get { return _GroupLinkOperatingText; }
  302. set { _GroupLinkOperatingText = value; }
  303. }
  304. string _GroupLinkDeleteText = "删除";
  305. /// <summary>
  306. /// 组LinkDelete 文本
  307. /// </summary>
  308. public string GroupLinkDeleteText
  309. {
  310. get { return _GroupLinkDeleteText; }
  311. set { _GroupLinkDeleteText = value; }
  312. }
  313. Brush GroupLinkOperatingBrush = Brushes.DodgerBlue;
  314. Brush GroupLinkDeleteBrush = Brushes.DodgerBlue;
  315. Font GroupOperatingFont = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  316. /// <summary>
  317. /// 组名字体
  318. /// </summary>
  319. Font GroupNameFont = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  320. string _GroupMark = "";
  321. /// <summary>
  322. /// 组标识
  323. /// </summary>
  324. public string GroupMark
  325. {
  326. get { return _GroupMark; }
  327. set { _GroupMark = value; }
  328. }
  329. string _GroupNameText = "组名";
  330. /// <summary>
  331. /// 组标题文本
  332. /// </summary>
  333. public string GroupNameText
  334. {
  335. get { return _GroupNameText; }
  336. set { _GroupNameText = value; }
  337. }
  338. List<UCItems> _UCItemsList = new List<UCItems>();
  339. public List<UCItems> UCItemsList
  340. {
  341. get { return _UCItemsList; }
  342. }
  343. public void Add(UCItems item)
  344. {
  345. item.ParentContainer = this;
  346. this._UCItemsList.Add(item);
  347. this.Height = 24 + this._UCItemsList.Count * item.Height;
  348. //this.Refresh();
  349. }
  350. public void Remove(UCItems item)
  351. {
  352. this._UCItemsList.Remove(item);
  353. this.Height = 24 + this._UCItemsList.Count * item.Height;
  354. this.Invalidate();
  355. }
  356. }
  357. public class UCItems
  358. {
  359. public UCItems()
  360. {
  361. }
  362. public UCItems(string itemText, int progressValue = 0)
  363. : this()
  364. {
  365. this.UCProgressValue = progressValue;
  366. this.LbItemText = itemText;
  367. SetLbProgressValue(progressValue);
  368. }
  369. UCGroupItems _ParentContainer;
  370. /// <summary>
  371. /// 父容器
  372. /// </summary>
  373. public UCGroupItems ParentContainer
  374. {
  375. get { return _ParentContainer; }
  376. set { _ParentContainer = value; }
  377. }
  378. int _Width = 900;
  379. public int Width
  380. {
  381. get { return _Width; }
  382. set { _Width = value; }
  383. }
  384. int _Height = 28;
  385. public int Height
  386. {
  387. get { return _Height; }
  388. set { _Height = value; }
  389. }
  390. int _index = 0;
  391. /// <summary>
  392. /// 索引
  393. /// </summary>
  394. public int Index
  395. {
  396. get { return _index; }
  397. }
  398. /// <summary>
  399. /// 设置索引
  400. /// </summary>
  401. /// <param name="index"></param>
  402. public void SetIndex(int index)
  403. {
  404. this._index = index;
  405. }
  406. /// <summary>
  407. /// 村粗数据
  408. /// </summary>
  409. object tag { get; set; }
  410. public object Tag
  411. {
  412. get { return tag; }
  413. set { tag = value; }
  414. }
  415. public int Y
  416. {
  417. get { return 24 + this.Height * this._index; }
  418. }
  419. public Rectangle GetLbProgressValueRectangle()
  420. {
  421. return new Rectangle(this.Width - (100 + this.PlProgressWidth), Y, 200,26);
  422. }
  423. public Rectangle GetLbItemTextRectangle()
  424. {
  425. return new Rectangle(0, Y + 1, this.Width - 300, 25);
  426. }
  427. public bool ContainsLbItemText(int x, int y)
  428. {
  429. return GetLbItemTextRectangle().Contains(x, y);
  430. }
  431. public Rectangle GetOperatingRectangle()
  432. {
  433. return new Rectangle(this.Width - 95, Y + 1, 35, 24);
  434. }
  435. public Rectangle GetDeleteRectangle()
  436. {
  437. return new Rectangle(this.Width - 52, Y + 1, 35, 24);
  438. }
  439. public bool ContainsOperating(int x, int y)
  440. {
  441. return GetOperatingRectangle().Contains(x, y);
  442. }
  443. public bool ContainsDelete(int x, int y)
  444. {
  445. return GetDeleteRectangle().Contains(x, y);
  446. }
  447. public void DrawOperatingButton(Graphics g, Brush b = null)
  448. {
  449. if (b == null)
  450. {
  451. b = Brushes.RoyalBlue;
  452. }
  453. g.DrawString(LinkOperatingText, this.OperatingFont, b, new PointF(this.Width - 90, Y + 3));
  454. }
  455. public void DrawDeleteButton(Graphics g, Brush b = null)
  456. {
  457. if (b == null)
  458. {
  459. b = Brushes.RoyalBlue;
  460. }
  461. g.DrawString(LinkDeleteText, this.OperatingFont, b, new PointF(this.Width - 50, Y + 3));
  462. }
  463. string _LinkOperatingText = "开始";
  464. /// <summary>
  465. /// 项 LinkOperatingText 文本
  466. /// </summary>
  467. public string LinkOperatingText
  468. {
  469. get { return _LinkOperatingText; }
  470. set { _LinkOperatingText = value; }
  471. }
  472. string _LinkDeleteText = "删除";
  473. /// <summary>
  474. /// 项 LinkDeleteText 文本
  475. /// </summary>
  476. public string LinkDeleteText
  477. {
  478. get { return _LinkDeleteText; }
  479. set { _LinkDeleteText = value; }
  480. }
  481. public Brush LinkOperatingBrush = Brushes.RoyalBlue;
  482. public Brush LinkDeleteBrush = Brushes.RoyalBlue;
  483. public Font OperatingFont = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  484. public Font LbItemTextFont = new Font("微软雅黑", 10);
  485. public Brush LbItemTextBrush = Brushes.Black;
  486. string _LbItemText = "新上传任务";
  487. /// <summary>
  488. ///
  489. /// </summary>
  490. public string LbItemText
  491. {
  492. get { return _LbItemText; }
  493. set { _LbItemText = value; }
  494. }
  495. public void SetLbProgressValue(int progressValue)
  496. {
  497. if (this.UCProgressValue != progressValue)
  498. {
  499. this.UCProgressValue = progressValue;
  500. if (this.ParentContainer != null)
  501. {
  502. this.ParentContainer.Invalidate(this.GetLbProgressValueRectangle());
  503. // this.ParentContainer.Invalidate();
  504. }
  505. }
  506. }
  507. int _UCProgressValue = 0;
  508. /// <summary>
  509. /// 进度值 % 百分比
  510. /// </summary>
  511. public int UCProgressValue
  512. {
  513. get { return _UCProgressValue; }
  514. set { _UCProgressValue = value; }
  515. }
  516. int _PlProgressWidth = 200;
  517. /// <summary>
  518. /// 进度条宽度
  519. /// </summary>
  520. public int PlProgressWidth
  521. {
  522. get { return _PlProgressWidth; }
  523. set { _PlProgressWidth = value; }
  524. }
  525. }
  526. public class EventGroupItemClick : EventArgs
  527. {
  528. public EventGroupItemClick(UCGroupItems gItem)
  529. {
  530. this.ucGroupItem = gItem;
  531. }
  532. UCGroupItems ucGroupItem;
  533. public UCGroupItems UcGroupItem
  534. {
  535. get { return ucGroupItem; }
  536. set { ucGroupItem = value; }
  537. }
  538. }
  539. public class EventItemClick : EventArgs
  540. {
  541. public EventItemClick(UCItems item) {
  542. this.ucItem = item;
  543. }
  544. UCItems ucItem;
  545. public UCItems UcItem
  546. {
  547. get { return ucItem; }
  548. set { ucItem = value; }
  549. }
  550. }
  551. }