update InputClickHelper
This commit is contained in:
1 parent
089aa7d2e8
commit
f9614d3b17
2 files changed
+29
-43
No files matched your search
@@ -346,7 +346,7 @@
|
|||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\DesignerHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\DesignerHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\DpiHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\DpiHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\IconHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\IconHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\Input\InputClickHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\InputClickHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\ResourceHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\ResourceHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\ScreenHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\ScreenHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\SecurityHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\SecurityHelper.cs" />
|
||||||
|
|||||||
+28
-42
@@ -2,15 +2,22 @@
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
|
||||||
namespace HandyControl.Tools.Input
|
namespace HandyControl.Tools
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 输入层点击帮助类
|
/// 输入层点击帮助类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class InputClickHelper
|
public static class InputClickHelper
|
||||||
{
|
{
|
||||||
|
private static readonly DependencyProperty InputInfoProperty = DependencyProperty.RegisterAttached(
|
||||||
|
"InputInfo", typeof(InputInfo), typeof(InputClickHelper), new PropertyMetadata(default(InputInfo)));
|
||||||
|
|
||||||
|
private static void SetInputInfo(DependencyObject element, InputInfo value) => element.SetValue(InputInfoProperty, value);
|
||||||
|
|
||||||
|
private static InputInfo GetInputInfo(DependencyObject element) => (InputInfo)element.GetValue(InputInfoProperty);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将 MouseDown MouseMove MouseUp 封装为点击事件
|
/// 将 MouseDown MouseMove MouseUp 封装为点击事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="element">要被附加的元素</param>
|
/// <param name="element">要被附加的元素</param>
|
||||||
/// <param name="clickEventHandler">点击的事件</param>
|
/// <param name="clickEventHandler">点击的事件</param>
|
||||||
@@ -34,7 +41,7 @@ namespace HandyControl.Tools.Input
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 去掉对 <paramref name="element"/> 的点击时间的监听
|
/// 去掉对 <paramref name="element" /> 的点击时间的监听
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="element"></param>
|
/// <param name="element"></param>
|
||||||
/// <param name="clickEventHandler">点击的事件</param>
|
/// <param name="clickEventHandler">点击的事件</param>
|
||||||
@@ -43,10 +50,7 @@ namespace HandyControl.Tools.Input
|
|||||||
EventHandler dragStarted = null)
|
EventHandler dragStarted = null)
|
||||||
{
|
{
|
||||||
var inputInfo = GetInputInfo(element);
|
var inputInfo = GetInputInfo(element);
|
||||||
if (inputInfo == null)
|
if (inputInfo == null) return;
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
inputInfo.ClickEventHandler -= clickEventHandler;
|
inputInfo.ClickEventHandler -= clickEventHandler;
|
||||||
inputInfo.DragStarted -= dragStarted;
|
inputInfo.DragStarted -= dragStarted;
|
||||||
@@ -63,34 +67,28 @@ namespace HandyControl.Tools.Input
|
|||||||
|
|
||||||
private static void Element_LostMouseCapture(object sender, MouseEventArgs e)
|
private static void Element_LostMouseCapture(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
var element = (UIElement) sender;
|
var element = (UIElement)sender;
|
||||||
GetInputInfo(element)?.LostCapture();
|
GetInputInfo(element)?.LostCapture();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Element_MouseUp(object sender, MouseButtonEventArgs e)
|
private static void Element_MouseUp(object sender, MouseButtonEventArgs e)
|
||||||
{
|
{
|
||||||
var element = (UIElement) sender;
|
var element = (UIElement)sender;
|
||||||
|
|
||||||
GetInputInfo(element)?.Up(e.GetPosition(element));
|
GetInputInfo(element)?.Up(e.GetPosition(element));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Element_MouseMove(object sender, MouseEventArgs e)
|
private static void Element_MouseMove(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
var element = (UIElement) sender;
|
var element = (UIElement)sender;
|
||||||
|
|
||||||
GetInputInfo(element)?.Move(e.GetPosition(element));
|
GetInputInfo(element)?.Move(e.GetPosition(element));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Element_MouseDown(object sender, MouseButtonEventArgs e)
|
private static void Element_MouseDown(object sender, MouseButtonEventArgs e)
|
||||||
{
|
{
|
||||||
var element = (UIElement) sender;
|
var element = (UIElement)sender;
|
||||||
|
|
||||||
GetInputInfo(element)?.Down(e.GetPosition(element));
|
GetInputInfo(element)?.Down(e.GetPosition(element));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly DependencyProperty InputInfoProperty = DependencyProperty.RegisterAttached(
|
|
||||||
"InputInfo", typeof(InputInfo), typeof(InputClickHelper), new PropertyMetadata(default(InputInfo)));
|
|
||||||
|
|
||||||
private static InputInfo GetOrCreateInputInfo(UIElement element)
|
private static InputInfo GetOrCreateInputInfo(UIElement element)
|
||||||
{
|
{
|
||||||
var inputInfo = GetInputInfo(element);
|
var inputInfo = GetInputInfo(element);
|
||||||
@@ -103,18 +101,18 @@ namespace HandyControl.Tools.Input
|
|||||||
return inputInfo;
|
return inputInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SetInputInfo(DependencyObject element, InputInfo value)
|
|
||||||
{
|
|
||||||
element.SetValue(InputInfoProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static InputInfo GetInputInfo(DependencyObject element)
|
|
||||||
{
|
|
||||||
return (InputInfo) element.GetValue(InputInfoProperty);
|
|
||||||
}
|
|
||||||
|
|
||||||
private class InputInfo
|
private class InputInfo
|
||||||
{
|
{
|
||||||
|
private const double ToleranceSquared = 0.01;
|
||||||
|
|
||||||
|
private Point _downedPosition;
|
||||||
|
|
||||||
|
private bool _isClick;
|
||||||
|
|
||||||
|
public event EventHandler ClickEventHandler;
|
||||||
|
|
||||||
|
public event EventHandler DragStarted;
|
||||||
|
|
||||||
public void Down(Point position)
|
public void Down(Point position)
|
||||||
{
|
{
|
||||||
_downedPosition = position;
|
_downedPosition = position;
|
||||||
@@ -134,8 +132,7 @@ namespace HandyControl.Tools.Input
|
|||||||
|
|
||||||
public void Up(Point position)
|
public void Up(Point position)
|
||||||
{
|
{
|
||||||
_isClick = _isClick
|
_isClick = _isClick && (position - _downedPosition).LengthSquared <= ToleranceSquared;
|
||||||
&& (position - _downedPosition).LengthSquared <= ToleranceSquared;
|
|
||||||
|
|
||||||
if (!_isClick) return;
|
if (!_isClick) return;
|
||||||
|
|
||||||
@@ -144,20 +141,9 @@ namespace HandyControl.Tools.Input
|
|||||||
_isClick = false;
|
_isClick = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LostCapture()
|
public void LostCapture() => _isClick = false;
|
||||||
{
|
|
||||||
_isClick = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double ToleranceSquared { set; get; } = 0.01;
|
|
||||||
|
|
||||||
public event EventHandler ClickEventHandler;
|
|
||||||
public event EventHandler DragStarted;
|
|
||||||
|
|
||||||
public bool IsEmpty() => ClickEventHandler is null && DragStarted is null;
|
public bool IsEmpty() => ClickEventHandler is null && DragStarted is null;
|
||||||
|
|
||||||
private Point _downedPosition;
|
|
||||||
private bool _isClick;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user