UCTimeLine.cs 9.3 KB

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