SimpleClockControl.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace Azylee.WinformSkin.UserWidgets.ClockWidgets
  12. {
  13. public partial class SimpleClockControl : UserControl
  14. {
  15. #region 属性
  16. private Image _SecondHandImage = null;
  17. [Category("时钟样式")]
  18. [Description("秒针")]
  19. public Image SecondHandImage
  20. {
  21. get { return _SecondHandImage; }
  22. set { _SecondHandImage = value; }
  23. }
  24. private Image _MinuteHandImage = null;
  25. [Category("时钟样式")]
  26. [Description("分针")]
  27. public Image MinuteHandImage
  28. {
  29. get { return _MinuteHandImage; }
  30. set { _MinuteHandImage = value; }
  31. }
  32. private Image _HourHandImage = null;
  33. [Category("时钟样式")]
  34. [Description("时针")]
  35. public Image HourHandImage
  36. {
  37. get { return _HourHandImage; }
  38. set { _HourHandImage = value; }
  39. }
  40. #endregion
  41. Graphics Graph = null;
  42. Bitmap Bmp = null;
  43. Graphics BmpGraph = null;
  44. DateTime Time = DateTime.Now;
  45. public SimpleClockControl()
  46. {
  47. InitializeComponent();
  48. //采用双缓冲技术的控件必需的设置
  49. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  50. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  51. SetStyle(ControlStyles.UserPaint, true);
  52. }
  53. private void SimpleClockControl_Load(object sender, EventArgs e)
  54. {
  55. }
  56. public void Start()
  57. {
  58. Graph = CreateGraphics();
  59. Graph.SmoothingMode = SmoothingMode.AntiAlias;
  60. Bmp = new Bitmap(Width, Height);
  61. BmpGraph = Graphics.FromImage(Bmp);
  62. BmpGraph.SmoothingMode = SmoothingMode.AntiAlias;
  63. TMPainter.Interval = 1 * 1000;
  64. if (SecondHandImage == null)
  65. {
  66. TMPainter.Interval = 60 * 1000;
  67. //SecondHandImage = Resources.simpleclock_simple_second_hand_1;
  68. }
  69. if (MinuteHandImage == null)
  70. {
  71. TMPainter.Interval = 60 * 60 * 1000;
  72. MinuteHandImage = Resources.simpleclock_simple_minute_hand_1;
  73. }
  74. if (HourHandImage == null)
  75. {
  76. HourHandImage = Resources.simpleclock_simple_hour_hand_1;
  77. }
  78. Draw();//第一次绘制
  79. TMPainter.Enabled = true;
  80. }
  81. public void ReDraw()
  82. {
  83. Draw();
  84. }
  85. private void TMPainter_Tick(object sender, EventArgs e)
  86. {
  87. Draw();
  88. }
  89. private void Draw()
  90. {
  91. try
  92. {
  93. if (Graph != null && Bmp != null && BmpGraph != null)
  94. {
  95. Refresh();//强制重绘控件
  96. Time = DateTime.Now;
  97. BmpGraph.ResetTransform();//恢复默认状态
  98. BmpGraph.FillRectangle(new SolidBrush(BackColor), 0, 0, Width, Height);
  99. if (BackgroundImage != null) BmpGraph.DrawImage(BackgroundImage, 0, 0, Width, Height);
  100. //绘制时针
  101. if (HourHandImage != null)
  102. {
  103. BmpGraph.ResetTransform();//恢复默认状态
  104. BmpGraph.TranslateTransform(Width / 2, Height / 2);//设置原点
  105. BmpGraph.RotateTransform(Time.Hour * 30 + Time.Minute * 1 / 2);
  106. BmpGraph.DrawImage(HourHandImage, -(Width / 2), -(Height / 2), Width, Height);
  107. }
  108. //绘制分针
  109. if (MinuteHandImage != null)
  110. {
  111. BmpGraph.ResetTransform();//恢复默认状态
  112. BmpGraph.TranslateTransform(Width / 2, Height / 2);//设置原点
  113. BmpGraph.RotateTransform(Time.Minute * 6);
  114. BmpGraph.DrawImage(MinuteHandImage, -(Width / 2), -(Height / 2), Width, Height);
  115. }
  116. //绘制秒针
  117. if (SecondHandImage != null)
  118. {
  119. BmpGraph.ResetTransform();//恢复默认状态
  120. BmpGraph.TranslateTransform(Width / 2, Height / 2);//设置原点
  121. BmpGraph.RotateTransform(Time.Second * 6);//以水平线为x轴,从垂直上方开始旋转,每次旋转6度。
  122. BmpGraph.DrawImage(SecondHandImage, -(Width / 2), -(Height / 2), Width, Height);
  123. }
  124. OnPaint(new PaintEventArgs(Graph, new Rectangle(0, 0, Width, Height)));
  125. }
  126. }
  127. catch { }
  128. }
  129. protected override void OnPaint(PaintEventArgs e)
  130. {
  131. base.OnPaint(e);
  132. if (!DesignMode && e != null && e.Graphics != null && Bmp != null)
  133. {
  134. Graphics g = e.Graphics;
  135. g.DrawImage(Bmp, 0, 0, Width, Height);
  136. }
  137. }
  138. ~SimpleClockControl()
  139. {
  140. Graph.Dispose();
  141. Bmp.Dispose();
  142. BmpGraph.Dispose();
  143. }
  144. }
  145. }