C# WPF-在设定的时间段内检测鼠标按下
检测鼠标按钮在特定元素上按下特定时间段的最佳方法是什么?您需要向对象添加C# WPF-在设定的时间段内检测鼠标按下,c#,.net,wpf,mouseevent,C#,.net,Wpf,Mouseevent,检测鼠标按钮在特定元素上按下特定时间段的最佳方法是什么?您需要向对象添加MouseDown和MouseUp处理程序。在MouseDown中记录DateTime.Now。如果在MouseUp处理程序中: DateTime.Now.Subtract(clickTime).TotalSeconds > your_seconds_value 然后触发一个新事件MouseClickedForXseconds 如果不想等待鼠标向上移动事件,则需要启动MouseDown方法上的计时器,该方法将触发Mo
MouseDown
和MouseUp
处理程序。在MouseDown
中记录DateTime.Now
。如果在MouseUp
处理程序中:
DateTime.Now.Subtract(clickTime).TotalSeconds > your_seconds_value
然后触发一个新事件MouseClickedForXseconds
如果不想等待鼠标向上移动事件,则需要启动
MouseDown
方法上的计时器,该方法将触发MouseClickedForXSeconds
事件。此计时器将被鼠标移动事件取消。感谢您的提示,我制作了一个附加属性以避免任何代码隐藏:
using System;
using System.Windows;
using System.Windows.Threading;
/// <summary>
/// Represents a particular mouse button being pressed
/// </summary>
public enum MouseButtonType
{
/// <summary>
/// Default selection
/// </summary>
None,
/// <summary>
/// Left mouse button
/// </summary>
Left,
/// <summary>
/// Right mouse button
/// </summary>
Right,
/// <summary>
/// Either mouse button
/// </summary>
Both
}
/// <summary>
/// Provides functionality for detecting when a mouse button has been held
/// </summary>
public class MouseDownWait
{
/// <summary>
/// States which mouse button press should be detected
/// </summary>
public static readonly DependencyProperty MouseButtonProperty =
DependencyProperty.RegisterAttached(
"MouseButton",
typeof(MouseButtonType),
typeof(MouseDownWait),
new PropertyMetadata(
(o, e) =>
{
var ctrl = o as UIElement;
if (ctrl != null)
{
new MouseDownWait(ctrl);
}
}));
/// <summary>
/// The time (in milliseconds) to wait before detecting mouse press
/// </summary>
public static readonly DependencyProperty TimeProperty = DependencyProperty.RegisterAttached(
"Time", typeof(int), typeof(MouseDownWait), new FrameworkPropertyMetadata(0, OnTimePropertyChanged));
/// <summary>
/// Method to be called when the mouse press is detected
/// </summary>
public static readonly DependencyProperty DetectMethodProperty =
DependencyProperty.RegisterAttached(
"DetectMethod",
typeof(string),
typeof(MouseDownWait));
/// <summary>
/// Target object for the method calls (if not the datacontext)
/// </summary>
public static readonly DependencyProperty MethodTargetProperty =
DependencyProperty.RegisterAttached("MethodTarget", typeof(object), typeof(MouseDownWait));
/// <summary>
/// The timer used to detect mouse button holds
/// </summary>
private static readonly DispatcherTimer Timer = new DispatcherTimer();
/// <summary>
/// The element containing the attached property
/// </summary>
private readonly UIElement element;
/// <summary>
/// Initializes a new instance of the <see cref="MouseDownWait"/> class.
/// </summary>
/// <param name="element">The element.</param>
public MouseDownWait(UIElement element)
{
this.element = element;
if (this.element == null)
{
return;
}
this.element.MouseLeftButtonDown += ElementMouseLeftButtonDown;
this.element.MouseLeftButtonUp += ElementMouseLeftButtonUp;
this.element.MouseRightButtonDown += ElementMouseRightButtonDown;
this.element.MouseRightButtonUp += ElementMouseRightButtonUp;
this.element.MouseDown += ElementMouseDown;
this.element.MouseUp += ElementMouseUp;
Timer.Tick += this.TimerTick;
}
/// <summary>
/// Gets the mouse button type
/// </summary>
/// <param name="element">The element.</param>
/// <returns>
/// The mouse button type
/// </returns>
public static MouseButtonType GetMouseButton(UIElement element)
{
return (MouseButtonType)element.GetValue(MouseButtonProperty);
}
/// <summary>
/// Sets the mouse button type
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">The type of mouse button</param>
public static void SetMouseButton(UIElement element, MouseButtonType value)
{
element.SetValue(MouseButtonProperty, value);
}
/// <summary>
/// Gets the time.
/// </summary>
/// <param name="element">The element.</param>
/// <returns>The time in milliseconds</returns>
public static int GetTime(UIElement element)
{
return (int)element.GetValue(TimeProperty);
}
/// <summary>
/// Sets the time.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">The value.</param>
public static void SetTime(UIElement element, int value)
{
element.SetValue(TimeProperty, value);
}
/// <summary>
/// Sets the detect method
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">The value.</param>
public static void SetDetectMethod(UIElement element, string value)
{
element.SetValue(DetectMethodProperty, value);
}
/// <summary>
/// Gets the detect method
/// </summary>
/// <param name="element">The element.</param>
/// <returns>method name</returns>
public static string GetDetectMethod(UIElement element)
{
return (string)element.GetValue(DetectMethodProperty);
}
/// <summary>
/// Gets the method target.
/// </summary>
/// <param name="ctrl">The CTRL .</param>
/// <returns>method target (i.e. viewmodel)</returns>
public static object GetMethodTarget(UIElement ctrl)
{
var result = ctrl.GetValue(MethodTargetProperty);
if (result == null)
{
var fe = ctrl as FrameworkElement;
if (fe != null)
{
result = fe.DataContext;
}
}
return result;
}
/// <summary>
/// Sets the method target.
/// </summary>
/// <param name="ctrl">The CTRL .</param>
/// <param name="value">The value.</param>
public static void SetMethodTarget(UIElement ctrl, object value)
{
ctrl.SetValue(MethodTargetProperty, value);
}
/// <summary>
/// Called when the time property changes.
/// </summary>
/// <param name="d">The dependency object.</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void OnTimePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Timer.Interval = TimeSpan.FromMilliseconds((int)e.NewValue);
}
/// <summary>
/// Called when a mouse down is detected
/// </summary>
private static void MouseDownDetected()
{
Timer.Start();
}
/// <summary>
/// Called when a mouse up is detected
/// </summary>
private static void MouseUpDetected()
{
Timer.Stop();
}
/// <summary>
/// Checks if the mouse button has been detected.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="type">The mouse button type.</param>
/// <param name="mouseDown">if set to <c>true</c> [mouse down].</param>
private static void CheckMouseDetected(object sender, MouseButtonType type, bool mouseDown)
{
var uiElement = sender as UIElement;
if (uiElement == null)
{
return;
}
if (GetMouseButton(uiElement) != type)
{
return;
}
if (mouseDown)
{
MouseDownDetected();
}
else
{
MouseUpDetected();
}
}
/// <summary>
/// Called when the mouse down event fires
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private static void ElementMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
CheckMouseDetected(sender, MouseButtonType.Both, true);
}
/// <summary>
/// Called when the mouse up event fires
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private static void ElementMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
CheckMouseDetected(sender, MouseButtonType.Both, false);
}
/// <summary>
/// Called when the left mouse down event fires
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private static void ElementMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
CheckMouseDetected(sender, MouseButtonType.Left, true);
}
/// <summary>
/// Called when the left mouse up event fires
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private static void ElementMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
CheckMouseDetected(sender, MouseButtonType.Left, false);
}
/// <summary>
/// Called when the right mouse down event fires
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private static void ElementMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
CheckMouseDetected(sender, MouseButtonType.Right, true);
}
/// <summary>
/// Called when the right mouse up event fires
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private static void ElementMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
CheckMouseDetected(sender, MouseButtonType.Right, false);
}
/// <summary>
/// Called on each timer tick
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void TimerTick(object sender, EventArgs e)
{
Timer.Stop();
var method = GetDetectMethod(this.element);
if (!string.IsNullOrEmpty(method))
{
this.InvokeMethod(method);
}
}
/// <summary>
/// Invokes the method.
/// </summary>
/// <param name="methodName">Name of the method.</param>
/// <param name="parameters">The parameters.</param>
private void InvokeMethod(string methodName, params object[] parameters)
{
var target = GetMethodTarget(this.element);
var targetMethod = target.GetType().GetMethod(methodName);
if (targetMethod == null)
{
throw new MissingMethodException(methodName);
}
targetMethod.Invoke(target, parameters);
}
}
使用系统;
使用System.Windows;
使用System.Windows.Threading;
///
///表示正在按下的特定鼠标按钮
///
公共枚举鼠标按钮类型
{
///
///默认选择
///
没有一个
///
///鼠标左键
///
左边
///
///鼠标右键
///
正确的,
///
///任一鼠标按钮
///
二者都
}
///
///提供检测何时按住鼠标按钮的功能
///
公共类MouseDownWait
{
///
///说明应检测哪些鼠标按钮按下
///
公共静态只读DependencyProperty MouseButtonProperty=
DependencyProperty.RegisterAttached(
“鼠标按钮”,
类型(鼠标按钮类型),
typeof(MouseDownWait),
新属性元数据(
(o,e)=>
{
var ctrl=o作为UIElement;
如果(ctrl!=null)
{
新建鼠标向下等待(ctrl);
}
}));
///
///检测到鼠标按下之前等待的时间(以毫秒为单位)
///
公共静态只读DependencyProperty TimeProperty=DependencyProperty.RegisterAttached(
“时间”、typeof(int)、typeof(MouseDownWait)、新的FrameworkPropertyMetadata(0,OnTimePropertyChanged));
///
///检测到鼠标按下时要调用的方法
///
公共静态只读从属属性检测方法属性=
DependencyProperty.RegisterAttached(
“检测方法”,
类型(字符串),
类型(MouseDownWait));
///
///方法调用的目标对象(如果不是datacontext)
///
公共静态只读DependencyProperty方法TargetProperty=
DependencyProperty.RegisterAttached(“MethodTarget”、typeof(object)、typeof(MouseDownWait));
///
///用于检测鼠标按钮保持的计时器
///
专用静态只读分派器计时器=新分派器();
///
///包含附加属性的元素
///
私有只读UIElement;
///
///初始化类的新实例。
///
///元素。
公共MouseDownWait(UIElement元素)
{
this.element=元素;
if(this.element==null)
{
返回;
}
this.element.MouseLeftButtonDown+=ElementMouseLeftButtonDown;
this.element.MouseLeftButtonUp+=ElementMouseLeftButtonUp;
this.element.MouseRightButtonDown+=ElementMouseRightButtonDown;
this.element.MouseRightButtonUp+=ElementMouseRightButtonUp;
this.element.MouseDown+=ElementMouseDown;
this.element.MouseUp+=ElementMouseUp;
Timer.Tick+=this.TimerTick;
}
///
///获取鼠标按钮类型
///
///元素。
///
///鼠标按钮类型
///
公共静态鼠标按钮类型GetMouseButton(UIElement元素)
{
return(MouseButtonType)元素.GetValue(MouseButtonProperty);
}
///
///设置鼠标按钮类型
///
///元素。
///鼠标按钮的类型
公共静态void SetMouseButton(UIElement元素,MouseButtonType值)
{
SetValue(MouseButtonProperty,value);
}
///
///得到时间。
///
///元素。
///时间以毫秒为单位
公共静态int-GetTime(UIElement)
{
return(int)元素.GetValue(TimeProperty);
}
///
///设定时间。
///
///元素。
///价值。
公共静态void设置时间(UIElement,int值)
{
元素.SetValue(TimeProperty,value);
}
///
///设置检测方法
///
///元素。
///价值。
公共静态void SetDetectMethod(UIElement元素,字符串值)
{
元素设置值(DetectMethodProperty,value);
}
///
///获取检测方法
///
///元素。
///方法名
公共静态字符串GetDetectMethod(UIElement)
{
返回(字符串)元素.GetValue(DetectMethodProperty);
}
///
///获取方法目标。
///
///按下CTRL键。
///方法目标(即viewmodel)
公共静态对象GetMethodTarget(UIElement ctrl)
{
var result=ctrl.GetValue(MethodTargetProperty);
如果(结果==null)
{
var fe=ctrl作为框架元素;
如果(fe!=null)
{
结果=fe.DataContext;
}
}
返回结果;
}
///
///设置方法目标。
///
///按下CTRL键。
///价值。
公共静态void SetMethodTarget(UIElement ctrl,对象值)
{
ctrl.SetValue(MethodTargetProperty,value);
}
///
///当时间属性更改时调用。
///
///依赖项对象。
///包含事件数据的实例。
私有静态void OnTimePropertyChanged(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
Timer.Interval=TimeSpan.frommilluses((int)e.NewValue);
}
/
<Border BorderBrush="Gray" BorderThickness="1" Height="200" Width="200" Background="Transparent"
local:MouseDownWait.MouseButton="Both"
local:MouseDownWait.Time="1000"
local:MouseDownWait.DetectMethod="MouseDetected">
<TextBlock x:Name="Title" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextAlignment="Center" FontSize="28" />
</Border>