欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

(五十八)c#Winform自定义控件-管道阀门(工业)

程序员文章站 2023-11-14 17:31:22
前提 入行已经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

目录

用处及效果

(五十八)c#Winform自定义控件-管道阀门(工业)(五十八)c#Winform自定义控件-管道阀门(工业)

准备工作

请先了解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; }
176             set
177             {
178                 liquidcolor = value;
179                 if (liquiddirection != conduit.liquiddirection.none)
180                     refresh();
181             }
182         }
183         /// <summary>
184         /// the m timer
185         /// </summary>
186         timer m_timer;
187         /// <summary>
188         /// the int line left
189         /// </summary>
190         int intlineleft = 0;

重绘

  1  protected override void onpaint(painteventargs e)
  2         {
  3             base.onpaint(e);
  4             var g = e.graphics;
  5             rectangle rectguan = rectangle.empty;//管道
  6             rectangle rectjk1 = rectangle.empty;//接口1
  7             rectangle rectjk2 = rectangle.empty;//接口2
  8             rectangle rectz = rectangle.empty;//轴
  9             graphicspath linepath = new graphicspath();//管道中心线
 10             graphicspath dzpath = new graphicspath();//轴底座
 11             graphicspath bspath = new graphicspath();//开关把手
 12             switch (valvestyle)
 13             {
 14                 case valvestyle.horizontal_top:
 15                     rectguan = new rectangle(0, this.height / 2, this.width, this.height / 2 - this.height / 8);
 16                     rectjk1 = new rectangle(this.height / 8, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
 17                     rectjk2 = new rectangle(rectguan.right - this.height / 8 - rectguan.height / 2, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
 18                     linepath.addline(new point(rectguan.left - 10, rectguan.top + rectguan.height / 2), new point(rectguan.right + 10, rectguan.top + rectguan.height / 2));
 19                     rectz = new rectangle(rectguan.left + (rectguan.width - rectguan.height / 4) / 2, 10, rectguan.height / 4, rectguan.top - 10);
 20                     point[] pstop = new point[]             
 21                     {
 22                         new point(rectguan.left+(rectguan.width-rectguan.height/2)/2,rectguan.top- this.height / 8- 5 ),
 23                         new point(rectguan.right-(rectguan.width-rectguan.height/2)/2,rectguan.top- this.height / 8- 5 ),
 24                         new point(rectguan.right-(rectguan.width-rectguan.height)/2,rectguan.top+2 ),
 25                         new point(rectguan.left+(rectguan.width-rectguan.height)/2,rectguan.top +2),
 26                     };
 27                     dzpath.addlines(pstop);
 28                     dzpath.closeallfigures();
 29                     if (opened)
 30                     {
 31                         bspath.addline(rectguan.left + (rectguan.width - rectguan.height - 10) / 2, 10 + (rectguan.height / 3) / 2, rectguan.left + (rectguan.width - rectguan.height - 10) / 2 + rectguan.height + 10, 10 + (rectguan.height / 3) / 2);
 32                     }
 33                     else
 34                     {
 35                         bspath.addline(new point(rectguan.left + rectguan.width / 2 - 3, 1), new point(rectguan.left + rectguan.width / 2 + 4, rectguan.height + 10));
 36                     }
 37                     break;
 38                 case valvestyle.horizontal_bottom:
 39                     rectguan = new rectangle(0, this.height / 8, this.width, this.height / 2 - this.height / 8);
 40                     rectjk1 = new rectangle(this.height / 8, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
 41                     rectjk2 = new rectangle(rectguan.right - this.height / 8 - rectguan.height / 2, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
 42                     linepath.addline(new point(rectguan.left - 10, rectguan.top + rectguan.height / 2), new point(rectguan.right + 10, rectguan.top + rectguan.height / 2));
 43                     rectz = new rectangle(rectguan.left + (rectguan.width - rectguan.height / 4) / 2, rectguan.bottom + 10, rectguan.height / 4, this.height - 10 - (rectguan.bottom + 10));
 44                     point[] psbottom = new point[]             
 45                     {
 46                         new point(rectguan.left+(rectguan.width-rectguan.height/2)/2,rectguan.bottom+ this.height / 8+ 5 ),
 47                         new point(rectguan.right-(rectguan.width-rectguan.height/2)/2,rectguan.bottom+ this.height / 8+ 5 ),
 48                         new point(rectguan.right-(rectguan.width-rectguan.height)/2,rectguan.bottom-2 ),
 49                         new point(rectguan.left+(rectguan.width-rectguan.height)/2,rectguan.bottom-2),
 50                     };
 51                     dzpath.addlines(psbottom);
 52                     dzpath.closeallfigures();
 53                     if (opened)
 54                     {
 55                         bspath.addline(rectguan.left + (rectguan.width - rectguan.height - 10) / 2, this.height - (10 + (rectguan.height / 3) / 2), rectguan.left + (rectguan.width - rectguan.height - 10) / 2 + rectguan.height + 10, this.height - (10 + (rectguan.height / 3) / 2));
 56                     }
 57                     else
 58                     {
 59                         bspath.addline(new point(rectguan.left + rectguan.width / 2 - 3, this.height - 1), new point(rectguan.left + rectguan.width / 2 + 4, this.height - (rectguan.height + 10)));
 60                     }
 61                     break;
 62                 case valvestyle.vertical_left:
 63                     rectguan = new rectangle(this.width / 8, 0, this.width / 2 - this.width / 8, this.height);
 64                     rectjk1 = new rectangle(0, this.width / 8, rectguan.width + this.width / 4, rectguan.width / 2);
 65                     rectjk2 = new rectangle(0, this.height - this.width / 8 - rectguan.width / 2, rectguan.width + this.width / 4, rectguan.width / 2);
 66                     linepath.addline(new point(rectguan.left + rectguan.width / 2, rectguan.top - 10), new point(rectguan.left + rectguan.width / 2, rectguan.bottom + 10));
 67                     rectz = new rectangle(rectguan.right, rectguan.top + (rectguan.height - rectguan.width / 4) / 2, rectguan.right - 10, rectguan.width / 4);
 68                     point[] psleft = new point[]             
 69                     {
 70                         new point(rectguan.right+ this.width / 8+ 5 ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2),
 71                         new point(rectguan.right+ this.width / 8+ 5 ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2+rectguan.width / 2),                      
 72                         new point(rectguan.right-2, rectguan.top +(rectguan.height-rectguan.width)/2+rectguan.width),
 73                         new point(rectguan.right-2, rectguan.top +(rectguan.height-rectguan.width)/2),
 74                     };
 75                     dzpath.addlines(psleft);
 76                     dzpath.closeallfigures();
 77                     if (opened)
 78                     {
 79                         bspath.addline(this.width - (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2, this.width - (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2 + rectguan.width + 10);
 80                     }
 81                     else
 82                     {
 83                         bspath.addline(new point(this.width - 1, rectguan.top + rectguan.height / 2 - 3), new point(this.width - (rectguan.width + 10), rectguan.top + rectguan.height / 2 + 4));
 84                     }
 85                     break;
 86                 case valvestyle.vertical_right:
 87                     rectguan = new rectangle(this.width - this.width / 8 - (this.width / 2 - this.width / 8), 0, this.width / 2 - this.width / 8, this.height);
 88                     rectjk1 = new rectangle(this.width - (rectguan.width + this.width / 4), this.width / 8, rectguan.width + this.width / 4, rectguan.width / 2);
 89                     rectjk2 = new rectangle(this.width - (rectguan.width + this.width / 4), this.height - this.width / 8 - rectguan.width / 2, rectguan.width + this.width / 4, rectguan.width / 2);
 90                     linepath.addline(new point(rectguan.left + rectguan.width / 2, rectguan.top - 10), new point(rectguan.left + rectguan.width / 2, rectguan.bottom + 10));
 91                     rectz = new rectangle(10, rectguan.top + (rectguan.height - rectguan.width / 4) / 2, rectguan.left-10, rectguan.width / 4);
 92                     point[] psright = new point[]             
 93                     {
 94                         new point(rectguan.left- (this.width / 8+ 5) ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2),
 95                         new point(rectguan.left-( this.width / 8+ 5) ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2+rectguan.width / 2),                      
 96                         new point(rectguan.left+2, rectguan.top +(rectguan.height-rectguan.width)/2+rectguan.width),
 97                         new point(rectguan.left+2, rectguan.top +(rectguan.height-rectguan.width)/2),
 98                     };
 99                     dzpath.addlines(psright);
100                     dzpath.closeallfigures();
101 
102                     if (opened)
103                     {
104                         bspath.addline( (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2, (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2 + rectguan.width + 10);
105                     }
106                     else
107                     {
108                         bspath.addline(new point( 1, rectguan.top + rectguan.height / 2 - 3), new point( (rectguan.width + 10), rectguan.top + rectguan.height / 2 + 4));
109                     }
110                     break;
111             }
112 
113             //管道
114             g.fillrectangle(new solidbrush(valvecolor), rectguan);
115             //接口
116             g.fillrectangle(new solidbrush(valvecolor), rectjk1);
117             g.fillrectangle(new solidbrush(color.fromargb(40, color.white)), rectjk1);
118             g.fillrectangle(new solidbrush(valvecolor), rectjk2);
119             g.fillrectangle(new solidbrush(color.fromargb(40, color.white)), rectjk2);
120 
121 
122             //高亮
123             int intcount = (valvestyle.tostring().startswith("h") ? rectguan.height : rectguan.width) / 2 / 4;
124             for (int i = 0; i < intcount; i++)
125             {
126                 int _penwidth = (valvestyle.tostring().startswith("h") ? rectguan.height : rectguan.width) / 2 - 4 * i;
127                 if (_penwidth <= 0)
128                     _penwidth = 1;
129                 g.drawpath(new pen(new solidbrush(color.fromargb(40, color.white.r, color.white.g, color.white.b)), _penwidth), linepath);
130                 if (_penwidth == 1)
131                     break;
132             }
133 
134             g.setgdihigh();
135             //轴
136             g.fillrectangle(new solidbrush(axiscolor), rectz);
137 
138             //阀门底座           
139             g.fillpath(new solidbrush(asisbottomcolor), dzpath);
140             g.fillpath(new solidbrush(color.fromargb(50, color.white)), dzpath);
141 
142             //把手
143             g.drawpath(new pen(new solidbrush(switchcolor), (valvestyle.tostring().startswith("h") ? rectguan.height : rectguan.width) / 3), bspath);
144 
145             //液体流动
146             if (opened)
147             {
148                 pen p = new pen(new solidbrush(liquidcolor), 4);
149                 p.dashpattern = new float[] { 6, 6 };
150                 p.dashoffset = intlineleft * (liquiddirection == conduit.liquiddirection.forward ? -1 : 1);
151                 g.drawpath(p, linepath);
152             }
153         }

全部代码

  1 // ***********************************************************************
  2 // assembly         : hzh_controls
  3 // created          : 2019-09-06
  4 //
  5 // ***********************************************************************
  6 // <copyright file="ucvalve.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 hzh_controls.controls.conduit;
 24 using system.componentmodel;
 25 
 26 namespace hzh_controls.controls
 27 {
 28     /// <summary>
 29     /// class ucvalve.
 30     /// implements the <see cref="system.windows.forms.usercontrol" />
 31     /// </summary>
 32     /// <seealso cref="system.windows.forms.usercontrol" />
 33     public class ucvalve : usercontrol
 34     {
 35         /// <summary>
 36         /// the valve style
 37         /// </summary>
 38         private valvestyle valvestyle = valvestyle.horizontal_top;
 39 
 40         /// <summary>
 41         /// gets or sets the valve style.
 42         /// </summary>
 43         /// <value>the valve style.</value>
 44         [description("阀门样式"), category("自定义")]
 45         public valvestyle valvestyle
 46         {
 47             get { return valvestyle; }
 48             set
 49             {
 50                 valvestyle = value;
 51                 refresh();
 52             }
 53         }
 54 
 55         /// <summary>
 56         /// the valve color
 57         /// </summary>
 58         private color valvecolor = color.fromargb(255, 77, 59);
 59 
 60         /// <summary>
 61         /// gets or sets the color of the valve.
 62         /// </summary>
 63         /// <value>the color of the valve.</value>
 64         [description("阀门颜色"), category("自定义")]
 65         public color valvecolor
 66         {
 67             get { return valvecolor; }
 68             set
 69             {
 70                 valvecolor = value;
 71                 refresh();
 72             }
 73         }
 74 
 75         /// <summary>
 76         /// the switch color
 77         /// </summary>
 78         private color switchcolor = color.fromargb(232, 30, 99);
 79 
 80         /// <summary>
 81         /// gets or sets the color of the switch.
 82         /// </summary>
 83         /// <value>the color of the switch.</value>
 84         [description("开关把手颜色"), category("自定义")]
 85         public color switchcolor
 86         {
 87             get { return switchcolor; }
 88             set
 89             {
 90                 switchcolor = value;
 91                 refresh();
 92             }
 93         }
 94 
 95         /// <summary>
 96         /// the axis color
 97         /// </summary>
 98         private color axiscolor = color.fromargb(3, 169, 243);
 99 
100         /// <summary>
101         /// gets or sets the color of the axis.
102         /// </summary>
103         /// <value>the color of the axis.</value>
104         [description("轴颜色"), category("自定义")]
105         public color axiscolor
106         {
107             get { return axiscolor; }
108             set
109             {
110                 axiscolor = value;
111                 refresh();
112             }
113         }
114 
115         /// <summary>
116         /// the asis bottom color
117         /// </summary>
118         private color asisbottomcolor = color.fromargb(3, 169, 243);
119 
120         /// <summary>
121         /// gets or sets the color of the asis bottom.
122         /// </summary>
123         /// <value>the color of the asis bottom.</value>
124         [description("轴底座颜色"), category("自定义")]
125         public color asisbottomcolor
126         {
127             get { return asisbottomcolor; }
128             set { asisbottomcolor = value; }
129         }
130 
131         /// <summary>
132         /// the opened
133         /// </summary>
134         private bool opened = true;
135 
136         /// <summary>
137         /// gets or sets a value indicating whether this <see cref="ucvalve" /> is opened.
138         /// </summary>
139         /// <value><c>true</c> if opened; otherwise, <c>false</c>.</value>
140         [description("是否打开"), category("自定义")]
141         public bool opened
142         {
143             get { return opened; }
144             set
145             {
146                 opened = value;
147                 refresh();
148             }
149         }
150 
151         /// <summary>
152         /// the liquid direction
153         /// </summary>
154         private liquiddirection liquiddirection = liquiddirection.forward;
155 
156         /// <summary>
157         /// gets or sets the liquid direction.
158         /// </summary>
159         /// <value>the liquid direction.</value>
160         [description("液体流动方向"), category("自定义")]
161         public liquiddirection liquiddirection
162         {
163             get { return liquiddirection; }
164             set
165             {
166                 liquiddirection = value;
167                 if (value == conduit.liquiddirection.none)
168                     m_timer.enabled = false;
169                 else
170                     m_timer.enabled = true;
171                 refresh();
172             }
173         }
174 
175         /// <summary>
176         /// the liquid speed
177         /// </summary>
178         private int liquidspeed = 100;
179 
180         /// <summary>
181         /// 液体流速,越小,速度越快gets or sets the liquid speed.
182         /// </summary>
183         /// <value>the liquid speed.</value>
184         [description("液体流速,越小,速度越快"), category("自定义")]
185         public int liquidspeed
186         {
187             get { return liquidspeed; }
188             set
189             {
190                 if (value <= 0)
191                     return;
192                 liquidspeed = value;
193                 m_timer.interval = value;
194             }
195         }
196 
197         /// <summary>
198         /// the liquid color
199         /// </summary>
200         private color liquidcolor = color.fromargb(3, 169, 243);
201 
202         /// <summary>
203         /// gets or sets the color of the liquid.
204         /// </summary>
205         /// <value>the color of the liquid.</value>
206         [description("液体颜色"), category("自定义")]
207         public color liquidcolor
208         {
209             get { return liquidcolor; }
210             set
211             {
212                 liquidcolor = value;
213                 if (liquiddirection != conduit.liquiddirection.none)
214                     refresh();
215             }
216         }
217         /// <summary>
218         /// the m timer
219         /// </summary>
220         timer m_timer;
221         /// <summary>
222         /// the int line left
223         /// </summary>
224         int intlineleft = 0;
225 
226         /// <summary>
227         /// initializes a new instance of the <see cref="ucvalve" /> class.
228         /// </summary>
229         public ucvalve()
230         {
231             this.setstyle(controlstyles.allpaintinginwmpaint, true);
232             this.setstyle(controlstyles.doublebuffer, true);
233             this.setstyle(controlstyles.resizeredraw, true);
234             this.setstyle(controlstyles.selectable, true);
235             this.setstyle(controlstyles.supportstransparentbackcolor, true);
236             this.setstyle(controlstyles.userpaint, true);
237             this.autoscalemode = system.windows.forms.autoscalemode.none;
238             this.size = new size(120, 100);
239             m_timer = new timer();
240             m_timer.interval = 100;
241             m_timer.tick += timer_tick;
242             m_timer.enabled = true;
243         }
244 
245         /// <summary>
246         /// handles the tick event of the timer control.
247         /// </summary>
248         /// <param name="sender">the source of the event.</param>
249         /// <param name="e">the <see cref="eventargs" /> instance containing the event data.</param>
250         void timer_tick(object sender, eventargs e)
251         {
252             intlineleft += 2;
253             if (intlineleft > 12)
254                 intlineleft = 0;
255             refresh();
256         }
257 
258 
259         /// <summary>
260         /// 引发 <see cref="e:system.windows.forms.control.paint" /> 事件。
261         /// </summary>
262         /// <param name="e">包含事件数据的 <see cref="t:system.windows.forms.painteventargs" />。</param>
263         protected override void onpaint(painteventargs e)
264         {
265             base.onpaint(e);
266             var g = e.graphics;
267             rectangle rectguan = rectangle.empty;//管道
268             rectangle rectjk1 = rectangle.empty;//接口1
269             rectangle rectjk2 = rectangle.empty;//接口2
270             rectangle rectz = rectangle.empty;//轴
271             graphicspath linepath = new graphicspath();//管道中心线
272             graphicspath dzpath = new graphicspath();//轴底座
273             graphicspath bspath = new graphicspath();//开关把手
274             switch (valvestyle)
275             {
276                 case valvestyle.horizontal_top:
277                     rectguan = new rectangle(0, this.height / 2, this.width, this.height / 2 - this.height / 8);
278                     rectjk1 = new rectangle(this.height / 8, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
279                     rectjk2 = new rectangle(rectguan.right - this.height / 8 - rectguan.height / 2, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
280                     linepath.addline(new point(rectguan.left - 10, rectguan.top + rectguan.height / 2), new point(rectguan.right + 10, rectguan.top + rectguan.height / 2));
281                     rectz = new rectangle(rectguan.left + (rectguan.width - rectguan.height / 4) / 2, 10, rectguan.height / 4, rectguan.top - 10);
282                     point[] pstop = new point[]             
283                     {
284                         new point(rectguan.left+(rectguan.width-rectguan.height/2)/2,rectguan.top- this.height / 8- 5 ),
285                         new point(rectguan.right-(rectguan.width-rectguan.height/2)/2,rectguan.top- this.height / 8- 5 ),
286                         new point(rectguan.right-(rectguan.width-rectguan.height)/2,rectguan.top+2 ),
287                         new point(rectguan.left+(rectguan.width-rectguan.height)/2,rectguan.top +2),
288                     };
289                     dzpath.addlines(pstop);
290                     dzpath.closeallfigures();
291                     if (opened)
292                     {
293                         bspath.addline(rectguan.left + (rectguan.width - rectguan.height - 10) / 2, 10 + (rectguan.height / 3) / 2, rectguan.left + (rectguan.width - rectguan.height - 10) / 2 + rectguan.height + 10, 10 + (rectguan.height / 3) / 2);
294                     }
295                     else
296                     {
297                         bspath.addline(new point(rectguan.left + rectguan.width / 2 - 3, 1), new point(rectguan.left + rectguan.width / 2 + 4, rectguan.height + 10));
298                     }
299                     break;
300                 case valvestyle.horizontal_bottom:
301                     rectguan = new rectangle(0, this.height / 8, this.width, this.height / 2 - this.height / 8);
302                     rectjk1 = new rectangle(this.height / 8, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
303                     rectjk2 = new rectangle(rectguan.right - this.height / 8 - rectguan.height / 2, rectguan.top - this.height / 8, rectguan.height / 2, rectguan.height + this.height / 4);
304                     linepath.addline(new point(rectguan.left - 10, rectguan.top + rectguan.height / 2), new point(rectguan.right + 10, rectguan.top + rectguan.height / 2));
305                     rectz = new rectangle(rectguan.left + (rectguan.width - rectguan.height / 4) / 2, rectguan.bottom + 10, rectguan.height / 4, this.height - 10 - (rectguan.bottom + 10));
306                     point[] psbottom = new point[]             
307                     {
308                         new point(rectguan.left+(rectguan.width-rectguan.height/2)/2,rectguan.bottom+ this.height / 8+ 5 ),
309                         new point(rectguan.right-(rectguan.width-rectguan.height/2)/2,rectguan.bottom+ this.height / 8+ 5 ),
310                         new point(rectguan.right-(rectguan.width-rectguan.height)/2,rectguan.bottom-2 ),
311                         new point(rectguan.left+(rectguan.width-rectguan.height)/2,rectguan.bottom-2),
312                     };
313                     dzpath.addlines(psbottom);
314                     dzpath.closeallfigures();
315                     if (opened)
316                     {
317                         bspath.addline(rectguan.left + (rectguan.width - rectguan.height - 10) / 2, this.height - (10 + (rectguan.height / 3) / 2), rectguan.left + (rectguan.width - rectguan.height - 10) / 2 + rectguan.height + 10, this.height - (10 + (rectguan.height / 3) / 2));
318                     }
319                     else
320                     {
321                         bspath.addline(new point(rectguan.left + rectguan.width / 2 - 3, this.height - 1), new point(rectguan.left + rectguan.width / 2 + 4, this.height - (rectguan.height + 10)));
322                     }
323                     break;
324                 case valvestyle.vertical_left:
325                     rectguan = new rectangle(this.width / 8, 0, this.width / 2 - this.width / 8, this.height);
326                     rectjk1 = new rectangle(0, this.width / 8, rectguan.width + this.width / 4, rectguan.width / 2);
327                     rectjk2 = new rectangle(0, this.height - this.width / 8 - rectguan.width / 2, rectguan.width + this.width / 4, rectguan.width / 2);
328                     linepath.addline(new point(rectguan.left + rectguan.width / 2, rectguan.top - 10), new point(rectguan.left + rectguan.width / 2, rectguan.bottom + 10));
329                     rectz = new rectangle(rectguan.right, rectguan.top + (rectguan.height - rectguan.width / 4) / 2, rectguan.right - 10, rectguan.width / 4);
330                     point[] psleft = new point[]             
331                     {
332                         new point(rectguan.right+ this.width / 8+ 5 ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2),
333                         new point(rectguan.right+ this.width / 8+ 5 ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2+rectguan.width / 2),                      
334                         new point(rectguan.right-2, rectguan.top +(rectguan.height-rectguan.width)/2+rectguan.width),
335                         new point(rectguan.right-2, rectguan.top +(rectguan.height-rectguan.width)/2),
336                     };
337                     dzpath.addlines(psleft);
338                     dzpath.closeallfigures();
339                     if (opened)
340                     {
341                         bspath.addline(this.width - (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2, this.width - (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2 + rectguan.width + 10);
342                     }
343                     else
344                     {
345                         bspath.addline(new point(this.width - 1, rectguan.top + rectguan.height / 2 - 3), new point(this.width - (rectguan.width + 10), rectguan.top + rectguan.height / 2 + 4));
346                     }
347                     break;
348                 case valvestyle.vertical_right:
349                     rectguan = new rectangle(this.width - this.width / 8 - (this.width / 2 - this.width / 8), 0, this.width / 2 - this.width / 8, this.height);
350                     rectjk1 = new rectangle(this.width - (rectguan.width + this.width / 4), this.width / 8, rectguan.width + this.width / 4, rectguan.width / 2);
351                     rectjk2 = new rectangle(this.width - (rectguan.width + this.width / 4), this.height - this.width / 8 - rectguan.width / 2, rectguan.width + this.width / 4, rectguan.width / 2);
352                     linepath.addline(new point(rectguan.left + rectguan.width / 2, rectguan.top - 10), new point(rectguan.left + rectguan.width / 2, rectguan.bottom + 10));
353                     rectz = new rectangle(10, rectguan.top + (rectguan.height - rectguan.width / 4) / 2, rectguan.left-10, rectguan.width / 4);
354                     point[] psright = new point[]             
355                     {
356                         new point(rectguan.left- (this.width / 8+ 5) ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2),
357                         new point(rectguan.left-( this.width / 8+ 5) ,rectguan.top + (rectguan.height - rectguan.width / 2) / 2+rectguan.width / 2),                      
358                         new point(rectguan.left+2, rectguan.top +(rectguan.height-rectguan.width)/2+rectguan.width),
359                         new point(rectguan.left+2, rectguan.top +(rectguan.height-rectguan.width)/2),
360                     };
361                     dzpath.addlines(psright);
362                     dzpath.closeallfigures();
363 
364                     if (opened)
365                     {
366                         bspath.addline( (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2, (10 + (rectguan.width / 3) / 2), rectguan.top + (rectguan.height - rectguan.width - 10) / 2 + rectguan.width + 10);
367                     }
368                     else
369                     {
370                         bspath.addline(new point( 1, rectguan.top + rectguan.height / 2 - 3), new point( (rectguan.width + 10), rectguan.top + rectguan.height / 2 + 4));
371                     }
372                     break;
373             }
374 
375             //管道
376             g.fillrectangle(new solidbrush(valvecolor), rectguan);
377             //接口
378             g.fillrectangle(new solidbrush(valvecolor), rectjk1);
379             g.fillrectangle(new solidbrush(color.fromargb(40, color.white)), rectjk1);
380             g.fillrectangle(new solidbrush(valvecolor), rectjk2);
381             g.fillrectangle(new solidbrush(color.fromargb(40, color.white)), rectjk2);
382 
383 
384             //高亮
385             int intcount = (valvestyle.tostring().startswith("h") ? rectguan.height : rectguan.width) / 2 / 4;
386             for (int i = 0; i < intcount; i++)
387             {
388                 int _penwidth = (valvestyle.tostring().startswith("h") ? rectguan.height : rectguan.width) / 2 - 4 * i;
389                 if (_penwidth <= 0)
390                     _penwidth = 1;
391                 g.drawpath(new pen(new solidbrush(color.fromargb(40, color.white.r, color.white.g, color.white.b)), _penwidth), linepath);
392                 if (_penwidth == 1)
393                     break;
394             }
395 
396             g.setgdihigh();
397             //轴
398             g.fillrectangle(new solidbrush(axiscolor), rectz);
399 
400             //阀门底座           
401             g.fillpath(new solidbrush(asisbottomcolor), dzpath);
402             g.fillpath(new solidbrush(color.fromargb(50, color.white)), dzpath);
403 
404             //把手
405             g.drawpath(new pen(new solidbrush(switchcolor), (valvestyle.tostring().startswith("h") ? rectguan.height : rectguan.width) / 3), bspath);
406 
407             //液体流动
408             if (opened)
409             {
410                 pen p = new pen(new solidbrush(liquidcolor), 4);
411                 p.dashpattern = new float[] { 6, 6 };
412                 p.dashoffset = intlineleft * (liquiddirection == conduit.liquiddirection.forward ? -1 : 1);
413                 g.drawpath(p, linepath);
414             }
415         }
416     }
417 
418     /// <summary>
419     /// enum valvestyle
420     /// </summary>
421     public enum valvestyle
422     {
423         /// <summary>
424         /// 横向,开关在上方
425         /// </summary>
426         horizontal_top,
427         /// <summary>
428         /// 横向,开关在下方
429         /// </summary>
430         horizontal_bottom,
431         /// <summary>
432         /// 纵向,开关在左侧
433         /// </summary>
434         vertical_left,
435         /// <summary>
436         /// 纵向,开关在右侧
437         /// </summary>
438         vertical_right,
439     }
440 }

 

最后的话

如果你喜欢的话,请到  点个星星吧