UIImage.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using Microsoft.Drawing;
  5. using Microsoft.Windows.Forms.Animate;
  6. namespace Microsoft.Windows.Forms
  7. {
  8. /// <summary>
  9. /// 图片动画控件
  10. /// </summary>
  11. public class UIImage : UIControl
  12. {
  13. internal const int DEFAULT_ANIMATION_INTERVAL = 8000; //默认动画触发时间间隔
  14. internal const int DEFAULT_FRAME_INTERVAL = 40; //默认帧间隔
  15. private UIImageAnimation m_Animation = new UIImageAnimation(); //图片动画
  16. private Timer m_AnimationTimer = new Timer(); //动画触发定时器
  17. private Timer m_FrameTimer = new Timer(); //帧定时器
  18. /// <summary>
  19. /// 获取或设置动画触发时间间隔,毫秒
  20. /// </summary>
  21. public int AnimationInterval
  22. {
  23. get
  24. {
  25. return this.m_AnimationTimer.Interval;
  26. }
  27. set
  28. {
  29. this.m_AnimationTimer.Interval = value;
  30. }
  31. }
  32. /// <summary>
  33. /// 获取或设置动画执行时间,毫秒
  34. /// </summary>
  35. public int AnimationSpan
  36. {
  37. get
  38. {
  39. return this.m_Animation.Span;
  40. }
  41. set
  42. {
  43. this.m_Animation.Span = value;
  44. }
  45. }
  46. /// <summary>
  47. /// 获取或设置动画是否随机
  48. /// </summary>
  49. public bool AnimationRandom
  50. {
  51. get
  52. {
  53. return this.m_Animation.RandomPlay;
  54. }
  55. set
  56. {
  57. this.m_Animation.RandomPlay = value;
  58. }
  59. }
  60. private Color m_BorderColor = DefaultTheme.BorderColor;
  61. /// <summary>
  62. /// 获取或设置边框颜色
  63. /// </summary>
  64. public virtual Color BorderColor
  65. {
  66. get
  67. {
  68. return this.m_BorderColor;
  69. }
  70. set
  71. {
  72. if (value != this.m_BorderColor)
  73. {
  74. this.m_BorderColor = value;
  75. this.Invalidate();
  76. }
  77. }
  78. }
  79. private BorderVisibleStyle m_BorderVisibleStyle;
  80. /// <summary>
  81. /// 获取或设置边框样式
  82. /// </summary>
  83. public virtual BorderVisibleStyle BorderVisibleStyle
  84. {
  85. get
  86. {
  87. return this.m_BorderVisibleStyle;
  88. }
  89. set
  90. {
  91. if (value != this.m_BorderVisibleStyle)
  92. {
  93. this.m_BorderVisibleStyle = value;
  94. this.Invalidate();
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// 构造函数
  100. /// </summary>
  101. public UIImage()
  102. {
  103. //初始化触发定时器
  104. this.m_AnimationTimer.Interval = DEFAULT_ANIMATION_INTERVAL;
  105. this.m_AnimationTimer.Tick += (sender, e) =>
  106. {
  107. this.m_FrameTimer.Start();
  108. this.m_Animation.Next();
  109. };
  110. //初始化帧定时器
  111. this.m_FrameTimer.Interval = DEFAULT_FRAME_INTERVAL;
  112. this.m_FrameTimer.Tick += (sender, e) =>
  113. {
  114. this.Invalidate();
  115. if (this.m_Animation.Stopped)
  116. this.m_FrameTimer.Stop();
  117. };
  118. //启动触发定时器
  119. this.m_AnimationTimer.Start();
  120. }
  121. /// <summary>
  122. /// 添加一个图像帧
  123. /// </summary>
  124. /// <param name="image">图像</param>
  125. public void AddFrame(Image image)
  126. {
  127. this.m_Animation.AddFrame(image);
  128. }
  129. /// <summary>
  130. /// 添加一个颜色帧
  131. /// </summary>
  132. /// <param name="color">颜色</param>
  133. public void AddFrame(Color color)
  134. {
  135. this.m_Animation.AddFrame(color);
  136. }
  137. /// <summary>
  138. /// 添加一个随机颜色帧
  139. /// </summary>
  140. public void AddFrame()
  141. {
  142. this.m_Animation.AddFrame();
  143. }
  144. /// <summary>
  145. /// 清空帧
  146. /// </summary>
  147. public void ClearFrame()
  148. {
  149. this.m_Animation.ClearFrame();
  150. }
  151. /// <summary>
  152. /// 渲染控件
  153. /// </summary>
  154. /// <param name="e">数据</param>
  155. protected override void RenderSelf(PaintEventArgs e)
  156. {
  157. //准备
  158. Graphics g = e.Graphics;
  159. Rectangle rect = RectangleEx.Subtract(this.ClientRectangle, this.Padding);
  160. //渲染
  161. this.Sprite.BackgroundImage = this.m_Animation.Current;
  162. this.Sprite.BackgroundImageLayout = ImageLayout.Stretch;
  163. this.Sprite.BorderColor = this.BorderColor;
  164. this.Sprite.BorderVisibleStyle = this.BorderVisibleStyle;
  165. this.Sprite.State = this.State;
  166. this.Sprite.BeginRender(g);
  167. this.Sprite.RenderBackgroundImage(rect);
  168. this.Sprite.RenderBorder(rect);
  169. this.Sprite.EndRender();
  170. }
  171. /// <summary>
  172. /// 大小改变
  173. /// </summary>
  174. /// <param name="e">数据</param>
  175. protected override void OnSizeChanged(EventArgs e)
  176. {
  177. base.OnSizeChanged(e);
  178. this.m_Animation.Size = this.Size - this.Padding.Size;
  179. }
  180. /// <summary>
  181. /// 释放资源
  182. /// </summary>
  183. /// <param name="disposing">释放托管资源为true,否则为false</param>
  184. protected override void Dispose(bool disposing)
  185. {
  186. if (this.m_AnimationTimer != null)
  187. {
  188. this.m_AnimationTimer.Dispose();
  189. this.m_AnimationTimer = null;
  190. }
  191. if (this.m_FrameTimer != null)
  192. {
  193. this.m_FrameTimer.Dispose();
  194. this.m_FrameTimer = null;
  195. }
  196. if (this.m_Animation != null)
  197. {
  198. this.m_Animation.Dispose();
  199. this.m_Animation = null;
  200. }
  201. base.Dispose(disposing);
  202. }
  203. }
  204. }