前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
来都来了,点个【推荐】再走吧,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
没什么可准备的,直接开干吧。
思路:
2个panel,分别放标题和明细
然后重绘控件,在标题旁边画圆并且连线
开始
添加一个类来存放节点信息
1 public class TimeLineItem 2 { 3 /// <summary> 4 /// Gets or sets the title. 5 /// </summary> 6 /// <value>The title.</value> 7 public string Title { get; set; } 8 /// <summary> 9 /// Gets or sets the details. 10 /// </summary> 11 /// <value>The details.</value> 12 public string Details { get; set; } 13 }
添加一个用户控件UCTimeLine
添加一些属性
1 /// <summary> 2 /// The line color 3 /// </summary> 4 private Color lineColor = TextColors.Light; 5 6 /// <summary> 7 /// Gets or sets the color of the line. 8 /// </summary> 9 /// <value>The color of the line.</value> 10 [Description("连接线颜色"), Category("自定义")] 11 public Color LineColor 12 { 13 get { return lineColor; } 14 set 15 { 16 lineColor = value; 17 Invalidate(); 18 } 19 } 20 /// <summary> 21 /// The title font 22 /// </summary> 23 private Font titleFont = new Font("微软雅黑", 14f); 24 25 /// <summary> 26 /// Gets or sets the title font. 27 /// </summary> 28 /// <value>The title font.</value> 29 [Description("标题字体"), Category("自定义")] 30 public Font TitleFont 31 { 32 get { return titleFont; } 33 set 34 { 35 titleFont = value; 36 ReloadItems(); 37 } 38 } 39 40 /// <summary> 41 /// The title forcolor 42 /// </summary> 43 private Color titleForcolor = TextColors.MoreDark; 44 45 /// <summary> 46 /// Gets or sets the title forcolor. 47 /// </summary> 48 /// <value>The title forcolor.</value> 49 [Description("标题颜色"), Category("自定义")] 50 public Color TitleForcolor 51 { 52 get { return titleForcolor; } 53 set 54 { 55 titleForcolor = value; 56 ReloadItems(); 57 } 58 } 59 60 /// <summary> 61 /// The details font 62 /// </summary> 63 private Font detailsFont = new Font("微软雅黑", 10); 64 65 /// <summary> 66 /// Gets or sets the details font. 67 /// </summary> 68 /// <value>The details font.</value> 69 [Description("详情字体"), Category("自定义")] 70 public Font DetailsFont 71 { 72 get { return detailsFont; } 73 set 74 { 75 detailsFont = value; 76 ReloadItems(); 77 } 78 } 79 80 /// <summary> 81 /// The details forcolor 82 /// </summary> 83 private Color detailsForcolor = TextColors.Light; 84 85 /// <summary> 86 /// Gets or sets the details forcolor. 87 /// </summary> 88 /// <value>The details forcolor.</value> 89 [Description("详情颜色"), Category("自定义")] 90 public Color DetailsForcolor 91 { 92 get { return detailsForcolor; } 93 set 94 { 95 detailsForcolor = value; 96 ReloadItems(); 97 } 98 } 99 100 /// <summary> 101 /// The items 102 /// </summary> 103 TimeLineItem[] items; 10