(六十)c#Winform自定义控件-鼓风机(工业)
2021-02-03 21:15
                         标签: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  麻烦博客下方点个【推荐】,谢谢 https://www.cnblogs.com/bfyx/p/11364884.html 可用作水泵,风机,涡轮等 GDI+画的,不懂的可以自行百度一下 添加2个枚举,分别控制进出风口的位置 属性 重绘 全部代码前提

NuGet
Install-Package HZH_Controls
目录
用处及效果

准备工作
开始
 1   /// 
 1  /// 
  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 // 
上一篇:启动运行发现窗体不能最大化,添加
文章标题:(六十)c#Winform自定义控件-鼓风机(工业)
文章链接:http://soscw.com/index.php/essay/50590.html