Check time after a mousebuttondown before the mous

2019-08-18 08:30发布

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条回答
贼婆χ
2楼-- · 2019-08-18 08:59
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);
}
查看更多
登录 后发表回答