SimpleClockControl.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Azylee.WinformSkin.Properties;
  11. using System.Drawing.Imaging;
  12. namespace Azylee.WinformSkin.UserWidgets.ClockWidgets
  13. {
  14. public partial class SimpleClockControl : UserControl
  15. {
  16. #region 属性
  17. private Image _SecondHandImage = null;
  18. [Category("时钟样式")]
  19. [Description("秒针")]
  20. public Image SecondHandImage
  21. {
  22. get { return _SecondHandImage; }
  23. set { _SecondHandImage = value; }
  24. }
  25. private Image _MinuteHandImage = null;
  26. [Category("时钟样式")]
  27. [Description("分针")]
  28. public Image MinuteHandImage
  29. {
  30. get { return _MinuteHandImage; }
  31. set { _MinuteHandImage = value; }
  32. }
  33. private Image _HourHandImage = null;
  34. [Category("时钟样式")]
  35. [Description("时针")]
  36. public Image HourHandImage
  37. {
  38. get { return _HourHandImage; }
  39. set { _HourHandImage = value; }
  40. }
  41. private Image _ClockBackImage = null;
  42. [Category("时钟样式")]
  43. [Description("表盘")]
  44. public Image ClockBackImage
  45. {
  46. get { return _ClockBackImage; }
  47. set
  48. {
  49. _ClockBackImage = value;
  50. BackgroundImage = _ClockBackImage;
  51. }
  52. }
  53. #endregion
  54. Bitmap Bmp = null;
  55. Graphics Graph = null;
  56. DateTime Time = DateTime.Now;
  57. double TimeShift = 0;
  58. public SimpleClockControl()
  59. {
  60. InitializeComponent();
  61. //采用双缓冲技术的控件必需的设置
  62. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  63. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  64. SetStyle(ControlStyles.UserPaint, true);
  65. }
  66. private void SimpleClockControl_Load(object sender, EventArgs e)
  67. {
  68. }
  69. #region 对外提供方法
  70. /// <summary>
  71. /// 启动时钟刷新
  72. /// </summary>
  73. /// <param name="secondInterval">刷新间隔时间()</param>
  74. /// <param name="timeShift">时间偏移(小时)</param>
  75. public void Start(int secondInterval = 1, double timeShift = 0)
  76. {
  77. Init(timeShift);//初始化数据
  78. Draw();//第一次绘制
  79. TMPainter.Interval = (secondInterval >= 1 ? secondInterval : 1) * 1000;//设置刷新间隔
  80. TMPainter.Enabled = true;//启动计时器任务
  81. }
  82. /// <summary>
  83. /// 强制重绘控件
  84. /// </summary>
  85. public void ReDraw()
  86. {
  87. Draw();
  88. }
  89. #endregion
  90. private void TMPainter_Tick(object sender, EventArgs e)
  91. {
  92. Draw();
  93. }
  94. private void Init(double timeShift)
  95. {
  96. try
  97. {
  98. Bmp = new Bitmap(Width, Height);
  99. Graph = Graphics.FromImage(Bmp);
  100. Graph.SmoothingMode = SmoothingMode.AntiAlias;
  101. TimeShift = timeShift;
  102. if (SecondHandImage == null)
  103. {
  104. //TMPainter.Interval = 60 * 1000;
  105. //SecondHandImage = Resources.simpleclock_simple_second_hand_1;
  106. }
  107. if (MinuteHandImage == null)
  108. {
  109. //TMPainter.Interval = 60 * 60 * 1000;
  110. MinuteHandImage = Resources.simpleclock_simple_minute_hand_1;
  111. }
  112. if (HourHandImage == null)
  113. {
  114. HourHandImage = Resources.simpleclock_simple_hour_hand_1;
  115. }
  116. }
  117. catch { }
  118. }
  119. private void Draw()
  120. {
  121. try
  122. {
  123. if (Bmp != null && Graph != null)
  124. {
  125. Time = DateTime.Now.AddHours(TimeShift);
  126. Graph.ResetTransform();//恢复默认状态
  127. Graph.Clear(Color.Transparent);
  128. Graph.FillRectangle(new SolidBrush(BackColor), 0, 0, Width, Height);
  129. if (ClockBackImage != null) Graph.DrawImage(ClockBackImage, 0, 0, Width, Height);
  130. //绘制时针
  131. if (HourHandImage != null)
  132. {
  133. Graph.ResetTransform();//恢复默认状态
  134. Graph.TranslateTransform(Width / 2, Height / 2);//设置原点
  135. Graph.RotateTransform(Time.Hour * 30 + Time.Minute * 1 / 2);
  136. Graph.DrawImage(HourHandImage, -(Width / 2), -(Height / 2), Width, Height);
  137. }
  138. //绘制分针
  139. if (MinuteHandImage != null)
  140. {
  141. Graph.ResetTransform();//恢复默认状态
  142. Graph.TranslateTransform(Width / 2, Height / 2);//设置原点
  143. Graph.RotateTransform(Time.Minute * 6);
  144. Graph.DrawImage(MinuteHandImage, -(Width / 2), -(Height / 2), Width, Height);
  145. }
  146. //绘制秒针
  147. if (SecondHandImage != null)
  148. {
  149. Graph.ResetTransform();//恢复默认状态
  150. Graph.TranslateTransform(Width / 2, Height / 2);//设置原点
  151. Graph.RotateTransform(Time.Second * 6);//以水平线为x轴,从垂直上方开始旋转,每次旋转6度。
  152. Graph.DrawImage(SecondHandImage, -(Width / 2), -(Height / 2), Width, Height);
  153. }
  154. BackgroundImage = Bmp;
  155. Refresh();
  156. }
  157. }
  158. catch { }
  159. }
  160. ~SimpleClockControl()
  161. {
  162. Graph.Dispose();
  163. Bmp.Dispose();
  164. }
  165. }
  166. }