鼠标按下和点击冲突

鼠标按下和点击冲突

本文介绍了鼠标按下和点击冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有form_MouseDown事件拖动()方法。我也有在窗体上的单击事件。问题是,如果我点击的形式,MouseDown事件被触发,它永远不会触发click事件的机会。



什么是解决这一问题的最佳方式是什么?我在想计数的像素,如果形式实际上是拖,但必须有一个更好的办法。有什么建议么?


解决方案

Nope, that's exactly how you have to do it.

This isn't just a software limitation; it's very much a practical one as well. If you think through the problem from a user's perspective, you'll immediately see the problem as well as the solution. Ask yourself, what is the difference between a click and a drag?

Both of them start with the mouse button going down over the object, but one of them ends with the mouse button going back up over the object in the same position and the other one ends with the mouse button going back up in a completely different position.

Since time machines haven't been perfected yet, you have no way of knowing this in advance.

So yes, you need to maintain some kind of a distance threshold, and if the pointer moves outside of that distance threshold while it is down over the object, then you consider it a drag. Otherwise, you consider it a click.

That distance threshold should not be 0. The user should not be required to hold the mouse completely still in order to initiate a drag. A lot of users are sub-par mousers. They are very likely to twitch slightly when trying to click. If the threshold is 0, they'll end up doing a lot of inadvertent dragging when they try to click.

Of course, you don't actually have to worry about any of this or compute the drag threshold yourself. Instead, use the Windows default values, obtainable by calling the GetSystemMetrics function and specifying either SM_CXDRAG or SM_CYDRAG. (These might be exposed somewhere by the WinForms framework, but I don't think so. It's just as easy to P/Invoke them yourself.)

const int SM_CXDRAG = 68;
const int SM_CYDRAG = 69;
[DllImport("user32.dll")]
static extern int GetSystemMetrics(int index);

Point GetDragThreshold()
{
    return new Point(GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
}

这篇关于鼠标按下和点击冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:45