// *********************************************************************** // Assembly : HZH_Controls // Created : 2019-09-09 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // 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 { /// /// Class UCBlower. /// Implements the /// /// public class UCBlower : UserControl { /// /// The entrance direction /// private BlowerEntranceDirection entranceDirection = BlowerEntranceDirection.None; /// /// Gets or sets the entrance direction. /// /// The entrance direction. [Description("入口方向"), Category("自定义")] public BlowerEntranceDirection EntranceDirection { get { return entranceDirection; } set { entranceDirection = value; Refresh(); } } /// /// The exit direction /// private BlowerExitDirection exitDirection = BlowerExitDirection.Right; /// /// Gets or sets the exit direction. /// /// The exit direction. [Description("出口方向"), Category("自定义")] public BlowerExitDirection ExitDirection { get { return exitDirection; } set { exitDirection = value; Refresh(); } } /// /// The blower color /// private Color blowerColor = Color.FromArgb(255, 77, 59); /// /// Gets or sets the color of the blower. /// /// The color of the blower. [Description("风机颜色"), Category("自定义")] public Color BlowerColor { get { return blowerColor; } set { blowerColor = value; Refresh(); } } /// /// The fan color /// private Color fanColor = Color.FromArgb(3, 169, 243); /// /// Gets or sets the color of the fan. /// /// The color of the fan. [Description("风叶颜色"), Category("自定义")] public Color FanColor { get { return fanColor; } set { fanColor = value; Refresh(); } } /// /// The m rect working /// Rectangle m_rectWorking; /// /// Initializes a new instance of the class. /// 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); } /// /// Handles the SizeChanged event of the UCBlower control. /// /// The source of the event. /// The instance containing the event data. 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)); } /// /// 引发 事件。 /// /// 包含事件数据的 。 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)); } } /// /// Enum BlowerEntranceDirection /// public enum BlowerEntranceDirection { /// /// The none /// None, /// /// The left /// Left, /// /// The right /// Right, /// /// Up /// Up } /// /// Enum BlowerExitDirection /// public enum BlowerExitDirection { /// /// The left /// Left, /// /// The right /// Right, /// /// Up /// Up } }