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

前提

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

官网:https://www.hzhcontrols.cn

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

码云:HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~

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

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

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

NuGet

Install-Package HZH_Controls

目录

c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客

用处及效果

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

准备工作

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

开始

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

复制代码

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

复制代码

属性

复制代码

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

复制代码

全部代码

// ***********************************************************************
// Assembly         : HZH_Controls
// Created          : 2019-09-09
//
// ***********************************************************************
// <copyright file="UCBlower.cs">
//     Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace HZH_Controls.Controls
{
    /// <summary>
    /// Class UCBlower.
    /// Implements the <see cref="System.Windows.Forms.UserControl" />
    /// </summary>
    /// <seealso cref="System.Windows.Forms.UserControl" />
    public class UCBlower : UserControl
    {
        /// <summary>
        /// The entrance direction
        /// </summary>
        private BlowerEntranceDirection entranceDirection = BlowerEntranceDirection.None;

        /// <summary>
        /// Gets or sets the entrance direction.
        /// </summary>
        /// <value>The entrance direction.</value>
        [Description("入口方向"), Category("自定义")]
        public BlowerEntranceDirection EntranceDirection
        {
            get { return entranceDirection; }
            set
            {
                entranceDirection = value;
                Refresh();
            }
        }

        /// <summary>
        /// The exit direction
        /// </summary>
        private BlowerExitDirection exitDirection = BlowerExitDirection.Right;

        /// <summary>
        /// Gets or sets the exit direction.
        /// </summary>
        /// <value>The exit direction.</value>
        [Description("出口方向"), Category("自定义")]
        public BlowerExitDirection ExitDirection
        {
            get { return exitDirection; }
            set
            {
                exitDirection = value;
                Refresh();
            }
        }

        /// <summary>
        /// The blower color
        /// </summary>
        private Color blowerColor = Color.FromArgb(255, 77, 59);

        /// <summary>
        /// Gets or sets the color of the blower.
        /// </summary>
        /// <value>The color of the blower.</value>
        [Description("风机颜色"), Category("自定义")]
        public Color BlowerColor
        {
            get { return blowerColor; }
            set
            {
                blowerColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// The fan color
        /// </summary>
        private Color fanColor = Color.FromArgb(3, 169, 243);

        /// <summary>
        /// Gets or sets the color of the fan.
        /// </summary>
        /// <value>The color of the fan.</value>
        [Description("风叶颜色"), Category("自定义")]
        public Color FanColor
        {
            get { return fanColor; }
            set
            {
                fanColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// The m rect working
        /// </summary>
        Rectangle m_rectWorking;

        /// <summary>
        /// Initializes a new instance of the <see cref="UCBlower"/> class.
        /// </summary>
        public UCBlower()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.SizeChanged += UCBlower_SizeChanged;
            this.Size = new Size(120, 120);

        }

        /// <summary>
        /// Handles the SizeChanged event of the UCBlower control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void UCBlower_SizeChanged(object sender, EventArgs e)
        {
            int intMin = Math.Min(this.Width, this.Height);
            m_rectWorking = new Rectangle((this.Width - (intMin / 3 * 2)) / 2, (this.Height - (intMin / 3 * 2)) / 2, (intMin / 3 * 2), (intMin / 3 * 2));
        }

        /// <summary>
        /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SetGDIHigh();
            GraphicsPath pathLineIn = new GraphicsPath();
            GraphicsPath pathLineOut = new GraphicsPath();
            int intLinePenWidth = 0;

            switch (exitDirection)
            {
                case BlowerExitDirection.Left:
                    g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
                    intLinePenWidth = m_rectWorking.Height / 2 - 5;
                    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));
                    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));
                    break;
                case BlowerExitDirection.Right:
                    g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
                    intLinePenWidth = m_rectWorking.Height / 2 - 5;
                    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));
                    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));
                    break;
                case BlowerExitDirection.Up:
                    g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5), 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
                    intLinePenWidth = m_rectWorking.Width / 2 - 5;
                    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));
                    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));
                    break;
            }

            switch (entranceDirection)
            {
                case BlowerEntranceDirection.Left:
                    g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
                    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));
                    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));
                    break;
                case BlowerEntranceDirection.Right:
                    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));
                    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));
                    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));
                    break;
                case BlowerEntranceDirection.Up:
                    g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Left, 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
                    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));
                    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));
                    break;
            }

            //渐变色
            int _intPenWidth = intLinePenWidth;
            int intCount = _intPenWidth / 2 / 4;
            for (int i = 0; i < intCount; i++)
            {
                int _penWidth = _intPenWidth / 2 - 4 * i;
                if (_penWidth <= 0)
                    _penWidth = 1;
                if (entranceDirection != BlowerEntranceDirection.None)
                    g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineIn);
                g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineOut);
                if (_penWidth == 1)
                    break;
            }

            //底座
            GraphicsPath gpDZ = new GraphicsPath();
            gpDZ.AddLines(new Point[] 
            {
                new Point( m_rectWorking.Left+m_rectWorking.Width/2,m_rectWorking.Top+m_rectWorking.Height/2),
                new Point(m_rectWorking.Left+2,this.Height),
                new Point(m_rectWorking.Right-2,this.Height)
            });
            gpDZ.CloseAllFigures();
            g.FillPath(new SolidBrush(blowerColor), gpDZ);
            g.FillPath(new SolidBrush(Color.FromArgb(50, Color.White)), gpDZ);
            g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Left, this.Height - 2), new Point(m_rectWorking.Right, this.Height - 2));

            //中心
            g.FillEllipse(new SolidBrush(blowerColor), m_rectWorking);
            g.FillEllipse(new SolidBrush(Color.FromArgb(20, Color.White)), m_rectWorking);


            //扇叶
            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));

            int _splitCount = 8;
            float fltSplitValue = 360F / (float)_splitCount;
            for (int i = 0; i <= _splitCount; i++)
            {
                float fltAngle = (fltSplitValue * i - 180) % 360;
                float fltY1 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                float fltX1 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                float fltY2 = 0;
                float fltX2 = 0;

                fltY2 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 4) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                fltX2 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 4) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));

                g.DrawLine(new Pen(new SolidBrush(fanColor), 2), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
            }

            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));
            g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), new Rectangle(_rect.Left - 5, _rect.Top - 5, _rect.Width + 10, _rect.Height + 10));
        }
    }
    /// <summary>
    /// Enum BlowerEntranceDirection
    /// </summary>
    public enum BlowerEntranceDirection
    {
        /// <summary>
        /// The none
        /// </summary>
        None,
        /// <summary>
        /// The left
        /// </summary>
        Left,
        /// <summary>
        /// The right
        /// </summary>
        Right,
        /// <summary>
        /// Up
        /// </summary>
        Up
    }

    /// <summary>
    /// Enum BlowerExitDirection
    /// </summary>
    public enum BlowerExitDirection
    {
        /// <summary>
        /// The left
        /// </summary>
        Left,
        /// <summary>
        /// The right
        /// </summary>
        Right,
        /// <summary>
        /// Up
        /// </summary>
        Up
    }
}

添加一个类UCBlower ,继承UserControl

最后的话

如果你喜欢的话,请到 HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~ 点个星星吧

WinForms中的自定义控件开发允许开发者创建具有特定功能的控件,这些控件可以用于多种应用程序。仪表盘控件是一种常见的自定义控件,它模拟物理仪表盘,用于显示从简单到复杂的各种数据。在C#中开发WinForms仪表盘控件通常涉及以下几个步骤: 1. **创建控件类**:首先,你需要继承自`UserControl`类,创建一个新的类,这个类将作为基础来定义你的仪表盘控件。 2. **设计界面**:在类中,使用设计器或代码来绘制控件的用户界面。这可能包括刻度、指针和数据标签等元素。 3. **编写业务逻辑**:实现控件的数据绑定和逻辑处理,这可能包括如何读取和显示数据,以及如何响应用户的交互。 4. **属性和事件**:定义公共属性来获取和设置控件的外观和行为,如颜色、范围等,并且创建事件以允许外部代码响应特定的用户操作,如值改变等。 5. **测试和调试**:在完成开发后,需要对控件进行彻底的测试,确保在不同情况下都能正确工作。 以下是一个简单的示例代码,展示了如何创建一个基础的仪表盘控件: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class DashboardControl : UserControl { private float value = 0; // 仪表盘的当前值 // 公共属性,允许外部设置仪表盘值 public float Value { get { return value; } set { if (value != this.value) { this.value = value; Invalidate(); // 重绘控件 } } } // 绘制控件 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; // 绘制仪表盘的刻度和指针等 // ... } // 其他方法,如响应用户事件等 } ``` 开发自定义控件是一个复杂的过程,它需要深入了解WinForms框架、GDI+绘图以及可能涉及的动画和数据绑定技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值