RotateImageButton.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.Drawing.Imaging;
  11. namespace Azylee.WinformSkin.ButtonUI
  12. {
  13. public partial class RotateImageButton : PictureBox
  14. {
  15. #region 属性
  16. #region 背景
  17. private Image _BackImageDefault = null;
  18. [Category("自定义属性")]
  19. [Description("正常显示背景")]
  20. public Image BackImageDefault
  21. {
  22. get { return _BackImageDefault; }
  23. set
  24. {
  25. _BackImageDefault = value;
  26. BackgroundImage = _BackImageDefault;
  27. }
  28. }
  29. private Image _BackImageHover = null;
  30. [Category("自定义属性")]
  31. [Description("鼠标悬停背景")]
  32. public Image BackImageHover
  33. {
  34. get { return _BackImageHover; }
  35. set { _BackImageHover = value; }
  36. }
  37. private Image _BackImageDown = null;
  38. [Category("自定义属性")]
  39. [Description("鼠标按下背景")]
  40. public Image BackImageDown
  41. {
  42. get { return _BackImageDown; }
  43. set { _BackImageDown = value; }
  44. }
  45. #endregion
  46. #region 前景
  47. private Image _ForeImageDefault = null;
  48. [Category("自定义属性")]
  49. [Description("正常显示前景")]
  50. public Image ForeImageDefault
  51. {
  52. get { return _ForeImageDefault; }
  53. set
  54. {
  55. _ForeImageDefault = value;
  56. Image = _ForeImageDefault;
  57. }
  58. }
  59. #endregion
  60. #endregion
  61. Graphics Graph = null;
  62. Timer TMPainter = new Timer();
  63. const int Interval = 50;
  64. int AnimaAngle = 0;
  65. const int AngleStep = 5;
  66. Bitmap Bmp = null;
  67. Graphics BmpGraph = null;
  68. bool IsInit = false;
  69. public RotateImageButton()
  70. {
  71. InitializeComponent();
  72. //采用双缓冲技术的控件必需的设置
  73. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  74. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  75. SetStyle(ControlStyles.UserPaint, true);
  76. if (!DesignMode)
  77. {
  78. Graph = CreateGraphics();
  79. Graph.SmoothingMode = SmoothingMode.AntiAlias;
  80. TMPainter.Interval = Interval;
  81. TMPainter.Tick += TMPainter_Tick;
  82. }
  83. }
  84. private void RotateImageButton_LoadCompleted(object sender, AsyncCompletedEventArgs e)
  85. {
  86. }
  87. #region 事件
  88. /// <summary>
  89. /// 鼠标进入
  90. /// </summary>
  91. /// <param name="sender"></param>
  92. /// <param name="e"></param>
  93. private void ImageButton_MouseEnter(object sender, EventArgs e)
  94. {
  95. if (BackImageHover != null)
  96. BackgroundImage = BackImageHover;
  97. else
  98. Stop();
  99. }
  100. /// <summary>
  101. /// 鼠标悬停
  102. /// </summary>
  103. /// <param name="sender"></param>
  104. /// <param name="e"></param>
  105. private void ImageButton_MouseHover(object sender, EventArgs e)
  106. {
  107. if (BackImageHover != null)
  108. BackgroundImage = BackImageHover;
  109. else
  110. Stop();
  111. Start();
  112. }
  113. /// <summary>
  114. /// 鼠标移出
  115. /// </summary>
  116. /// <param name="sender"></param>
  117. /// <param name="e"></param>
  118. private void ImageButton_MouseLeave(object sender, EventArgs e)
  119. {
  120. Stop();
  121. }
  122. /// <summary>
  123. /// 鼠标按下
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void ImageButton_MouseDown(object sender, MouseEventArgs e)
  128. {
  129. Stop();
  130. if (BackImageDown != null) BackgroundImage = BackImageDown;
  131. }
  132. /// <summary>
  133. /// 鼠标抬起
  134. /// </summary>
  135. /// <param name="sender"></param>
  136. /// <param name="e"></param>
  137. private void ImageButton_MouseUp(object sender, MouseEventArgs e)
  138. {
  139. if (BackImageHover != null)
  140. BackgroundImage = BackImageHover;
  141. else
  142. Stop();
  143. }
  144. private void TMPainter_Tick(object sender, EventArgs e)
  145. {
  146. Draw();
  147. }
  148. #endregion
  149. /// <summary>
  150. /// 开始动画
  151. /// </summary>
  152. private void Start()
  153. {
  154. if (ForeImageDefault != null && !TMPainter.Enabled)
  155. {
  156. //之前设置为0,导致开始执行到18次时会抽搐一下子
  157. AnimaAngle = 360;
  158. TMPainter.Enabled = true;
  159. }
  160. }
  161. /// <summary>
  162. /// 停止并恢复到默认状态
  163. /// </summary>
  164. private void Stop()
  165. {
  166. TMPainter.Enabled = false;
  167. Image = ForeImageDefault;
  168. BackgroundImage = BackImageDefault;
  169. }
  170. private void Init()
  171. {
  172. try
  173. {
  174. if (!IsInit)
  175. {
  176. Bmp = new Bitmap(Width, Height);
  177. BmpGraph = Graphics.FromImage(Bmp);
  178. BmpGraph.SmoothingMode = SmoothingMode.AntiAlias;
  179. IsInit = true;
  180. }
  181. }
  182. catch { }
  183. }
  184. private void Draw()
  185. {
  186. try
  187. {
  188. Init();
  189. if (Graph != null && Bmp != null && BmpGraph != null)
  190. {
  191. BmpGraph.Clear(Color.Transparent);
  192. //绘制
  193. if (ForeImageDefault != null)
  194. {
  195. BmpGraph.ResetTransform();//恢复默认状态
  196. BmpGraph.TranslateTransform(Width / 2, Height / 2);//设置原点
  197. BmpGraph.RotateTransform(AnimaAngle += AngleStep);//以水平线为x轴,从垂直上方开始旋转,每次旋转6度。
  198. BmpGraph.DrawImage(ForeImageDefault, -(Width / 2), -(Height / 2), Width, Height);
  199. }
  200. Image = Bmp;
  201. }
  202. }
  203. catch { }
  204. }
  205. ~RotateImageButton()
  206. {
  207. BmpGraph.Dispose();
  208. Bmp.Dispose();
  209. Graph.Dispose();
  210. }
  211. }
  212. }