SoftAnimation.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. namespace HslCommunication.BasicFramework
  9. {
  10. /******************************************************************************
  11. *
  12. * 系统的一些动画特效,颜色渐变,位置移动等等
  13. *
  14. *
  15. *
  16. *
  17. *******************************************************************************/
  18. /// <summary>
  19. /// 系统框架支持的一些常用的动画特效
  20. /// </summary>
  21. public class SoftAnimation
  22. {
  23. /// <summary>
  24. /// 最小的时间片段
  25. /// </summary>
  26. private static int TimeFragment { get; set; } = 20;
  27. /// <summary>
  28. /// 调整控件背景色
  29. /// </summary>
  30. /// <param name="control">控件</param>
  31. /// <param name="color">设置的颜色</param>
  32. /// <param name="time">时间</param>
  33. public static void BeginBackcolorAnimation(Control control, Color color, int time)
  34. {
  35. if (control.BackColor != color)
  36. {
  37. Func<Control, Color> getcolor = m => m.BackColor;
  38. Action<Control, Color> setcolor = (m, n) => m.BackColor = n;
  39. ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolColorAnimation),
  40. new object[] { control, color, time, getcolor, setcolor });
  41. }
  42. }
  43. private static byte GetValue(byte Start, byte End, int i, int count)
  44. {
  45. if (Start == End) return Start;
  46. return (byte)((End - Start) * i / count + Start);
  47. }
  48. private static float GetValue(float Start, float End, int i, int count)
  49. {
  50. if (Start == End) return Start;
  51. return (End - Start) * i / count + Start;
  52. }
  53. private static void ThreadPoolColorAnimation(object obj)
  54. {
  55. object[] objs = obj as object[];
  56. Control control = objs[0] as Control;
  57. Color color = (Color)objs[1];
  58. int time = (int)objs[2];
  59. Func<Control, Color> getColor = (Func<Control, Color>)objs[3];
  60. Action<Control, Color> setcolor = (Action<Control, Color>)objs[4];
  61. int count = (time + TimeFragment - 1) / TimeFragment;
  62. Color color_old = getColor(control);
  63. try
  64. {
  65. for (int i = 0; i < count; i++)
  66. {
  67. control.Invoke(new Action(() =>
  68. {
  69. setcolor(control, Color.FromArgb(
  70. GetValue(color_old.R, color.R, i, count),
  71. GetValue(color_old.G, color.G, i, count),
  72. GetValue(color_old.B, color.B, i, count)));
  73. }));
  74. Thread.Sleep(TimeFragment);
  75. }
  76. control?.Invoke(new Action(() =>
  77. {
  78. setcolor(control, color);
  79. }));
  80. }
  81. catch
  82. {
  83. }
  84. }
  85. private static void ThreadPoolFloatAnimation(object obj)
  86. {
  87. object[] objs = obj as object[];
  88. Control control = objs[0] as Control;
  89. lock (control)
  90. {
  91. float value = (float)objs[1];
  92. int time = (int)objs[2];
  93. Func<Control, float> getValue = (Func<Control, float>)objs[3];
  94. Action<Control, float> setValue = (Action<Control, float>)objs[4];
  95. int count = (time + TimeFragment - 1) / TimeFragment;
  96. float value_old = getValue(control);
  97. for (int i = 0; i < count; i++)
  98. {
  99. if (control.IsHandleCreated && !control.IsDisposed)
  100. {
  101. control.Invoke(new Action(() =>
  102. {
  103. setValue(control, GetValue(value_old, value, i, count));
  104. }));
  105. }
  106. else
  107. {
  108. return;
  109. }
  110. Thread.Sleep(TimeFragment);
  111. }
  112. if (control.IsHandleCreated && !control.IsDisposed)
  113. {
  114. control.Invoke(new Action(() =>
  115. {
  116. setValue(control, value);
  117. }));
  118. }
  119. }
  120. }
  121. }
  122. }