using JinYuan.ControlLib.UCHelper; using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace JinYuan.ControlLib { public partial 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; Invalidate(); } } /// /// 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; Invalidate(); } } /// /// 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; Invalidate(); } } /// /// 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; Invalidate(); } } TurnAround turnAround = JinYuan.ControlLib.TurnAround.None; [Description("风叶旋转方向,None表示不旋转"), Category("自定义")] public TurnAround TurnAround { get { return turnAround; } set { turnAround = value; if (value == JinYuan.ControlLib.TurnAround.None) { updateTimer.Enabled = false; jiaodu = 0; Invalidate(); } else updateTimer.Enabled = true; } } //[Description("风叶旋转速度,100-1000,值越小 速度越快"), Category("自定义")] //public int TurnSpeed //{ // get { return turnSpeed; } // set // { // if (value < 0 || value > 1000) // { // return; // } // turnSpeed = value; // } //} private int turnSpeed = 100; private int jiaodu = 0; [Description("风叶旋转速度,100-1000,值越小 速度越快"), Category("自定义")] public int TurnSpeed { get { return turnSpeed; } set { if (value <= 0 || value > 1000) return; turnSpeed = value; updateTimer.Interval = value; } } /// /// 是否显示底座 /// private bool isDZ = false; /// /// 是否显示底座 /// /// 是否显示底座 [Description("是否显示底座"), Category("自定义")] public bool IsDZ { get { return isDZ; } set { isDZ = value; Invalidate(); } } private bool isExit; [Description("是否退出"), Category("自定义")] public bool IsExit { get { return isExit; } set { isExit = value; } } /// /// The m rect working /// Rectangle m_rectWorking; //实时更新的定时器 private System.Windows.Forms.Timer updateTimer; /// /// Initializes a new instance of the class. /// public UCBlower() { InitializeComponent(); 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); updateTimer = new System.Windows.Forms.Timer(); updateTimer.Interval = 200; updateTimer.Tick += timer1_Tick; updateTimer.Start(); //Task.Run(() => //{ // Timer_Thread(); //}); } /// /// 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 / 2 * 2)) / 2, (this.Height - (intMin / 2 * 2)) / 2, (intMin / 2 * 2), (intMin / 2 * 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; } //底座 if (isDZ) { 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 + jiaodu; 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)); } public delegate void ShowStateHandler(); private void timer1_Tick(object sender, EventArgs e) { ShowStateHandler stateHandler = ShowButtonEven_Run; IAsyncResult result1 = stateHandler.BeginInvoke(null, stateHandler); } private void ShowButtonEven_Run() { if (turnAround == JinYuan.ControlLib.TurnAround.Clockwise) { jiaodu += 15; if (jiaodu == 45) jiaodu = 0; } else if (turnAround == JinYuan.ControlLib.TurnAround.Counterclockwise) { jiaodu -= 15; if (jiaodu == -45) jiaodu = 0; } this.BeginInvoke((Action)delegate { Invalidate(); }); } private void UCBlower_VisibleChanged(object sender, EventArgs e) { //IsStop = !this.Visible; } } /// /// Enum BlowerEntranceDirection /// public enum BlowerEntranceDirection { /// /// The none /// None, /// /// The left /// Left, /// /// The right /// Right, /// /// Up /// Up } /// /// Enum BlowerExitDirection /// public enum BlowerExitDirection { /// /// The none /// None, /// /// The left /// Left, /// /// The right /// Right, /// /// Up /// Up } /// /// 旋转方向 /// public enum TurnAround { /// /// 不旋转 /// None, /// /// 顺时针 /// Clockwise, /// /// 逆时针 /// Counterclockwise } }