Check time after a mousebuttondown before the mousebuttonup

狂风中的少年 提交于 2019-12-25 04:31:54

问题


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

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.

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?


回答1:


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);
}

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);

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

_watch = Stopwatch.StartNew();

in OnMouseLeftButtonUp:

_watch.Stop();

and in OnMouseMove before your code:

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


来源:https://stackoverflow.com/questions/31587046/check-time-after-a-mousebuttondown-before-the-mousebuttonup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!