UCTimeLine.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-10-09
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCTimeLine.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCTimeLine.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public partial class UCTimeLine : UserControl
  32. {
  33. /// <summary>
  34. /// The line color
  35. /// </summary>
  36. private Color lineColor = TextColors.Light;
  37. /// <summary>
  38. /// Gets or sets the color of the line.
  39. /// </summary>
  40. /// <value>The color of the line.</value>
  41. [Description("连接线颜色"), Category("自定义")]
  42. public Color LineColor
  43. {
  44. get { return lineColor; }
  45. set
  46. {
  47. lineColor = value;
  48. Invalidate();
  49. }
  50. }
  51. /// <summary>
  52. /// The title font
  53. /// </summary>
  54. private Font titleFont = new Font("微软雅黑", 14f);
  55. /// <summary>
  56. /// Gets or sets the title font.
  57. /// </summary>
  58. /// <value>The title font.</value>
  59. [Description("标题字体"), Category("自定义")]
  60. public Font TitleFont
  61. {
  62. get { return titleFont; }
  63. set
  64. {
  65. titleFont = value;
  66. ReloadItems();
  67. }
  68. }
  69. /// <summary>
  70. /// The title forcolor
  71. /// </summary>
  72. private Color titleForcolor = TextColors.MoreDark;
  73. /// <summary>
  74. /// Gets or sets the title forcolor.
  75. /// </summary>
  76. /// <value>The title forcolor.</value>
  77. [Description("标题颜色"), Category("自定义")]
  78. public Color TitleForcolor
  79. {
  80. get { return titleForcolor; }
  81. set
  82. {
  83. titleForcolor = value;
  84. ReloadItems();
  85. }
  86. }
  87. /// <summary>
  88. /// The details font
  89. /// </summary>
  90. private Font detailsFont = new Font("微软雅黑", 10);
  91. /// <summary>
  92. /// Gets or sets the details font.
  93. /// </summary>
  94. /// <value>The details font.</value>
  95. [Description("详情字体"), Category("自定义")]
  96. public Font DetailsFont
  97. {
  98. get { return detailsFont; }
  99. set
  100. {
  101. detailsFont = value;
  102. ReloadItems();
  103. }
  104. }
  105. /// <summary>
  106. /// The details forcolor
  107. /// </summary>
  108. private Color detailsForcolor = TextColors.Light;
  109. /// <summary>
  110. /// Gets or sets the details forcolor.
  111. /// </summary>
  112. /// <value>The details forcolor.</value>
  113. [Description("详情颜色"), Category("自定义")]
  114. public Color DetailsForcolor
  115. {
  116. get { return detailsForcolor; }
  117. set
  118. {
  119. detailsForcolor = value;
  120. ReloadItems();
  121. }
  122. }
  123. /// <summary>
  124. /// The items
  125. /// </summary>
  126. TimeLineItem[] items;
  127. /// <summary>
  128. /// Gets or sets the items.
  129. /// </summary>
  130. /// <value>The items.</value>
  131. [Description("项列表"), Category("自定义")]
  132. public TimeLineItem[] Items
  133. {
  134. get { return items; }
  135. set
  136. {
  137. items = value;
  138. ReloadItems();
  139. }
  140. }
  141. /// <summary>
  142. /// Initializes a new instance of the <see cref="UCTimeLine"/> class.
  143. /// </summary>
  144. public UCTimeLine()
  145. {
  146. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  147. this.SetStyle(ControlStyles.DoubleBuffer, true);
  148. this.SetStyle(ControlStyles.ResizeRedraw, true);
  149. this.SetStyle(ControlStyles.Selectable, true);
  150. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  151. this.SetStyle(ControlStyles.UserPaint, true);
  152. InitializeComponent();
  153. items = new TimeLineItem[0];
  154. if (ControlHelper.IsDesignMode())
  155. {
  156. items = new TimeLineItem[4];
  157. for (int i = 0; i < 4; i++)
  158. {
  159. items[i] = new TimeLineItem()
  160. {
  161. Title = DateTime.Now.AddMonths(-1 * (3 - i)).ToString("yyyy年MM月"),
  162. Details = DateTime.Now.AddMonths(-1 * (3 - i)).ToString("yyyy年MM月") + "发生了一件大事,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,然后王二麻子他爹王咔嚓出生了。"
  163. };
  164. }
  165. ReloadItems();
  166. }
  167. }
  168. /// <summary>
  169. /// Reloads the items.
  170. /// </summary>
  171. private void ReloadItems()
  172. {
  173. try
  174. {
  175. ControlHelper.FreezeControl(this, true);
  176. this.Controls.Clear();
  177. if (items != null)
  178. {
  179. foreach (var item in items)
  180. {
  181. FlowLayoutPanel panelTitle = new FlowLayoutPanel();
  182. panelTitle.Dock = DockStyle.Top;
  183. panelTitle.AutoScroll = false;
  184. panelTitle.Padding = new System.Windows.Forms.Padding(5);
  185. panelTitle.Name = "title_" + Guid.NewGuid().ToString();
  186. Label lblTitle = new Label();
  187. lblTitle.Dock = DockStyle.Top;
  188. lblTitle.AutoSize = true;
  189. lblTitle.Font = titleFont;
  190. lblTitle.ForeColor = titleForcolor;
  191. lblTitle.Text = item.Title;
  192. lblTitle.SizeChanged += item_SizeChanged;
  193. panelTitle.Controls.Add(lblTitle);
  194. this.Controls.Add(panelTitle);
  195. panelTitle.BringToFront();
  196. FlowLayoutPanel panelDetails = new FlowLayoutPanel();
  197. panelDetails.Dock = DockStyle.Top;
  198. panelDetails.AutoScroll = false;
  199. panelDetails.Padding = new System.Windows.Forms.Padding(5);
  200. panelDetails.Name = "details_" + Guid.NewGuid().ToString();
  201. Label lblDetails = new Label();
  202. lblDetails.AutoSize = true;
  203. lblDetails.Dock = DockStyle.Top;
  204. lblDetails.Font = detailsFont;
  205. lblDetails.ForeColor = detailsForcolor;
  206. lblDetails.Text = item.Details;
  207. lblDetails.SizeChanged += item_SizeChanged;
  208. panelDetails.Controls.Add(lblDetails);
  209. this.Controls.Add(panelDetails);
  210. panelDetails.BringToFront();
  211. }
  212. }
  213. }
  214. finally
  215. {
  216. ControlHelper.FreezeControl(this, false);
  217. }
  218. }
  219. /// <summary>
  220. /// Handles the SizeChanged event of the item control.
  221. /// </summary>
  222. /// <param name="sender">The source of the event.</param>
  223. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  224. void item_SizeChanged(object sender, EventArgs e)
  225. {
  226. Label lbl = (Label)sender;
  227. lbl.Parent.Height = lbl.Height + 10;
  228. }
  229. /// <summary>
  230. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  231. /// </summary>
  232. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  233. protected override void OnPaint(PaintEventArgs e)
  234. {
  235. base.OnPaint(e);
  236. var g = e.Graphics;
  237. g.SetGDIHigh();
  238. var lst = this.Controls.ToArray().Where(p => p.Name.StartsWith("title_")).ToList();
  239. for (int i = 0; i < lst.Count; i++)
  240. {
  241. //画圆
  242. g.DrawEllipse(new Pen(new SolidBrush(lineColor)), new Rectangle(7, lst[i].Location.Y + 10, 16, 16));
  243. //划线
  244. if (i != lst.Count - 1)
  245. {
  246. g.DrawLine(new Pen(new SolidBrush(lineColor)), new Point(7 + 8, lst[i].Location.Y + 10 - 2), new Point(7 + 8, lst[i + 1].Location.Y + 10 + 16 + 2));
  247. }
  248. }
  249. }
  250. }
  251. /// <summary>
  252. /// Class TimeLineItem.
  253. /// </summary>
  254. public class TimeLineItem
  255. {
  256. /// <summary>
  257. /// Gets or sets the title.
  258. /// </summary>
  259. /// <value>The title.</value>
  260. public string Title { get; set; }
  261. /// <summary>
  262. /// Gets or sets the details.
  263. /// </summary>
  264. /// <value>The details.</value>
  265. public string Details { get; set; }
  266. }
  267. }