UserClock.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /**************************************************************
  11. *
  12. * 一个显示时间的控件,用来显示当前的时间和额外的定制界面
  13. *
  14. *
  15. *
  16. *************************************************************/
  17. namespace HslCommunication.Controls
  18. {
  19. /// <summary>
  20. /// 一个时钟控件
  21. /// </summary>
  22. public partial class UserClock : UserControl
  23. {
  24. /// <summary>
  25. /// 实例化一个时钟控件
  26. /// </summary>
  27. public UserClock()
  28. {
  29. InitializeComponent();
  30. _Time1s.Interval=50;
  31. _Time1s.Tick+=_Time1s_Tick;
  32. DoubleBuffered = true;
  33. }
  34. private void ClockMy_Load(object sender, EventArgs e)
  35. {
  36. BackgroundImage = _BackGround();
  37. _Time1s.Start();
  38. }
  39. //1秒的时钟
  40. private Timer _Time1s = new Timer();
  41. private void _Time1s_Tick(object sender,EventArgs e)
  42. {
  43. _NowTime = DateTime.Now;
  44. this.Invalidate();
  45. }
  46. private Bitmap _BackGround()
  47. {
  48. int _Width = this.Width;
  49. Bitmap temp = new Bitmap(_Width - 20, _Width - 20);
  50. Graphics g = Graphics.FromImage(temp);
  51. g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
  52. //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  53. //g.CompositingQuality = CompositingQuality.HighQuality;
  54. Point _CenterPoint = new Point(temp.Width / 2, temp.Height / 2);
  55. int _R = (temp.Width - 1) / 2;
  56. Rectangle _Rec1 = new Rectangle(0, 0, temp.Width - 1, temp.Width - 1);
  57. Rectangle _Rec2 = new Rectangle(2, 2, temp.Width - 5, temp.Width - 5);
  58. Rectangle _Rec3 = new Rectangle(_CenterPoint.X - 4, _CenterPoint.Y - 4, 8, 8);
  59. Rectangle _Rec4 = new Rectangle(5, 5, temp.Width - 11, temp.Width - 11);
  60. Rectangle _Rec5 = new Rectangle(8, 8, temp.Width - 17, temp.Width - 17);
  61. g.FillEllipse(Brushes.DarkGray, _Rec1);
  62. g.FillEllipse(Brushes.White, _Rec2);
  63. g.DrawEllipse(new Pen(Brushes.Black, (float)1.5), _Rec3);
  64. g.TranslateTransform(_CenterPoint.X, _CenterPoint.Y);
  65. for (int i = 0; i < 60; i++)
  66. {
  67. g.RotateTransform(6);
  68. g.DrawLine(Pens.DarkGray, new Point(_R - 3, 0), new Point(_R - 1, 0));
  69. }
  70. for (int i = 0; i < 12; i++)
  71. {
  72. g.RotateTransform(30);
  73. g.DrawLine(new Pen(Color.Chocolate, 2), new Point(_R - 6, 0), new Point(_R - 1, 0));
  74. }
  75. g.ResetTransform();
  76. System.Drawing.Font FontTemp = new Font("Microsoft YaHei UI", 12);
  77. int IntOffset_X = _R / 2;
  78. int IntOffset_Y = (int)(_R * Math.Sqrt(3) / 2);
  79. g.DrawString("1", FontTemp, Brushes.Green, new PointF(_R + IntOffset_X - 13, _R - IntOffset_Y + 4));
  80. g.DrawString("2", FontTemp, Brushes.Green, new PointF(_R + IntOffset_Y - 17, _R - IntOffset_X - 2));
  81. g.DrawString("3", FontTemp, Brushes.Green, new PointF(2 * _R - 18, _R - 8));
  82. g.DrawString("4", FontTemp, Brushes.Green, new PointF(_R + IntOffset_Y - 18, _R + IntOffset_X - 14));
  83. g.DrawString("5", FontTemp, Brushes.Green, new PointF(_R + IntOffset_X - 14, _R + IntOffset_Y - 19));
  84. g.DrawString("6", FontTemp, Brushes.Green, new PointF(_R - 6, 2 * _R - 22));
  85. g.DrawString("7", FontTemp, Brushes.Green, new PointF(_R - IntOffset_X + 2, _R + IntOffset_Y - 19));
  86. g.DrawString("8", FontTemp, Brushes.Green, new PointF(_R - IntOffset_Y + 5, _R + IntOffset_X - 14));
  87. g.DrawString("9", FontTemp, Brushes.Green, new PointF(8, _R - 9));
  88. g.DrawString("10", FontTemp, Brushes.Green, new PointF(_R - IntOffset_Y + 4, _R - IntOffset_X - 2));
  89. g.DrawString("11", FontTemp, Brushes.Green, new PointF(_R - IntOffset_X, _R - IntOffset_Y + 3));
  90. g.DrawString("12", FontTemp, Brushes.Green, new PointF(_R - 10, 7));
  91. // g.DrawString("Sweet", new Font("Courier New", 9), Brushes.Green, new PointF(_R - 18, _R/2));
  92. Bitmap _temp = new Bitmap(this.Width, this.Height);
  93. Graphics g1 = Graphics.FromImage(_temp);
  94. g1.DrawImage(temp, new Point(10, 10));
  95. temp.Dispose();
  96. return _temp;
  97. }
  98. //#############################################################################################################
  99. //属性设计器
  100. //#############################################################################################################
  101. //
  102. //==================================================================================================
  103. private DateTime _NowTime = DateTime.Now;
  104. /// <summary>
  105. /// 获取时钟的当前时间
  106. /// </summary>
  107. [Category("我的属性"), Description("设置边框的宽度")]
  108. public DateTime 当前时间
  109. {
  110. get { return _NowTime; }
  111. }
  112. //==================================================================================================
  113. //小时的指针颜色
  114. private Color _HourColor = Color.Chocolate;
  115. /// <summary>
  116. /// 获取或设置时钟指针的颜色
  117. /// </summary>
  118. [Category("我的属性"), Description("设置时钟的指针颜色")]
  119. [DefaultValue(typeof(Color), "Chocolate")]
  120. public Color 时钟指针颜色
  121. {
  122. set { _HourColor = value; }
  123. get { return _HourColor; }
  124. }
  125. //==================================================================================================
  126. //分钟的指针颜色
  127. private Color _MiniteColor = Color.Coral;
  128. /// <summary>
  129. /// 获取或设置时钟分钟指针颜色
  130. /// </summary>
  131. [Category("我的属性"), Description("设置分钟的指针颜色")]
  132. [DefaultValue(typeof(Color), "Coral")]
  133. public Color 分钟指针颜色
  134. {
  135. set { _MiniteColor = value; }
  136. get { return _MiniteColor; }
  137. }
  138. //==================================================================================================
  139. //秒钟的指针颜色
  140. private Color _SecondColor = Color.Green;
  141. /// <summary>
  142. /// 获取或设置秒钟指针颜色
  143. /// </summary>
  144. [Category("我的属性"), Description("设置秒钟的指针颜色")]
  145. [DefaultValue(typeof(Color), "Green")]
  146. public Color 秒钟指针颜色
  147. {
  148. set { _SecondColor = value; }
  149. get { return _SecondColor; }
  150. }
  151. //==================================================================================================
  152. //显示的文本
  153. private string _ShowText = "Sweet";
  154. /// <summary>
  155. /// 获取或设置时钟的个性化文本
  156. /// </summary>
  157. [Category("我的属性"), Description("设置时钟显示的字符串")]
  158. [DefaultValue(typeof(string), "Sweet")]
  159. public string 显示文本
  160. {
  161. set { _ShowText = value; }
  162. get { return _ShowText; }
  163. }
  164. //==================================================================================================
  165. //显示的文本字体
  166. private string _ShowTextFont = "Courier New";
  167. /// <summary>
  168. /// 字体
  169. /// </summary>
  170. [Category("我的属性"), Description("设置时钟显示的字符串")]
  171. [DefaultValue(typeof(string), "Courier New")]
  172. public string 显示文本字体
  173. {
  174. set { _ShowTextFont = value; }
  175. get { return _ShowTextFont; }
  176. }
  177. /// <summary>
  178. /// 重绘控件显示
  179. /// </summary>
  180. /// <param name="e"></param>
  181. protected override void OnPaint(PaintEventArgs e)
  182. {
  183. base.OnPaint(e);
  184. int _R = (this.Width - 21) / 2;
  185. int _Hour = _NowTime.Hour;
  186. int _Minite = _NowTime.Minute;
  187. float _Seconds = _NowTime.Second + (float)_NowTime.Millisecond / 1000;
  188. int _ArcHour = _Hour * (30) + 270 + _Minite / 2;
  189. int _ArcMinite = _Minite * 6 + 270;
  190. float _ArcSeconds = _Seconds * 6 + 270;
  191. Graphics g = e.Graphics;
  192. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  193. System.Drawing.Font _font1 = new System.Drawing.Font(_ShowTextFont, 14);
  194. System.Drawing.Size _SizeTemp = g.MeasureString(_ShowText, _font1).ToSize();
  195. g.DrawString(_ShowText, _font1, Brushes.Green, new PointF(_R - _SizeTemp.Width / 2 + 10, _R / 2 + 12));
  196. g.SmoothingMode = SmoothingMode.AntiAlias;
  197. SizeF _Size = g.MeasureString(_NowTime.DayOfWeek.ToString(), new Font(_ShowTextFont, 10));
  198. g.DrawString(_NowTime.DayOfWeek.ToString(),
  199. new Font(_ShowTextFont, 10), Brushes.Chocolate, new PointF(_R - _Size.ToSize().Width / 2 + 10, _R * 3 / 2 - 2));
  200. g.TranslateTransform(this.Width / 2, this.Width / 2);
  201. g.RotateTransform(_ArcHour, MatrixOrder.Prepend);
  202. g.DrawLine(new Pen(_HourColor, 2), new Point(4, 0), new Point(9, 0));
  203. g.DrawClosedCurve(new Pen(_HourColor, 1), new Point[]{new Point(12,2),new Point(10,0),new Point(12,-2),new Point(_R/2,-2),
  204. new Point(_R/2+6,0),new Point(_R/2,2)});
  205. g.RotateTransform(-_ArcHour);
  206. g.RotateTransform(_ArcMinite, MatrixOrder.Prepend);
  207. g.DrawLine(new Pen(_MiniteColor, 2), new Point(4, 0), new Point(9, 0));
  208. g.DrawClosedCurve(new Pen(_MiniteColor, 1), new Point[]{new Point(14,2),new Point(10,0),new Point(14,-2),new Point(_R-17,-2),
  209. new Point(_R-10,0),new Point(_R-17,2)});
  210. g.RotateTransform(-_ArcMinite);
  211. g.RotateTransform(_ArcSeconds, MatrixOrder.Prepend);
  212. g.DrawLine(new Pen(_SecondColor, 1), new Point(-13, 0), new Point(_R - 6, 0));
  213. g.ResetTransform();
  214. string _StrDate = _NowTime.Year + "年" + _NowTime.Month + "月" + _NowTime.Day + "日";
  215. System.Drawing.Size _Size2 = g.MeasureString(_StrDate, new Font(_ShowTextFont, 12)).ToSize();
  216. g.DrawString(_StrDate, new Font(_ShowTextFont, 12), Brushes.Green, new PointF(_R - _Size2.Width / 2 + 10, _R * 2 + 15));
  217. }
  218. private void ClockMy_SizeChanged(object sender, EventArgs e)
  219. {
  220. this.BackgroundImage = _BackGround();
  221. }
  222. }
  223. }