UCTimeLine.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.Light;
  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.MoreDark;
  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 = "Code_" + item.Code;//+ Guid.NewGuid().ToString();
  167. panelTitle.Tag = item.Code;
  168. //panelTitle.BackColor = Color.Yellow;
  169. Label lblTitle = new Label();
  170. lblTitle.Dock = DockStyle.Top;
  171. lblTitle.AutoSize = true;
  172. lblTitle.Font = titleFont;
  173. lblTitle.ForeColor = titleForcolor;
  174. lblTitle.Text = item.Title;
  175. lblTitle.Tag = item.Code;
  176. lblTitle.Name = "lbl"+item.Code;
  177. lblTitle.SizeChanged += item_SizeChanged;
  178. panelTitle.Controls.Add(lblTitle);
  179. this.Controls.Add(panelTitle);
  180. panelTitle.BringToFront();
  181. FlowLayoutPanel panelDetails = new FlowLayoutPanel();
  182. panelDetails.Dock = DockStyle.Top;
  183. panelDetails.AutoScroll = false;
  184. panelDetails.Padding = new System.Windows.Forms.Padding(5);
  185. panelDetails.Name = "details_" + item.Code; //"details_" + Guid.NewGuid().ToString();
  186. panelDetails.BackColor = Color.Black;
  187. Label lblDetails = new Label();
  188. lblDetails.AutoSize = true;
  189. lblDetails.Dock = DockStyle.Top;
  190. lblDetails.Font = detailsFont;
  191. lblDetails.ForeColor = detailsForcolor;
  192. lblDetails.Text = item.Details;
  193. lblDetails.SizeChanged += item_SizeChanged;
  194. panelDetails.Controls.Add(lblDetails);
  195. this.Controls.Add(panelDetails);
  196. panelDetails.BringToFront();
  197. }
  198. }
  199. }
  200. finally
  201. {
  202. ControlHelper.FreezeControl(this, false);
  203. }
  204. }
  205. /// <summary>
  206. /// Handles the SizeChanged event of the item control.
  207. /// </summary>
  208. /// <param name="sender">The source of the event.</param>
  209. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  210. void item_SizeChanged(object sender, EventArgs e)
  211. {
  212. Label lbl = (Label)sender;
  213. lbl.Parent.Height = lbl.Height + 10;
  214. }
  215. /// <summary>
  216. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  217. /// </summary>
  218. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  219. protected override void OnPaint(PaintEventArgs e)
  220. {
  221. base.OnPaint(e);
  222. var g = e.Graphics;
  223. g.SetGDIHigh();
  224. var lst = this.Controls.ToArray().Where(p => p.Name.StartsWith("Code_")).ToList();
  225. //for (int i = lst.Count-1;i>=0; i--)
  226. for (int i = 0; i<lst.Count; i++)
  227. {
  228. Label label = (Label)lst[i].Controls[0];
  229. foreach (var item in items)
  230. {
  231. if(label.Tag.ToString() == item.Code)
  232. {
  233. switch (item.State)
  234. {
  235. case 0:
  236. lineColor = Color.Red;
  237. break;
  238. case 1:
  239. lineColor = Color.Yellow;
  240. break;
  241. case 2:
  242. lineColor = Color.White;
  243. break;
  244. default:
  245. lineColor = TextColors.Light;
  246. break;
  247. }
  248. break;
  249. }
  250. }
  251. //画圆
  252. g.DrawEllipse(new Pen(new SolidBrush(lineColor)), new Rectangle(7, lst[i].Location.Y + 10, 16, 16));
  253. //划线
  254. if (i != lst.Count - 1)
  255. {
  256. 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));
  257. }
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// Class TimeLineItem.
  263. /// </summary>
  264. public class TimeLineItem
  265. {
  266. /// <summary>
  267. /// Gets or sets the title.
  268. /// </summary>
  269. /// <value>The title.</value>
  270. public string Title { get; set; }
  271. /// <summary>
  272. /// Gets or sets the details.
  273. /// </summary>
  274. /// <value>The details.</value>
  275. public string Details { get; set; }
  276. /// <summary>
  277. /// Gets or sets the Code.
  278. /// </summary>
  279. /// <value>The Code.</value>
  280. public string Code { get; set; }
  281. /// <summary>
  282. /// Gets or sets the State.
  283. /// </summary>
  284. /// <value>The State.</value>
  285. public int State { get; set; }
  286. /// <summary>
  287. /// Gets or sets the IsData.
  288. /// </summary>
  289. /// <value>The IsData.</value>
  290. public bool IsData { get; set; }
  291. }
  292. }