InputClickHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Input;
  4. namespace HandyControl.Tools;
  5. /// <summary>
  6. /// 输入层点击帮助类
  7. /// </summary>
  8. public static class InputClickHelper
  9. {
  10. private static readonly DependencyProperty InputInfoProperty = DependencyProperty.RegisterAttached(
  11. "InputInfo", typeof(InputInfo), typeof(InputClickHelper), new PropertyMetadata(default(InputInfo)));
  12. private static void SetInputInfo(DependencyObject element, InputInfo value) => element.SetValue(InputInfoProperty, value);
  13. private static InputInfo GetInputInfo(DependencyObject element) => (InputInfo) element.GetValue(InputInfoProperty);
  14. /// <summary>
  15. /// 将 MouseDown MouseMove MouseUp 封装为点击事件
  16. /// </summary>
  17. /// <param name="element">要被附加的元素</param>
  18. /// <param name="clickEventHandler">点击的事件</param>
  19. /// <param name="dragStarted">因为拖动而结束点击时触发</param>
  20. public static void AttachMouseDownMoveUpToClick(UIElement element, EventHandler clickEventHandler,
  21. EventHandler dragStarted = null)
  22. {
  23. var inputInfo = GetOrCreateInputInfo(element);
  24. inputInfo.ClickEventHandler += clickEventHandler;
  25. inputInfo.DragStarted += dragStarted;
  26. element.MouseDown -= Element_MouseDown;
  27. element.MouseDown += Element_MouseDown;
  28. element.MouseMove -= Element_MouseMove;
  29. element.MouseMove += Element_MouseMove;
  30. element.MouseUp -= Element_MouseUp;
  31. element.MouseUp += Element_MouseUp;
  32. element.LostMouseCapture -= Element_LostMouseCapture;
  33. element.LostMouseCapture += Element_LostMouseCapture;
  34. }
  35. /// <summary>
  36. /// 去掉对 <paramref name="element" /> 的点击时间的监听
  37. /// </summary>
  38. /// <param name="element"></param>
  39. /// <param name="clickEventHandler">点击的事件</param>
  40. /// <param name="dragStarted">因为拖动而结束点击时触发的事件</param>
  41. public static void DetachMouseDownMoveUpToClick(UIElement element, EventHandler clickEventHandler,
  42. EventHandler dragStarted = null)
  43. {
  44. var inputInfo = GetInputInfo(element);
  45. if (inputInfo == null) return;
  46. inputInfo.ClickEventHandler -= clickEventHandler;
  47. inputInfo.DragStarted -= dragStarted;
  48. if (inputInfo.IsEmpty())
  49. {
  50. element.ClearValue(InputInfoProperty);
  51. element.MouseDown -= Element_MouseDown;
  52. element.MouseMove -= Element_MouseMove;
  53. element.MouseUp -= Element_MouseUp;
  54. element.LostMouseCapture -= Element_LostMouseCapture;
  55. }
  56. }
  57. private static void Element_LostMouseCapture(object sender, MouseEventArgs e)
  58. {
  59. var element = (UIElement) sender;
  60. GetInputInfo(element)?.LostCapture();
  61. }
  62. private static void Element_MouseUp(object sender, MouseButtonEventArgs e)
  63. {
  64. var element = (UIElement) sender;
  65. GetInputInfo(element)?.Up(e.GetPosition(element));
  66. }
  67. private static void Element_MouseMove(object sender, MouseEventArgs e)
  68. {
  69. var element = (UIElement) sender;
  70. GetInputInfo(element)?.Move(e.GetPosition(element));
  71. }
  72. private static void Element_MouseDown(object sender, MouseButtonEventArgs e)
  73. {
  74. var element = (UIElement) sender;
  75. GetInputInfo(element)?.Down(e.GetPosition(element));
  76. }
  77. private static InputInfo GetOrCreateInputInfo(UIElement element)
  78. {
  79. var inputInfo = GetInputInfo(element);
  80. if (inputInfo == null)
  81. {
  82. inputInfo = new InputInfo();
  83. SetInputInfo(element, inputInfo);
  84. }
  85. return inputInfo;
  86. }
  87. private class InputInfo
  88. {
  89. private const double ToleranceSquared = 0.01;
  90. private Point _downedPosition;
  91. private bool _isClick;
  92. public event EventHandler ClickEventHandler;
  93. public event EventHandler DragStarted;
  94. public void Down(Point position)
  95. {
  96. _downedPosition = position;
  97. _isClick = true;
  98. }
  99. public void Move(Point position)
  100. {
  101. if (!_isClick) return;
  102. if ((position - _downedPosition).LengthSquared > ToleranceSquared)
  103. {
  104. _isClick = false;
  105. DragStarted?.Invoke(null, EventArgs.Empty);
  106. }
  107. }
  108. public void Up(Point position)
  109. {
  110. _isClick = _isClick && (position - _downedPosition).LengthSquared <= ToleranceSquared;
  111. if (!_isClick) return;
  112. ClickEventHandler?.Invoke(null, EventArgs.Empty);
  113. _isClick = false;
  114. }
  115. public void LostCapture() => _isClick = false;
  116. public bool IsEmpty() => ClickEventHandler is null && DragStarted is null;
  117. }
  118. }