检查鼠标按下后到鼠标按下前的时间 [英] Check time after a mousebuttondown before the mousebuttonup

查看:135
本文介绍了检查鼠标按下后到鼠标按下前的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为那应该只是一个小问题,但是我对此没有明确的想法.有人有主意吗?

I think that must be only a little problem, but I can't get a clear thought on that. Someone an idea?

我在画布上有一些边框(充满图像),我想单击边框(我使用OnMouseLeftButtonDown进行此操作),边框变为红色(因此用户可以确定他单击了哪个对象),然后,在1或2秒钟后,当仍然按下鼠标按钮时,应该开始拖放操作.

I have some borders on a canvas (filled with images) and i want to click the border (i do this with the OnMouseLeftButtonDown) where the border gets red (so the user knows for sure which object he had clicked) and then, after 1 or 2 seconds, when the mousebutton is still pushed down, a drag'n'drop should start.

起初,我在按钮内部有边框,但是clickevent似乎与dragevent冲突.因此,我摆脱了按钮,直接在边框内进行了所有操作,效果也很好.但是,如何在鼠标按钮按下后开始拖动,并在时间用完之前发生鼠标按钮按下时停止拖动.

At first I had the borders inside buttons, but the clickevent seems to conflict with the dragevent. So I got rid of the buttons and did everything inside the borders directly, which works well also. But how can I start the drag after the mousebuttondown and stop it when the mousebuttonup happens before the time runs out.

有人提出清洁解决方案的想法吗?

Someone an idea for a clean solution?

推荐答案

private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _source = sender as Border;
    Mouse.Capture(_source);
}

private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    _source = null;
    Mouse.Capture(null);
}

并且在OnMouseMove事件中,您可以根据鼠标位置修改边框边距,但是在检查_source是否为Border类型之后.

and in event OnMouseMove you can modify border margins depends on mouse position but after checking if _source is type of Border.

var position = e.GetPosition(Canvas);

这段时间,您可以添加属性Stopwatch _watch,并且可以在OnMouseLeftButtonDown事件处理程序内部执行此操作:

For that time you can add property Stopwatch _watch, and inside OnMouseLeftButtonDown event handler you can do it:

_watch = Stopwatch.StartNew();

在OnMouseLeftButtonUp中:

in OnMouseLeftButtonUp:

_watch.Stop();

,并在代码之前的OnM​​ouseMove中:

and in OnMouseMove before your code:

while (_watch.ElapsedMilliseconds < 2000 && _watch.IsRunning)
{
    Thread.Sleep(100);
}

这篇关于检查鼠标按下后到鼠标按下前的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆