UCGroupItems.cs 19 KB

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