(五十五)c#Winform自定义控件-管道(工业)
程序员文章站
2022-07-02 12:59:02
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
麻烦博客下方点个【推荐】,谢谢
nuget
install-package hzh_controls
目录
用处及效果
准备工作
使用gdi+画的,用到了三角函数,如果不了解可以先行百度
开始
添加一个类ucconduit,继承usercontrol
添加几个属性
1 /// <summary> 2 /// the conduit style 3 /// </summary> 4 private conduitstyle conduitstyle = conduitstyle.horizontal_none_none; 5 6 /// <summary> 7 /// gets or sets the conduit style. 8 /// </summary> 9 /// <value>the conduit style.</value> 10 [description("样式"), category("自定义")] 11 public conduitstyle conduitstyle 12 { 13 get { return conduitstyle; } 14 set 15 { 16 string strold = conduitstyle.tostring().substring(0, 1); 17 string strnew = value.tostring().substring(0, 1); 18 conduitstyle = value; 19 if (strold != strnew) 20 { 21 this.size = new size(this.size.height, this.size.width); 22 } 23 refresh(); 24 } 25 } 26 27 /// <summary> 28 /// the conduit color 29 /// </summary> 30 private color conduitcolor = color.fromargb(255, 77, 59); 31 [description("颜色"), category("自定义")] 32 /// <summary> 33 /// gets or sets the color of the conduit. 34 /// </summary> 35 /// <value>the color of the conduit.</value> 36 public color conduitcolor 37 { 38 get { return conduitcolor; } 39 set 40 { 41 conduitcolor = value; 42 refresh(); 43 } 44 } 45 46 /// <summary> 47 /// the liquid color 48 /// </summary> 49 private color liquidcolor = color.fromargb(3, 169, 243); 50 51 /// <summary> 52 /// gets or sets the color of the liquid. 53 /// </summary> 54 /// <value>the color of the liquid.</value> 55 [description("液体颜色"), category("自定义")] 56 public color liquidcolor 57 { 58 get { return liquidcolor; } 59 set 60 { 61 liquidcolor = value; 62 if (liquiddirection != conduit.liquiddirection.none) 63 refresh(); 64 } 65 } 66 67 /// <summary> 68 /// the liquid direction 69 /// </summary> 70 private liquiddirection liquiddirection = liquiddirection.forward; 71 72 /// <summary> 73 /// gets or sets the liquid direction. 74 /// </summary> 75 /// <value>the liquid direction.</value> 76 [description("液体流动方向"), category("自定义")] 77 public liquiddirection liquiddirection 78 { 79 get { return liquiddirection; } 80 set 81 { 82 liquiddirection = value; 83 refresh(); 84 } 85 } 86 87 /// <summary> 88 /// the liquid speed 89 /// </summary> 90 private int liquidspeed = 100; 91 92 /// <summary> 93 /// 液体流速,越小,速度越快gets or sets the liquid speed. 94 /// </summary> 95 /// <value>the liquid speed.</value> 96 [description("液体流速,越小,速度越快"), category("自定义")] 97 public int liquidspeed 98 { 99 get { return liquidspeed; } 100 set 101 { 102 if (value <= 0) 103 return; 104 liquidspeed = value; 105 m_timer.interval = value; 106 } 107 } 108 109 /// <summary> 110 /// the int pen width 111 /// </summary> 112 int intpenwidth = 0; 113 114 /// <summary> 115 /// the int line left 116 /// </summary> 117 int intlineleft = 0; 118 /// <summary> 119 /// the m timer 120 /// </summary> 121 timer m_timer;
根据参数设置重绘
1 /// <summary> 2 /// 引发 <see cref="e:system.windows.forms.control.paint" /> 事件。 3 /// </summary> 4 /// <param name="e">包含事件数据的 <see cref="t:system.windows.forms.painteventargs" />。</param> 5 protected override void onpaint(painteventargs e) 6 { 7 base.onpaint(e); 8 graphics g = e.graphics; 9 10 list<arcentity> lstarcs = new list<arcentity>(); 11 12 graphicspath path = new graphicspath(); 13 graphicspath linepath = new graphicspath(); 14 switch (conduitstyle) 15 { 16 #region h english:h 17 case conduitstyle.horizontal_none_none: 18 path.addlines(new pointf[] 19 { 20 new pointf(0, 0), 21 new pointf(this.clientrectangle.right, 0), 22 new pointf(this.clientrectangle.right, this.height), 23 new pointf(0, this.height) 24 }); 25 path.closeallfigures(); 26 linepath.addline(0, this.height / 2, this.width, this.height / 2); 27 break; 28 case conduitstyle.horizontal_up_none: 29 path.addlines(new pointf[] 30 { 31 new pointf(0, 0), 32 new pointf(this.clientrectangle.right, 0), 33 new pointf(this.clientrectangle.right, this.height), 34 new pointf(0+intpenwidth, this.height) 35 }); 36 path.addarc(new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 90, 90); 37 path.closeallfigures(); 38 39 linepath.addarc(new rectangle(intpenwidth / 2 + 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 40 linepath.addline(intpenwidth, this.height / 2, this.width, this.height / 2); 41 42 lstarcs.add(new arcentity() { rect = new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 43 break; 44 case conduitstyle.horizontal_down_none: 45 path.addlines(new pointf[] 46 { 47 new pointf(intpenwidth, 0), 48 new pointf(this.clientrectangle.right, 0), 49 new pointf(this.clientrectangle.right, this.height), 50 new pointf(0, this.height) 51 }); 52 path.addarc(new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), 180, 90); 53 path.closeallfigures(); 54 55 linepath.addarc(new rectangle(intpenwidth / 2 + 1, this.height / 2, intpenwidth, intpenwidth), 179, 91); 56 linepath.addline(intpenwidth + 1, this.height / 2, this.width, this.height / 2); 57 58 lstarcs.add(new arcentity() { rect = new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 59 break; 60 case conduitstyle.horizontal_none_up: 61 path.addlines(new pointf[] 62 { 63 new pointf(this.clientrectangle.right-intpenwidth, this.height), 64 new pointf(0, this.height), 65 new pointf(0, 0), 66 new pointf(this.clientrectangle.right-intpenwidth, 0) 67 }); 68 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 0, 90); 69 path.closeallfigures(); 70 71 linepath.addline(0, this.height / 2, this.width - intpenwidth, this.height / 2); 72 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 91, -91); 73 74 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 75 break; 76 case conduitstyle.horizontal_none_down: 77 path.addlines(new pointf[] 78 { 79 new pointf(this.clientrectangle.right, this.height), 80 new pointf(0, this.height), 81 new pointf(0, 0), 82 new pointf(this.clientrectangle.right-intpenwidth, 0) 83 }); 84 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), 270, 90); 85 path.closeallfigures(); 86 87 linepath.addline(0, this.height / 2, this.width - intpenwidth - 1, this.height / 2); 88 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, intpenwidth / 2, intpenwidth, intpenwidth), 269, 91); 89 90 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 91 break; 92 case conduitstyle.horizontal_down_up: 93 path.addline(new point(intpenwidth, 0), new point(this.width, 0)); 94 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 0, 90); 95 path.addline(new point(this.width - intpenwidth, this.height), new point(0, this.height)); 96 path.addarc(new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), 180, 90); 97 path.closeallfigures(); 98 99 linepath.addarc(new rectangle(intpenwidth / 2 + 1, this.height / 2, intpenwidth, intpenwidth), 179, 91); 100 //linepath.addline(intpenwidth, this.height / 2, this.width - intpenwidth, this.height / 2); 101 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 91, -91); 102 103 lstarcs.add(new arcentity() { rect = new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 104 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 105 break; 106 case conduitstyle.horizontal_up_down: 107 path.addline(new point(0, 0), new point(this.width - intpenwidth, 0)); 108 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), 270, 90); 109 path.addline(new point(this.width, this.height), new point(intpenwidth, this.height)); 110 path.addarc(new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 90, 90); 111 path.closeallfigures(); 112 113 linepath.addarc(new rectangle(intpenwidth / 2 + 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 114 linepath.addline(intpenwidth, this.height / 2, this.width - intpenwidth - 1, this.height / 2); 115 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, intpenwidth / 2, intpenwidth, intpenwidth), 269, 91); 116 117 lstarcs.add(new arcentity() { rect = new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 118 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 119 break; 120 case conduitstyle.horizontal_up_up: 121 path.addline(new point(0, 0), new point(this.width, 0)); 122 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 0, 90); 123 path.addline(new point(this.width - intpenwidth, this.height), new point(intpenwidth, this.height)); 124 path.addarc(new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 90, 90); 125 path.closeallfigures(); 126 127 linepath.addarc(new rectangle(intpenwidth / 2 + 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 128 //linepath.addline(intpenwidth, this.height / 2, this.width - intpenwidth, this.height / 2); 129 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 91, -91); 130 131 lstarcs.add(new arcentity() { rect = new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 132 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 133 break; 134 case conduitstyle.horizontal_down_down: 135 path.addline(new point(intpenwidth, 0), new point(this.width - intpenwidth, 0)); 136 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), 270, 90); 137 path.addline(new point(this.width, this.height), new point(0, this.height)); 138 path.addarc(new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), 180, 90); 139 path.closeallfigures(); 140 141 linepath.addarc(new rectangle(intpenwidth / 2 + 1, this.height / 2, intpenwidth, intpenwidth), 179, 91); 142 linepath.addline(intpenwidth + 1, this.height / 2, this.width - intpenwidth - 1, this.height / 2); 143 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, intpenwidth / 2, intpenwidth, intpenwidth), 269, 91); 144 145 lstarcs.add(new arcentity() { rect = new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 146 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 147 break; 148 #endregion 149 150 #region v english:v 151 case conduitstyle.vertical_none_none: 152 path.addlines(new pointf[] 153 { 154 new pointf(0, 0), 155 new pointf(this.clientrectangle.right, 0), 156 new pointf(this.clientrectangle.right, this.height), 157 new pointf(0, this.height) 158 }); 159 path.closeallfigures(); 160 linepath.addline(this.width / 2, 0, this.width / 2, this.height); 161 break; 162 case conduitstyle.vertical_left_none: 163 path.addlines(new pointf[] 164 { 165 new pointf(this.clientrectangle.right, intpenwidth), 166 new pointf(this.clientrectangle.right, this.height), 167 new pointf(0, this.height), 168 new pointf(0, 0) 169 }); 170 path.addarc(new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), 270, 90); 171 path.closeallfigures(); 172 173 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 269, 91); 174 linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height); 175 176 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 177 break; 178 case conduitstyle.vertical_right_none: 179 path.addlines(new pointf[] 180 { 181 new pointf(this.clientrectangle.right, 0), 182 new pointf(this.clientrectangle.right, this.height), 183 new pointf(0, this.height), 184 new pointf(0, intpenwidth) 185 }); 186 path.addarc(new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), 180, 90); 187 path.closeallfigures(); 188 189 linepath.addarc(new rectangle(intpenwidth / 2, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 271, -91); 190 linepath.addline(intpenwidth / 2, intpenwidth + 1, intpenwidth / 2, this.height); 191 192 lstarcs.add(new arcentity() { rect = new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 193 break; 194 case conduitstyle.vertical_none_left: 195 path.addlines(new pointf[] 196 { 197 new pointf(0, this.height), 198 new pointf(0, 0), 199 new pointf(this.clientrectangle.right, 0), 200 new pointf(this.clientrectangle.right, this.height-intpenwidth), 201 }); 202 path.addarc(new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 0, 90); 203 path.closeallfigures(); 204 205 linepath.addline(this.width / 2, 0, this.width / 2, this.height - intpenwidth); 206 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), -1, 91); 207 208 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 209 break; 210 case conduitstyle.vertical_none_right: 211 path.addlines(new pointf[] 212 { 213 new pointf(0, this.height-intpenwidth), 214 new pointf(0, 0), 215 new pointf(this.clientrectangle.right, 0), 216 new pointf(this.clientrectangle.right, this.height), 217 }); 218 path.addarc(new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 90, 90); 219 path.closeallfigures(); 220 221 linepath.addline(this.width / 2, 0, this.width / 2, this.height - intpenwidth - 1); 222 linepath.addarc(new rectangle(intpenwidth / 2, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 223 224 lstarcs.add(new arcentity() { rect = new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 225 break; 226 case conduitstyle.vertical_left_right: 227 path.addline(this.width, intpenwidth, this.width, this.height); 228 path.addarc(new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 90, 90); 229 path.addline(0, this.height - intpenwidth, 0, 0); 230 path.addarc(new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), 270, 90); 231 path.closeallfigures(); 232 233 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 269, 91); 234 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 235 linepath.addarc(new rectangle(intpenwidth / 2, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 236 237 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 238 lstarcs.add(new arcentity() { rect = new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 239 break; 240 case conduitstyle.vertical_right_left: 241 path.addline(this.width, 0, this.width, this.height - intpenwidth); 242 path.addarc(new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 0, 90); 243 path.addline(0, this.height, 0, intpenwidth); 244 path.addarc(new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), 180, 90); 245 path.closeallfigures(); 246 247 linepath.addarc(new rectangle(intpenwidth / 2, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 271, -91); 248 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 249 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), -1, 91); 250 251 lstarcs.add(new arcentity() { rect = new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 252 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 253 break; 254 case conduitstyle.vertical_left_left: 255 path.addline(this.width, intpenwidth, this.width, this.height - intpenwidth); 256 path.addarc(new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 0, 90); 257 path.addline(0, this.height, 0, 0); 258 path.addarc(new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), 270, 90); 259 path.closeallfigures(); 260 261 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 269, 91); 262 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 263 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), -1, 91); 264 265 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 266 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 267 break; 268 case conduitstyle.vertical_right_right: 269 path.addline(this.width, 0, this.width, this.height); 270 path.addarc(new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 90, 90); 271 path.addline(0, this.height - intpenwidth, 0, intpenwidth); 272 path.addarc(new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), 180, 90); 273 path.closeallfigures(); 274 275 linepath.addarc(new rectangle(intpenwidth / 2, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 271, -91); 276 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 277 linepath.addarc(new rectangle(intpenwidth / 2, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), 180, -91); 278 279 lstarcs.add(new arcentity() { rect = new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 280 lstarcs.add(new arcentity() { rect = new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 281 break; 282 #endregion 283 } 284 g.fillpath(new solidbrush(conduitcolor), path); 285 286 //渐变色 287 int intcount = intpenwidth / 2 / 4; 288 int intsplit = (255 - 100) / intcount; 289 for (int i = 0; i < intcount; i++) 290 { 291 int _penwidth = intpenwidth / 2 - 4 * i; 292 if (_penwidth <= 0) 293 _penwidth = 1; 294 g.drawpath(new pen(new solidbrush(color.fromargb(40, color.white.r, color.white.g, color.white.b)), _penwidth), linepath); 295 if (_penwidth == 1) 296 break; 297 } 298 299 g.setgdihigh(); 300 //使用抗锯齿画圆角 301 foreach (var item in lstarcs) 302 { 303 g.drawarc(new pen(new solidbrush(this.backcolor)), item.rect, item.startangle, item.sweepangle); 304 } 305 306 //液体流动 307 if (liquiddirection != conduit.liquiddirection.none) 308 { 309 pen p = new pen(new solidbrush(liquidcolor), 4); 310 p.dashpattern = new float[] { 6, 6 }; 311 p.dashoffset = intlineleft * (liquiddirection == conduit.liquiddirection.forward ? -1 : 1); 312 g.drawpath(p, linepath); 313 } 314 }
一个记录圆角的类
1 /// <summary> 2 /// class arcentity. 3 /// </summary> 4 class arcentity 5 { 6 /// <summary> 7 /// gets or sets the rect. 8 /// </summary> 9 /// <value>the rect.</value> 10 public rectangle rect { get; set; } 11 /// <summary> 12 /// gets or sets the start angle. 13 /// </summary> 14 /// <value>the start angle.</value> 15 public float startangle { get; set; } 16 /// <summary> 17 /// gets or sets the sweep angle. 18 /// </summary> 19 /// <value>the sweep angle.</value> 20 public float sweepangle { get; set; } 21 }
2个枚举
1 /// <summary> 2 /// enum liquiddirection 3 /// </summary> 4 public enum liquiddirection 5 { 6 /// <summary> 7 /// the none 8 /// </summary> 9 none, 10 /// <summary> 11 /// the forward 12 /// </summary> 13 forward, 14 /// <summary> 15 /// the backward 16 /// </summary> 17 backward 18 } 19 20 /// <summary> 21 /// 管道样式enum conduitstyle 22 /// </summary> 23 public enum conduitstyle 24 { 25 /// <summary> 26 /// 直线 the horizontal none none 27 /// </summary> 28 horizontal_none_none, 29 /// <summary> 30 /// 左上the horizontal up none 31 /// </summary> 32 horizontal_up_none, 33 /// <summary> 34 /// 左下the horizontal down none 35 /// </summary> 36 horizontal_down_none, 37 /// <summary> 38 /// 右上the horizontal none up 39 /// </summary> 40 horizontal_none_up, 41 /// <summary> 42 /// 右下the horizontal none down 43 /// </summary> 44 horizontal_none_down, 45 /// <summary> 46 /// 左下右上the horizontal down up 47 /// </summary> 48 horizontal_down_up, 49 /// <summary> 50 /// 左上右下the horizontal up down 51 /// </summary> 52 horizontal_up_down, 53 /// <summary> 54 /// 左上,右上the horizontal up up 55 /// </summary> 56 horizontal_up_up, 57 /// <summary> 58 /// 左下右下the horizontal down down 59 /// </summary> 60 horizontal_down_down, 61 62 /// <summary> 63 /// 竖线the vertical none none 64 /// </summary> 65 vertical_none_none, 66 /// <summary> 67 /// 上左the vertical left none 68 /// </summary> 69 vertical_left_none, 70 /// <summary> 71 /// 上右the vertical right none 72 /// </summary> 73 vertical_right_none, 74 /// <summary> 75 /// 下左the vertical none left 76 /// </summary> 77 vertical_none_left, 78 /// <summary> 79 /// 下右the vertical none right 80 /// </summary> 81 vertical_none_right, 82 /// <summary> 83 /// 上左下右the vertical left right 84 /// </summary> 85 vertical_left_right, 86 /// <summary> 87 /// 上右下左the vertical right left 88 /// </summary> 89 vertical_right_left, 90 /// <summary> 91 /// 上左下左the vertical left left 92 /// </summary> 93 vertical_left_left, 94 /// <summary> 95 /// 上右下右the vertical right left 96 /// </summary> 97 vertical_right_right, 98 }
重点讲解来了,
重绘的时候,先填充底色,并且记录下中心线path,和圆角
填充底色后,画中间的渐变色
然后设置g为抗锯齿模式,把圆角重画一遍,就没有锯齿感了
然后根据中心线,画液体就可以了
完整代码
1 // *********************************************************************** 2 // assembly : hzh_controls 3 // created : 2019-09-04 4 // 5 // *********************************************************************** 6 // <copyright file="ucconduit.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.linq; 19 using system.text; 20 using system.windows.forms; 21 using system.drawing; 22 using system.drawing.drawing2d; 23 using system.componentmodel; 24 25 namespace hzh_controls.controls.conduit 26 { 27 /// <summary> 28 /// class ucconduit. 29 /// implements the <see cref="system.windows.forms.usercontrol" /> 30 /// </summary> 31 /// <seealso cref="system.windows.forms.usercontrol" /> 32 public class ucconduit : usercontrol 33 { 34 /// <summary> 35 /// the conduit style 36 /// </summary> 37 private conduitstyle conduitstyle = conduitstyle.horizontal_none_none; 38 39 /// <summary> 40 /// gets or sets the conduit style. 41 /// </summary> 42 /// <value>the conduit style.</value> 43 [description("样式"), category("自定义")] 44 public conduitstyle conduitstyle 45 { 46 get { return conduitstyle; } 47 set 48 { 49 string strold = conduitstyle.tostring().substring(0, 1); 50 string strnew = value.tostring().substring(0, 1); 51 conduitstyle = value; 52 if (strold != strnew) 53 { 54 this.size = new size(this.size.height, this.size.width); 55 } 56 refresh(); 57 } 58 } 59 60 /// <summary> 61 /// the conduit color 62 /// </summary> 63 private color conduitcolor = color.fromargb(255, 77, 59); 64 [description("颜色"), category("自定义")] 65 /// <summary> 66 /// gets or sets the color of the conduit. 67 /// </summary> 68 /// <value>the color of the conduit.</value> 69 public color conduitcolor 70 { 71 get { return conduitcolor; } 72 set 73 { 74 conduitcolor = value; 75 refresh(); 76 } 77 } 78 79 /// <summary> 80 /// the liquid color 81 /// </summary> 82 private color liquidcolor = color.fromargb(3, 169, 243); 83 84 /// <summary> 85 /// gets or sets the color of the liquid. 86 /// </summary> 87 /// <value>the color of the liquid.</value> 88 [description("液体颜色"), category("自定义")] 89 public color liquidcolor 90 { 91 get { return liquidcolor; } 92 set 93 { 94 liquidcolor = value; 95 if (liquiddirection != conduit.liquiddirection.none) 96 refresh(); 97 } 98 } 99 100 /// <summary> 101 /// the liquid direction 102 /// </summary> 103 private liquiddirection liquiddirection = liquiddirection.forward; 104 105 /// <summary> 106 /// gets or sets the liquid direction. 107 /// </summary> 108 /// <value>the liquid direction.</value> 109 [description("液体流动方向"), category("自定义")] 110 public liquiddirection liquiddirection 111 { 112 get { return liquiddirection; } 113 set 114 { 115 liquiddirection = value; 116 refresh(); 117 } 118 } 119 120 /// <summary> 121 /// the liquid speed 122 /// </summary> 123 private int liquidspeed = 100; 124 125 /// <summary> 126 /// 液体流速,越小,速度越快gets or sets the liquid speed. 127 /// </summary> 128 /// <value>the liquid speed.</value> 129 [description("液体流速,越小,速度越快"), category("自定义")] 130 public int liquidspeed 131 { 132 get { return liquidspeed; } 133 set 134 { 135 if (value <= 0) 136 return; 137 liquidspeed = value; 138 m_timer.interval = value; 139 } 140 } 141 142 /// <summary> 143 /// the int pen width 144 /// </summary> 145 int intpenwidth = 0; 146 147 /// <summary> 148 /// the int line left 149 /// </summary> 150 int intlineleft = 0; 151 /// <summary> 152 /// the m timer 153 /// </summary> 154 timer m_timer; 155 /// <summary> 156 /// initializes a new instance of the <see cref="ucconduit"/> class. 157 /// </summary> 158 public ucconduit() 159 { 160 this.setstyle(controlstyles.allpaintinginwmpaint, true); 161 this.setstyle(controlstyles.doublebuffer, true); 162 this.setstyle(controlstyles.resizeredraw, true); 163 this.setstyle(controlstyles.selectable, true); 164 this.setstyle(controlstyles.supportstransparentbackcolor, true); 165 this.setstyle(controlstyles.userpaint, true); 166 this.autoscalemode = system.windows.forms.autoscalemode.none; 167 this.sizechanged += ucconduit_sizechanged; 168 this.size = new size(200, 50); 169 m_timer = new timer(); 170 m_timer.interval = 100; 171 m_timer.tick += timer_tick; 172 m_timer.enabled = true; 173 } 174 175 /// <summary> 176 /// handles the tick event of the timer control. 177 /// </summary> 178 /// <param name="sender">the source of the event.</param> 179 /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param> 180 void timer_tick(object sender, eventargs e) 181 { 182 intlineleft += 2; 183 if (intlineleft > 12) 184 intlineleft = 0; 185 refresh(); 186 } 187 188 189 /// <summary> 190 /// handles the sizechanged event of the ucconduit control. 191 /// </summary> 192 /// <param name="sender">the source of the event.</param> 193 /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param> 194 void ucconduit_sizechanged(object sender, eventargs e) 195 { 196 intpenwidth = conduitstyle.tostring().startswith("h") ? this.height : this.width; 197 } 198 199 /// <summary> 200 /// 引发 <see cref="e:system.windows.forms.control.paint" /> 事件。 201 /// </summary> 202 /// <param name="e">包含事件数据的 <see cref="t:system.windows.forms.painteventargs" />。</param> 203 protected override void onpaint(painteventargs e) 204 { 205 base.onpaint(e); 206 graphics g = e.graphics; 207 208 list<arcentity> lstarcs = new list<arcentity>(); 209 210 graphicspath path = new graphicspath(); 211 graphicspath linepath = new graphicspath(); 212 switch (conduitstyle) 213 { 214 #region h english:h 215 case conduitstyle.horizontal_none_none: 216 path.addlines(new pointf[] 217 { 218 new pointf(0, 0), 219 new pointf(this.clientrectangle.right, 0), 220 new pointf(this.clientrectangle.right, this.height), 221 new pointf(0, this.height) 222 }); 223 path.closeallfigures(); 224 linepath.addline(0, this.height / 2, this.width, this.height / 2); 225 break; 226 case conduitstyle.horizontal_up_none: 227 path.addlines(new pointf[] 228 { 229 new pointf(0, 0), 230 new pointf(this.clientrectangle.right, 0), 231 new pointf(this.clientrectangle.right, this.height), 232 new pointf(0+intpenwidth, this.height) 233 }); 234 path.addarc(new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 90, 90); 235 path.closeallfigures(); 236 237 linepath.addarc(new rectangle(intpenwidth / 2 + 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 238 linepath.addline(intpenwidth, this.height / 2, this.width, this.height / 2); 239 240 lstarcs.add(new arcentity() { rect = new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 241 break; 242 case conduitstyle.horizontal_down_none: 243 path.addlines(new pointf[] 244 { 245 new pointf(intpenwidth, 0), 246 new pointf(this.clientrectangle.right, 0), 247 new pointf(this.clientrectangle.right, this.height), 248 new pointf(0, this.height) 249 }); 250 path.addarc(new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), 180, 90); 251 path.closeallfigures(); 252 253 linepath.addarc(new rectangle(intpenwidth / 2 + 1, this.height / 2, intpenwidth, intpenwidth), 179, 91); 254 linepath.addline(intpenwidth + 1, this.height / 2, this.width, this.height / 2); 255 256 lstarcs.add(new arcentity() { rect = new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 257 break; 258 case conduitstyle.horizontal_none_up: 259 path.addlines(new pointf[] 260 { 261 new pointf(this.clientrectangle.right-intpenwidth, this.height), 262 new pointf(0, this.height), 263 new pointf(0, 0), 264 new pointf(this.clientrectangle.right-intpenwidth, 0) 265 }); 266 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 0, 90); 267 path.closeallfigures(); 268 269 linepath.addline(0, this.height / 2, this.width - intpenwidth, this.height / 2); 270 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 91, -91); 271 272 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 273 break; 274 case conduitstyle.horizontal_none_down: 275 path.addlines(new pointf[] 276 { 277 new pointf(this.clientrectangle.right, this.height), 278 new pointf(0, this.height), 279 new pointf(0, 0), 280 new pointf(this.clientrectangle.right-intpenwidth, 0) 281 }); 282 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), 270, 90); 283 path.closeallfigures(); 284 285 linepath.addline(0, this.height / 2, this.width - intpenwidth - 1, this.height / 2); 286 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, intpenwidth / 2, intpenwidth, intpenwidth), 269, 91); 287 288 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 289 break; 290 case conduitstyle.horizontal_down_up: 291 path.addline(new point(intpenwidth, 0), new point(this.width, 0)); 292 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 0, 90); 293 path.addline(new point(this.width - intpenwidth, this.height), new point(0, this.height)); 294 path.addarc(new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), 180, 90); 295 path.closeallfigures(); 296 297 linepath.addarc(new rectangle(intpenwidth / 2 + 1, this.height / 2, intpenwidth, intpenwidth), 179, 91); 298 //linepath.addline(intpenwidth, this.height / 2, this.width - intpenwidth, this.height / 2); 299 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 91, -91); 300 301 lstarcs.add(new arcentity() { rect = new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 302 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 303 break; 304 case conduitstyle.horizontal_up_down: 305 path.addline(new point(0, 0), new point(this.width - intpenwidth, 0)); 306 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), 270, 90); 307 path.addline(new point(this.width, this.height), new point(intpenwidth, this.height)); 308 path.addarc(new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 90, 90); 309 path.closeallfigures(); 310 311 linepath.addarc(new rectangle(intpenwidth / 2 + 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 312 linepath.addline(intpenwidth, this.height / 2, this.width - intpenwidth - 1, this.height / 2); 313 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, intpenwidth / 2, intpenwidth, intpenwidth), 269, 91); 314 315 lstarcs.add(new arcentity() { rect = new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 316 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 317 break; 318 case conduitstyle.horizontal_up_up: 319 path.addline(new point(0, 0), new point(this.width, 0)); 320 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 0, 90); 321 path.addline(new point(this.width - intpenwidth, this.height), new point(intpenwidth, this.height)); 322 path.addarc(new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), 90, 90); 323 path.closeallfigures(); 324 325 linepath.addarc(new rectangle(intpenwidth / 2 + 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 326 //linepath.addline(intpenwidth, this.height / 2, this.width - intpenwidth, this.height / 2); 327 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, -1 * intpenwidth / 2 - 1, intpenwidth, intpenwidth), 91, -91); 328 329 lstarcs.add(new arcentity() { rect = new rectangle(0, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 330 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, intpenwidth * -1, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 331 break; 332 case conduitstyle.horizontal_down_down: 333 path.addline(new point(intpenwidth, 0), new point(this.width - intpenwidth, 0)); 334 path.addarc(new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), 270, 90); 335 path.addline(new point(this.width, this.height), new point(0, this.height)); 336 path.addarc(new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), 180, 90); 337 path.closeallfigures(); 338 339 linepath.addarc(new rectangle(intpenwidth / 2 + 1, this.height / 2, intpenwidth, intpenwidth), 179, 91); 340 linepath.addline(intpenwidth + 1, this.height / 2, this.width - intpenwidth - 1, this.height / 2); 341 linepath.addarc(new rectangle(this.clientrectangle.right - intpenwidth - intpenwidth / 2 - 1, intpenwidth / 2, intpenwidth, intpenwidth), 269, 91); 342 343 lstarcs.add(new arcentity() { rect = new rectangle(0, -1, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 344 lstarcs.add(new arcentity() { rect = new rectangle(this.clientrectangle.right - intpenwidth * 2, -1, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 345 break; 346 #endregion 347 348 #region v english:v 349 case conduitstyle.vertical_none_none: 350 path.addlines(new pointf[] 351 { 352 new pointf(0, 0), 353 new pointf(this.clientrectangle.right, 0), 354 new pointf(this.clientrectangle.right, this.height), 355 new pointf(0, this.height) 356 }); 357 path.closeallfigures(); 358 linepath.addline(this.width / 2, 0, this.width / 2, this.height); 359 break; 360 case conduitstyle.vertical_left_none: 361 path.addlines(new pointf[] 362 { 363 new pointf(this.clientrectangle.right, intpenwidth), 364 new pointf(this.clientrectangle.right, this.height), 365 new pointf(0, this.height), 366 new pointf(0, 0) 367 }); 368 path.addarc(new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), 270, 90); 369 path.closeallfigures(); 370 371 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 269, 91); 372 linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height); 373 374 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 375 break; 376 case conduitstyle.vertical_right_none: 377 path.addlines(new pointf[] 378 { 379 new pointf(this.clientrectangle.right, 0), 380 new pointf(this.clientrectangle.right, this.height), 381 new pointf(0, this.height), 382 new pointf(0, intpenwidth) 383 }); 384 path.addarc(new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), 180, 90); 385 path.closeallfigures(); 386 387 linepath.addarc(new rectangle(intpenwidth / 2, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 271, -91); 388 linepath.addline(intpenwidth / 2, intpenwidth + 1, intpenwidth / 2, this.height); 389 390 lstarcs.add(new arcentity() { rect = new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 391 break; 392 case conduitstyle.vertical_none_left: 393 path.addlines(new pointf[] 394 { 395 new pointf(0, this.height), 396 new pointf(0, 0), 397 new pointf(this.clientrectangle.right, 0), 398 new pointf(this.clientrectangle.right, this.height-intpenwidth), 399 }); 400 path.addarc(new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 0, 90); 401 path.closeallfigures(); 402 403 linepath.addline(this.width / 2, 0, this.width / 2, this.height - intpenwidth); 404 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), -1, 91); 405 406 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 407 break; 408 case conduitstyle.vertical_none_right: 409 path.addlines(new pointf[] 410 { 411 new pointf(0, this.height-intpenwidth), 412 new pointf(0, 0), 413 new pointf(this.clientrectangle.right, 0), 414 new pointf(this.clientrectangle.right, this.height), 415 }); 416 path.addarc(new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 90, 90); 417 path.closeallfigures(); 418 419 linepath.addline(this.width / 2, 0, this.width / 2, this.height - intpenwidth - 1); 420 linepath.addarc(new rectangle(intpenwidth / 2, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 421 422 lstarcs.add(new arcentity() { rect = new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 423 break; 424 case conduitstyle.vertical_left_right: 425 path.addline(this.width, intpenwidth, this.width, this.height); 426 path.addarc(new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 90, 90); 427 path.addline(0, this.height - intpenwidth, 0, 0); 428 path.addarc(new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), 270, 90); 429 path.closeallfigures(); 430 431 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 269, 91); 432 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 433 linepath.addarc(new rectangle(intpenwidth / 2, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), 181, -91); 434 435 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 436 lstarcs.add(new arcentity() { rect = new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 437 break; 438 case conduitstyle.vertical_right_left: 439 path.addline(this.width, 0, this.width, this.height - intpenwidth); 440 path.addarc(new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 0, 90); 441 path.addline(0, this.height, 0, intpenwidth); 442 path.addarc(new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), 180, 90); 443 path.closeallfigures(); 444 445 linepath.addarc(new rectangle(intpenwidth / 2, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 271, -91); 446 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 447 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), -1, 91); 448 449 lstarcs.add(new arcentity() { rect = new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 450 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 451 break; 452 case conduitstyle.vertical_left_left: 453 path.addline(this.width, intpenwidth, this.width, this.height - intpenwidth); 454 path.addarc(new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 0, 90); 455 path.addline(0, this.height, 0, 0); 456 path.addarc(new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), 270, 90); 457 path.closeallfigures(); 458 459 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 269, 91); 460 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 461 linepath.addarc(new rectangle(-1 * intpenwidth / 2 - 1, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), -1, 91); 462 463 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, 0, intpenwidth * 2, intpenwidth * 2), startangle = 270, sweepangle = 90 }); 464 lstarcs.add(new arcentity() { rect = new rectangle(-1 * intpenwidth, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 0, sweepangle = 90 }); 465 break; 466 case conduitstyle.vertical_right_right: 467 path.addline(this.width, 0, this.width, this.height); 468 path.addarc(new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), 90, 90); 469 path.addline(0, this.height - intpenwidth, 0, intpenwidth); 470 path.addarc(new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), 180, 90); 471 path.closeallfigures(); 472 473 linepath.addarc(new rectangle(intpenwidth / 2, intpenwidth / 2 + 1, intpenwidth, intpenwidth), 271, -91); 474 //linepath.addline(intpenwidth / 2, intpenwidth, intpenwidth / 2, this.height - intpenwidth); 475 linepath.addarc(new rectangle(intpenwidth / 2, this.height - intpenwidth - intpenwidth / 2 - 1, intpenwidth, intpenwidth), 180, -91); 476 477 lstarcs.add(new arcentity() { rect = new rectangle(-1, 0, intpenwidth * 2, intpenwidth * 2), startangle = 180, sweepangle = 90 }); 478 lstarcs.add(new arcentity() { rect = new rectangle(-1, this.height - intpenwidth * 2, intpenwidth * 2, intpenwidth * 2), startangle = 90, sweepangle = 90 }); 479 break; 480 #endregion 481 } 482 g.fillpath(new solidbrush(conduitcolor), path); 483 484 //渐变色 485 int intcount = intpenwidth / 2 / 4; 486 int intsplit = (255 - 100) / intcount; 487 for (int i = 0; i < intcount; i++) 488 { 489 int _penwidth = intpenwidth / 2 - 4 * i; 490 if (_penwidth <= 0) 491 _penwidth = 1; 492 g.drawpath(new pen(new solidbrush(color.fromargb(40, color.white.r, color.white.g, color.white.b)), _penwidth), linepath); 493 if (_penwidth == 1) 494 break; 495 } 496 497 g.setgdihigh(); 498 //使用抗锯齿画圆角 499 foreach (var item in lstarcs) 500 { 501 g.drawarc(new pen(new solidbrush(this.backcolor)), item.rect, item.startangle, item.sweepangle); 502 } 503 504 //液体流动 505 if (liquiddirection != conduit.liquiddirection.none) 506 { 507 pen p = new pen(new solidbrush(liquidcolor), 4); 508 p.dashpattern = new float[] { 6, 6 }; 509 p.dashoffset = intlineleft * (liquiddirection == conduit.liquiddirection.forward ? -1 : 1); 510 g.drawpath(p, linepath); 511 } 512 } 513 514 515 /// <summary> 516 /// class arcentity. 517 /// </summary> 518 class arcentity 519 { 520 /// <summary> 521 /// gets or sets the rect. 522 /// </summary> 523 /// <value>the rect.</value> 524 public rectangle rect { get; set; } 525 /// <summary> 526 /// gets or sets the start angle. 527 /// </summary> 528 /// <value>the start angle.</value> 529 public float startangle { get; set; } 530 /// <summary> 531 /// gets or sets the sweep angle. 532 /// </summary> 533 /// <value>the sweep angle.</value> 534 public float sweepangle { get; set; } 535 } 536 537 } 538 539 /// <summary> 540 /// enum liquiddirection 541 /// </summary> 542 public enum liquiddirection 543 { 544 /// <summary> 545 /// the none 546 /// </summary> 547 none, 548 /// <summary> 549 /// the forward 550 /// </summary> 551 forward, 552 /// <summary> 553 /// the backward 554 /// </summary> 555 backward 556 } 557 558 /// <summary> 559 /// 管道样式enum conduitstyle 560 /// </summary> 561 public enum conduitstyle 562 { 563 /// <summary> 564 /// 直线 the horizontal none none 565 /// </summary> 566 horizontal_none_none, 567 /// <summary> 568 /// 左上the horizontal up none 569 /// </summary> 570 horizontal_up_none, 571 /// <summary> 572 /// 左下the horizontal down none 573 /// </summary> 574 horizontal_down_none, 575 /// <summary> 576 /// 右上the horizontal none up 577 /// </summary> 578 horizontal_none_up, 579 /// <summary> 580 /// 右下the horizontal none down 581 /// </summary> 582 horizontal_none_down, 583 /// <summary> 584 /// 左下右上the horizontal down up 585 /// </summary> 586 horizontal_down_up, 587 /// <summary> 588 /// 左上右下the horizontal up down 589 /// </summary> 590 horizontal_up_down, 591 /// <summary> 592 /// 左上,右上the horizontal up up 593 /// </summary> 594 horizontal_up_up, 595 /// <summary> 596 /// 左下右下the horizontal down down 597 /// </summary> 598 horizontal_down_down, 599 600 /// <summary> 601 /// 竖线the vertical none none 602 /// </summary> 603 vertical_none_none, 604 /// <summary> 605 /// 上左the vertical left none 606 /// </summary> 607 vertical_left_none, 608 /// <summary> 609 /// 上右the vertical right none 610 /// </summary> 611 vertical_right_none, 612 /// <summary> 613 /// 下左the vertical none left 614 /// </summary> 615 vertical_none_left, 616 /// <summary> 617 /// 下右the vertical none right 618 /// </summary> 619 vertical_none_right, 620 /// <summary> 621 /// 上左下右the vertical left right 622 /// </summary> 623 vertical_left_right, 624 /// <summary> 625 /// 上右下左the vertical right left 626 /// </summary> 627 vertical_right_left, 628 /// <summary> 629 /// 上左下左the vertical left left 630 /// </summary> 631 vertical_left_left, 632 /// <summary> 633 /// 上右下右the vertical right left 634 /// </summary> 635 vertical_right_right, 636 } 637 }
最后的话
如果你喜欢的话,请到 点个星星吧