Clock.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Controls.Primitives;
  6. using System.Windows.Data;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using HandyControl.Tools;
  10. namespace HandyControl.Controls;
  11. [TemplatePart(Name = ElementButtonAm, Type = typeof(RadioButton))]
  12. [TemplatePart(Name = ElementButtonPm, Type = typeof(RadioButton))]
  13. [TemplatePart(Name = ElementCanvas, Type = typeof(Canvas))]
  14. [TemplatePart(Name = ElementBorderTitle, Type = typeof(Border))]
  15. [TemplatePart(Name = ElementBorderClock, Type = typeof(Border))]
  16. [TemplatePart(Name = ElementPanelNum, Type = typeof(CirclePanel))]
  17. [TemplatePart(Name = ElementTimeStr, Type = typeof(TextBlock))]
  18. public class Clock : ClockBase
  19. {
  20. #region Constants
  21. private const string ElementButtonAm = "PART_ButtonAm";
  22. private const string ElementButtonPm = "PART_ButtonPm";
  23. private const string ElementCanvas = "PART_Canvas";
  24. private const string ElementBorderTitle = "PART_BorderTitle";
  25. private const string ElementBorderClock = "PART_BorderClock";
  26. private const string ElementPanelNum = "PART_PanelNum";
  27. private const string ElementTimeStr = "PART_TimeStr";
  28. #endregion Constants
  29. #region Data
  30. private RadioButton _buttonAm;
  31. private RadioButton _buttonPm;
  32. private Canvas _canvas;
  33. private Border _borderTitle;
  34. private Border _borderClock;
  35. private ClockRadioButton _currentButton;
  36. private RotateTransform _rotateTransformClock;
  37. private CirclePanel _circlePanel;
  38. private List<ClockRadioButton> _radioButtonList;
  39. private TextBlock _blockTime;
  40. private int _secValue;
  41. #endregion Data
  42. #region Public Properties
  43. public static readonly DependencyProperty ClockRadioButtonStyleProperty = DependencyProperty.Register(
  44. nameof(ClockRadioButtonStyle), typeof(Style), typeof(Clock), new PropertyMetadata(default(Style)));
  45. public Style ClockRadioButtonStyle
  46. {
  47. get => (Style) GetValue(ClockRadioButtonStyleProperty);
  48. set => SetValue(ClockRadioButtonStyleProperty, value);
  49. }
  50. private int SecValue
  51. {
  52. get => _secValue;
  53. set
  54. {
  55. if (value < 0)
  56. {
  57. _secValue = 59;
  58. }
  59. else if (value > 59)
  60. {
  61. _secValue = 0;
  62. }
  63. else
  64. {
  65. _secValue = value;
  66. }
  67. }
  68. }
  69. #endregion Public Properties
  70. #region Public Methods
  71. public override void OnApplyTemplate()
  72. {
  73. AppliedTemplate = false;
  74. if (_buttonAm != null)
  75. {
  76. _buttonAm.Click -= ButtonAm_OnClick;
  77. }
  78. if (_buttonPm != null)
  79. {
  80. _buttonPm.Click -= ButtonPm_OnClick;
  81. }
  82. if (ButtonConfirm != null)
  83. {
  84. ButtonConfirm.Click -= ButtonConfirm_OnClick;
  85. }
  86. if (_borderTitle != null)
  87. {
  88. _borderTitle.MouseWheel -= BorderTitle_OnMouseWheel;
  89. }
  90. if (_canvas != null)
  91. {
  92. _canvas.MouseWheel -= Canvas_OnMouseWheel;
  93. _canvas.RemoveHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Canvas_OnClick));
  94. _canvas.MouseMove -= Canvas_OnMouseMove;
  95. }
  96. base.OnApplyTemplate();
  97. _buttonAm = GetTemplateChild(ElementButtonAm) as RadioButton;
  98. _buttonPm = GetTemplateChild(ElementButtonPm) as RadioButton;
  99. ButtonConfirm = GetTemplateChild(ElementButtonConfirm) as Button;
  100. _borderTitle = GetTemplateChild(ElementBorderTitle) as Border;
  101. _canvas = GetTemplateChild(ElementCanvas) as Canvas;
  102. _borderClock = GetTemplateChild(ElementBorderClock) as Border;
  103. _circlePanel = GetTemplateChild(ElementPanelNum) as CirclePanel;
  104. _blockTime = GetTemplateChild(ElementTimeStr) as TextBlock;
  105. if (!CheckNull()) return;
  106. _buttonAm.Click += ButtonAm_OnClick;
  107. _buttonPm.Click += ButtonPm_OnClick;
  108. ButtonConfirm.Click += ButtonConfirm_OnClick;
  109. _borderTitle.MouseWheel += BorderTitle_OnMouseWheel;
  110. _canvas.MouseWheel += Canvas_OnMouseWheel;
  111. _canvas.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Canvas_OnClick));
  112. _canvas.MouseMove += Canvas_OnMouseMove;
  113. _rotateTransformClock = new RotateTransform();
  114. _borderClock.RenderTransform = _rotateTransformClock;
  115. _radioButtonList = new List<ClockRadioButton>();
  116. for (var i = 0; i < 12; i++)
  117. {
  118. var num = i + 1;
  119. var button = new ClockRadioButton
  120. {
  121. Num = num,
  122. Content = num
  123. };
  124. button.SetBinding(StyleProperty, new Binding(ClockRadioButtonStyleProperty.Name) { Source = this });
  125. _radioButtonList.Add(button);
  126. _circlePanel.Children.Add(button);
  127. }
  128. AppliedTemplate = true;
  129. if (SelectedTime.HasValue)
  130. {
  131. Update(SelectedTime.Value);
  132. }
  133. else
  134. {
  135. DisplayTime = DateTime.Now;
  136. Update(DisplayTime);
  137. }
  138. }
  139. #endregion Public Methods
  140. #region Private Methods
  141. private bool CheckNull()
  142. {
  143. if (_buttonPm == null || _buttonAm == null || ButtonConfirm == null || _canvas == null ||
  144. _borderTitle == null || _borderClock == null || _circlePanel == null ||
  145. _blockTime == null) return false;
  146. return true;
  147. }
  148. private void BorderTitle_OnMouseWheel(object sender, MouseWheelEventArgs e)
  149. {
  150. if (e.Delta < 0)
  151. {
  152. SecValue--;
  153. Update();
  154. }
  155. else
  156. {
  157. SecValue++;
  158. Update();
  159. }
  160. e.Handled = true;
  161. }
  162. private void Canvas_OnMouseWheel(object sender, MouseWheelEventArgs e)
  163. {
  164. var value = (int) _rotateTransformClock.Angle;
  165. if (e.Delta < 0)
  166. {
  167. value += 6;
  168. }
  169. else
  170. {
  171. value -= 6;
  172. }
  173. if (value < 0)
  174. {
  175. value = value + 360;
  176. }
  177. _rotateTransformClock.Angle = value;
  178. Update();
  179. e.Handled = true;
  180. }
  181. private void Canvas_OnClick(object sender, RoutedEventArgs e)
  182. {
  183. _currentButton = e.OriginalSource as ClockRadioButton;
  184. if (_currentButton != null)
  185. {
  186. Update();
  187. }
  188. }
  189. private void Canvas_OnMouseMove(object sender, MouseEventArgs e)
  190. {
  191. if (e.LeftButton == MouseButtonState.Pressed)
  192. {
  193. var value = ArithmeticHelper.CalAngle(new Point(85, 85), e.GetPosition(_canvas)) + 90;
  194. if (value < 0)
  195. {
  196. value = value + 360;
  197. }
  198. value = value - value % 6;
  199. _rotateTransformClock.Angle = value;
  200. Update();
  201. }
  202. }
  203. private void Update()
  204. {
  205. if (!AppliedTemplate) return;
  206. var hValue = _currentButton.Num;
  207. if (_buttonPm.IsChecked == true)
  208. {
  209. hValue += 12;
  210. if (hValue == 24) hValue = 12;
  211. }
  212. else if (hValue == 12)
  213. {
  214. hValue = 0;
  215. }
  216. if (hValue == 12 && _buttonAm.IsChecked == true)
  217. {
  218. _buttonPm.IsChecked = true;
  219. _buttonAm.IsChecked = false;
  220. }
  221. if (_blockTime != null)
  222. {
  223. DisplayTime = GetDisplayTime();
  224. _blockTime.Text = DisplayTime.ToString(TimeFormat);
  225. }
  226. }
  227. /// <summary>
  228. /// 更新
  229. /// </summary>
  230. /// <param name="time"></param>
  231. internal override void Update(DateTime time)
  232. {
  233. if (!AppliedTemplate) return;
  234. var h = time.Hour;
  235. var m = time.Minute;
  236. if (h >= 12)
  237. {
  238. _buttonPm.IsChecked = true;
  239. _buttonAm.IsChecked = false;
  240. }
  241. else
  242. {
  243. _buttonPm.IsChecked = false;
  244. _buttonAm.IsChecked = true;
  245. }
  246. _rotateTransformClock.Angle = m * 6;
  247. var hRest = h % 12;
  248. if (hRest == 0) hRest = 12;
  249. var ctl = _radioButtonList[hRest - 1];
  250. ctl.IsChecked = true;
  251. ctl.RaiseEvent(new RoutedEventArgs { RoutedEvent = ButtonBase.ClickEvent });
  252. _secValue = time.Second;
  253. Update();
  254. }
  255. /// <summary>
  256. /// 获取显示时间
  257. /// </summary>
  258. /// <returns></returns>
  259. private DateTime GetDisplayTime()
  260. {
  261. var hValue = _currentButton.Num;
  262. if (_buttonPm.IsChecked == true)
  263. {
  264. hValue += 12;
  265. if (hValue == 24) hValue = 12;
  266. }
  267. else if (hValue == 12)
  268. {
  269. hValue = 0;
  270. }
  271. var now = DateTime.Now;
  272. return new DateTime(now.Year, now.Month, now.Day, hValue, (int) Math.Abs(_rotateTransformClock.Angle) % 360 / 6, _secValue);
  273. }
  274. private void ButtonAm_OnClick(object sender, RoutedEventArgs e) => Update();
  275. private void ButtonPm_OnClick(object sender, RoutedEventArgs e) => Update();
  276. #endregion Private Methods
  277. }