前提
入行已经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
用处及效果
准备工作
请先了解GDI+和三角函数
开始
添加一个类UCValve,继承UserControl
添加一个阀门显示样式枚举
1 /// <summary> 2 /// Enum ValveStyle 3 /// </summary> 4 public enum ValveStyle 5 { 6 /// <summary> 7 /// 横向,开关在上方 8 /// </summary> 9 Horizontal_Top, 10 /// <summary> 11 /// 横向,开关在下方 12 /// </summary> 13 Horizontal_Bottom, 14 /// <summary> 15 /// 纵向,开关在左侧 16 /// </summary> 17 Vertical_Left, 18 /// <summary> 19 /// 纵向,开关在右侧 20 /// </summary> 21 Vertical_Right, 22 }
添加一些属性
1 /// <summary> 2 /// The valve style 3 /// </summary> 4 private ValveStyle valveStyle = ValveStyle.Horizontal_Top; 5 6 /// <summary> 7 /// Gets or sets the valve style. 8 /// </summary> 9 /// <value>The valve style.</value> 10 [Description("阀门样式"), Category("自定义")] 11 public ValveStyle ValveStyle 12 { 13 get { return valveStyle; } 14 set 15 { 16 valveStyle = value; 17 Refresh(); 18 } 19 } 20 21 /// <summary> 22 /// The valve color 23 /// </summary> 24 private Color valveColor = Color.FromArgb(255, 77, 59); 25 26 /// <summary> 27 /// Gets or sets the color of the valve. 28 /// </summary> 29 /// <value>The color of the valve.</value> 30 [Description("阀门颜色"), Category("自定义")] 31 public Color ValveColor 32 { 33 get { return valveColor; } 34 set 35 { 36 valveColor = value; 37 Refresh(); 38 } 39 } 40 41 /// <summary> 42 /// The switch color 43 /// </summary> 44 private Color switchColor = Color.FromArgb(232, 30, 99); 45 46 /// <summary> 47 /// Gets or sets the color of the switch. 48 /// </summary> 49 /// <value>The color of the switch.</value> 50 [Description("开关把手颜色"), Category("自定义")] 51 public Color SwitchColor 52 { 53 get { return switchColor; } 54 set 55 { 56 switchColor = value; 57 Refresh(); 58 } 59 } 60 61 /// <summary> 62 /// The axis color 63 /// </summary> 64 private Color axisColor = Color.FromArgb(3, 169, 243); 65 66 /// <summary> 67 /// Gets or sets the color of the axis. 68 /// </summary> 69 /// <value>The color of the axis.</value> 70 [Description("轴颜色"), Category("自定义")] 71 public Color AxisColor 72 { 73 get { return axisColor; } 74 set 75 { 76 axisColor = value; 77 Refresh(); 78 } 79 } 80 81 /// <summary> 82 /// The asis bottom color 83 /// </summary> 84 private Color asisBottomColor = Color.FromArgb(3, 169, 243); 85 86 /// <summary> 87 /// Gets or sets the color of the asis bottom. 88 /// </summary> 89 /// <value>The color of the asis bottom.</value> 90 [Description("轴底座颜色"), Category("自定义")] 91 public Color AsisBottomColor 92 { 93 get { return asisBottomColor; } 94 set { asisBottomColor = value; } 95 } 96 97 /// <summary> 98 /// The opened 99 /// </summary> 100 private bool opened = true; 101 102 /// <summary> 103 /// Gets or sets a value indicating whether this <see cref="UCValve" /> is opened. 104 /// </summary> 105 /// <value><c>true</c> if opened; otherwise, <c>false</c>.</value> 106 [Description("是否打开"), Category("自定义")] 107 public bool Opened 108 { 109 get { return opened; } 110 set 111 { 112 opened = value; 113 Refresh(); 114 } 115 } 116 117 /// <summary> 118 /// The liquid direction 119 /// </summary> 120 private LiquidDirection liquidDirection = LiquidDirection.Forward; 121 122 /// <summary> 123 /// Gets or sets the liquid direction. 124 /// </summary> 125 /// <value>The liquid direction.</value> 126 [Description("液体流动方向"), Category("自定义")] 127 public LiquidDirection LiquidDirection 128 { 129 get { return liquidDirection; } 130 set 131 { 132 liquidDirection = value; 133 if (value == Conduit.LiquidDirection.None) 134 m_timer.Enabled = false; 135 else 136 m_timer.Enabled = true; 137 Refresh(); 138 } 139 } 140 141 /// <summary> 142 /// The liquid speed 143 /// </summary> 144 private int liquidSpeed = 100; 145 146 /// <summary> 147 /// 液体流速,越小,速度越快Gets or sets the liquid speed. 148 /// </summary> 149 /// <value>The liquid speed.</value> 150 [Description("液体流速,越小,速度越快"), Category("自定义")] 151 public int LiquidSpeed 152 { 153 get { return liquidSpeed; } 154 set 155 { 156 if (value <= 0) 157 return; 158 liquidSpeed = value; 159 m_timer.Interval = value; 160 } 161 } 162 163 /// <summary> 164 /// The liquid color 165 /// </summary> 166 private Color liquidColor = Color.FromArgb(3, 169, 243); 167 168 /// <summary> 169 /// Gets or sets the color of the liquid. 170 /// </summary> 171 /// <value>The color of the liquid.</value> 172 [Description("液体颜色"), Category("自定义")] 173 public Color LiquidColor 174 { 175 get { return liquidColor; }