(六十)c#Winform自定义控件-鼓风机(工业)

2021-02-03 21:15

阅读:549

标签:nta   res   hid   i++   pack   ack   please   eve   ***   

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 技术图片

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

可用作水泵,风机,涡轮等

技术图片

准备工作

GDI+画的,不懂的可以自行百度一下

开始

添加2个枚举,分别控制进出风口的位置

 1   /// 
 2     /// Enum BlowerEntranceDirection
 3     /// 
 4     public enum BlowerEntranceDirection
 5     {
 6         /// 
 7         /// The none
 8         /// 
 9         None,
10         /// 
11         /// The left
12         /// 
13         Left,
14         /// 
15         /// The right
16         /// 
17         Right,
18         /// 
19         /// Up
20         /// 
21         Up
22     }
23 
24     /// 
25     /// Enum BlowerExitDirection
26     /// 
27     public enum BlowerExitDirection
28     {
29         /// 
30         /// The left
31         /// 
32         Left,
33         /// 
34         /// The right
35         /// 
36         Right,
37         /// 
38         /// Up
39         /// 
40         Up
41     }

属性

 1  /// 
 2         /// The entrance direction
 3         /// 
 4         private BlowerEntranceDirection entranceDirection = BlowerEntranceDirection.None;
 5 
 6         /// 
 7         /// Gets or sets the entrance direction.
 8         /// 
 9         /// The entrance direction.
10         [Description("入口方向"), Category("自定义")]
11         public BlowerEntranceDirection EntranceDirection
12         {
13             get { return entranceDirection; }
14             set
15             {
16                 entranceDirection = value;
17                 Refresh();
18             }
19         }
20 
21         /// 
22         /// The exit direction
23         /// 
24         private BlowerExitDirection exitDirection = BlowerExitDirection.Right;
25 
26         /// 
27         /// Gets or sets the exit direction.
28         /// 
29         /// The exit direction.
30         [Description("出口方向"), Category("自定义")]
31         public BlowerExitDirection ExitDirection
32         {
33             get { return exitDirection; }
34             set
35             {
36                 exitDirection = value;
37                 Refresh();
38             }
39         }
40 
41         /// 
42         /// The blower color
43         /// 
44         private Color blowerColor = Color.FromArgb(255, 77, 59);
45 
46         /// 
47         /// Gets or sets the color of the blower.
48         /// 
49         /// The color of the blower.
50         [Description("风机颜色"), Category("自定义")]
51         public Color BlowerColor
52         {
53             get { return blowerColor; }
54             set
55             {
56                 blowerColor = value;
57                 Refresh();
58             }
59         }
60 
61         /// 
62         /// The fan color
63         /// 
64         private Color fanColor = Color.FromArgb(3, 169, 243);
65 
66         /// 
67         /// Gets or sets the color of the fan.
68         /// 
69         /// The color of the fan.
70         [Description("风叶颜色"), Category("自定义")]
71         public Color FanColor
72         {
73             get { return fanColor; }
74             set
75             {
76                 fanColor = value;
77                 Refresh();
78             }
79         }
80 
81         /// 
82         /// The m rect working
83         /// 
84         Rectangle m_rectWorking;

重绘

  1 protected override void OnPaint(PaintEventArgs e)
  2         {
  3             base.OnPaint(e);
  4             var g = e.Graphics;
  5             g.SetGDIHigh();
  6             GraphicsPath pathLineIn = new GraphicsPath();
  7             GraphicsPath pathLineOut = new GraphicsPath();
  8             int intLinePenWidth = 0;
  9 
 10             switch (exitDirection)
 11             {
 12                 case BlowerExitDirection.Left:
 13                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
 14                     intLinePenWidth = m_rectWorking.Height / 2 - 5;
 15                     pathLineOut.AddLine(new Point(-10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
 16                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Top - 2), new Point(1, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
 17                     break;
 18                 case BlowerExitDirection.Right:
 19                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
 20                     intLinePenWidth = m_rectWorking.Height / 2 - 5;
 21                     pathLineOut.AddLine(new Point(this.Width + 10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
 22                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(this.Width - 2, m_rectWorking.Top - 2), new Point(this.Width - 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
 23                     break;
 24                 case BlowerExitDirection.Up:
 25                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5), 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
 26                     intLinePenWidth = m_rectWorking.Width / 2 - 5;
 27                     pathLineOut.AddLine(new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, -10), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, m_rectWorking.Top + m_rectWorking.Height / 2));
 28                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Right + 2, 1), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) - 2, 1));
 29                     break;
 30             }
 31 
 32             switch (entranceDirection)
 33             {
 34                 case BlowerEntranceDirection.Left:
 35                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
 36                     pathLineIn.AddLine(new Point(-10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2));
 37                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 - 2), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) + 2));
 38                     break;
 39                 case BlowerEntranceDirection.Right:
 40                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
 41                     pathLineIn.AddLine(new Point(this.Width + 10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2));
 42                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(this.Width - 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 - 2), new Point(this.Width - 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) + 2));
 43                     break;
 44                 case BlowerEntranceDirection.Up:
 45                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Left, 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
 46                     pathLineIn.AddLine(new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) / 2, -10), new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) / 2, m_rectWorking.Top + m_rectWorking.Height / 2));
 47                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Left - 2, 1), new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) + 2, 1));
 48                     break;
 49             }
 50 
 51             //渐变色
 52             int _intPenWidth = intLinePenWidth;
 53             int intCount = _intPenWidth / 2 / 4;
 54             for (int i = 0; i )
 55             {
 56                 int _penWidth = _intPenWidth / 2 - 4 * i;
 57                 if (_penWidth 0)
 58                     _penWidth = 1;
 59                 if (entranceDirection != BlowerEntranceDirection.None)
 60                     g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineIn);
 61                 g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineOut);
 62                 if (_penWidth == 1)
 63                     break;
 64             }
 65 
 66             //底座
 67             GraphicsPath gpDZ = new GraphicsPath();
 68             gpDZ.AddLines(new Point[] 
 69             {
 70                 new Point( m_rectWorking.Left+m_rectWorking.Width/2,m_rectWorking.Top+m_rectWorking.Height/2),
 71                 new Point(m_rectWorking.Left+2,this.Height),
 72                 new Point(m_rectWorking.Right-2,this.Height)
 73             });
 74             gpDZ.CloseAllFigures();
 75             g.FillPath(new SolidBrush(blowerColor), gpDZ);
 76             g.FillPath(new SolidBrush(Color.FromArgb(50, Color.White)), gpDZ);
 77             g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Left, this.Height - 2), new Point(m_rectWorking.Right, this.Height - 2));
 78 
 79             //中心
 80             g.FillEllipse(new SolidBrush(blowerColor), m_rectWorking);
 81             g.FillEllipse(new SolidBrush(Color.FromArgb(20, Color.White)), m_rectWorking);
 82 
 83 
 84             //扇叶
 85             Rectangle _rect = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - (m_rectWorking.Width / 3 * 2)) / 2, m_rectWorking.Top + (m_rectWorking.Height - (m_rectWorking.Width / 3 * 2)) / 2, (m_rectWorking.Width / 3 * 2), (m_rectWorking.Width / 3 * 2));
 86 
 87             int _splitCount = 8;
 88             float fltSplitValue = 360F / (float)_splitCount;
 89             for (int i = 0; i )
 90             {
 91                 float fltAngle = (fltSplitValue * i - 180) % 360;
 92                 float fltY1 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
 93                 float fltX1 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
 94                 float fltY2 = 0;
 95                 float fltX2 = 0;
 96 
 97                 fltY2 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 4) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
 98                 fltX2 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 4) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
 99 
100                 g.DrawLine(new Pen(new SolidBrush(fanColor), 2), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
101             }
102 
103             g.FillEllipse(new SolidBrush(fanColor), new Rectangle(_rect.Left + _rect.Width / 2 - _rect.Width / 4 + 2, _rect.Top + _rect.Width / 2 - _rect.Width / 4 + 2, _rect.Width / 2 - 4, _rect.Width / 2 - 4));
104             g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), new Rectangle(_rect.Left - 5, _rect.Top - 5, _rect.Width + 10, _rect.Height + 10));
105         }

全部代码

技术图片技术图片
  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-09
  4 //
  5 // ***********************************************************************
  6 //   7 //     Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8 // 
  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
 26 {
 27     /// 
 28     /// Class UCBlower.
 29     /// Implements the 
 30     /// 
 31     /// 
 32     public class UCBlower : UserControl
 33     {
 34         /// 
 35         /// The entrance direction
 36         /// 
 37         private BlowerEntranceDirection entranceDirection = BlowerEntranceDirection.None;
 38 
 39         /// 
 40         /// Gets or sets the entrance direction.
 41         /// 
 42         /// The entrance direction.
 43         [Description("入口方向"), Category("自定义")]
 44         public BlowerEntranceDirection EntranceDirection
 45         {
 46             get { return entranceDirection; }
 47             set
 48             {
 49                 entranceDirection = value;
 50                 Refresh();
 51             }
 52         }
 53 
 54         /// 
 55         /// The exit direction
 56         /// 
 57         private BlowerExitDirection exitDirection = BlowerExitDirection.Right;
 58 
 59         /// 
 60         /// Gets or sets the exit direction.
 61         /// 
 62         /// The exit direction.
 63         [Description("出口方向"), Category("自定义")]
 64         public BlowerExitDirection ExitDirection
 65         {
 66             get { return exitDirection; }
 67             set
 68             {
 69                 exitDirection = value;
 70                 Refresh();
 71             }
 72         }
 73 
 74         /// 
 75         /// The blower color
 76         /// 
 77         private Color blowerColor = Color.FromArgb(255, 77, 59);
 78 
 79         /// 
 80         /// Gets or sets the color of the blower.
 81         /// 
 82         /// The color of the blower.
 83         [Description("风机颜色"), Category("自定义")]
 84         public Color BlowerColor
 85         {
 86             get { return blowerColor; }
 87             set
 88             {
 89                 blowerColor = value;
 90                 Refresh();
 91             }
 92         }
 93 
 94         /// 
 95         /// The fan color
 96         /// 
 97         private Color fanColor = Color.FromArgb(3, 169, 243);
 98 
 99         /// 
100         /// Gets or sets the color of the fan.
101         /// 
102         /// The color of the fan.
103         [Description("风叶颜色"), Category("自定义")]
104         public Color FanColor
105         {
106             get { return fanColor; }
107             set
108             {
109                 fanColor = value;
110                 Refresh();
111             }
112         }
113 
114         /// 
115         /// The m rect working
116         /// 
117         Rectangle m_rectWorking;
118 
119         /// 
120         /// Initializes a new instance of the  class.
121         /// 
122         public UCBlower()
123         {
124             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
125             this.SetStyle(ControlStyles.DoubleBuffer, true);
126             this.SetStyle(ControlStyles.ResizeRedraw, true);
127             this.SetStyle(ControlStyles.Selectable, true);
128             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
129             this.SetStyle(ControlStyles.UserPaint, true);
130             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
131             this.SizeChanged += UCBlower_SizeChanged;
132             this.Size = new Size(120, 120);
133 
134         }
135 
136         /// 
137         /// Handles the SizeChanged event of the UCBlower control.
138         /// 
139         /// The source of the event.
140         /// The  instance containing the event data.
141         void UCBlower_SizeChanged(object sender, EventArgs e)
142         {
143             int intMin = Math.Min(this.Width, this.Height);
144             m_rectWorking = new Rectangle((this.Width - (intMin / 3 * 2)) / 2, (this.Height - (intMin / 3 * 2)) / 2, (intMin / 3 * 2), (intMin / 3 * 2));
145         }
146 
147         /// 
148         /// 引发  事件。
149         /// 
150         /// 包含事件数据的 
151         protected override void OnPaint(PaintEventArgs e)
152         {
153             base.OnPaint(e);
154             var g = e.Graphics;
155             g.SetGDIHigh();
156             GraphicsPath pathLineIn = new GraphicsPath();
157             GraphicsPath pathLineOut = new GraphicsPath();
158             int intLinePenWidth = 0;
159 
160             switch (exitDirection)
161             {
162                 case BlowerExitDirection.Left:
163                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
164                     intLinePenWidth = m_rectWorking.Height / 2 - 5;
165                     pathLineOut.AddLine(new Point(-10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
166                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Top - 2), new Point(1, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
167                     break;
168                 case BlowerExitDirection.Right:
169                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
170                     intLinePenWidth = m_rectWorking.Height / 2 - 5;
171                     pathLineOut.AddLine(new Point(this.Width + 10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
172                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(this.Width - 2, m_rectWorking.Top - 2), new Point(this.Width - 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
173                     break;
174                 case BlowerExitDirection.Up:
175                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5), 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
176                     intLinePenWidth = m_rectWorking.Width / 2 - 5;
177                     pathLineOut.AddLine(new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, -10), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, m_rectWorking.Top + m_rectWorking.Height / 2));
178                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Right + 2, 1), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) - 2, 1));
179                     break;
180             }
181 
182             switch (entranceDirection)
183             {
184                 case BlowerEntranceDirection.Left:
185                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
186                     pathLineIn.AddLine(new Point(-10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2));
187                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 - 2), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) + 2));
188                     break;
189                 case BlowerEntranceDirection.Right:
190                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
191                     pathLineIn.AddLine(new Point(this.Width + 10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(


评论


亲,登录后才可以留言!