鼠标点击和DoubleClick之间的区别鼠标点击、区别、DoubleClick

2023-09-04 00:47:56 作者:久溺深海心会寒

WPF,形状

WPF, Shapes

我有一个自定义的图形,并尝试实施鼠标点击 MouseDoubleClick 的东西就可以了。

I have a custom Shape, and try to implement MouseClick and MouseDoubleClick stuff on it.

通过如。我需要的打开信息窗口的MyShape的左右的OnClick和选择的它OnDoubleClick。

By eg. I need to open a information window about MyShape OnClick and select it OnDoubleClick.

存根如下:

public class MyShape : Shape
{
    public Point Point1, Point2;

    public MyShape() : base() { }

    protected override Geometry DefiningGeometry
    {
        get { return new LineGeometry(Point1, Point2); }
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        if (e.ClickCount == 1)
        {
            // Open Informational Window
        }
        else if (e.ClickCount == 2)
        {
            // Select Item
        }
    }
}

我每次双击两个 //只有做到鼠标点击 //只有做到MouseDoubleClick 发生。

有没有办法避免这种情况在WPF,.NET 4?

Is there any way to avoid this in WPF, .NET 4?

推荐答案

之间的第一次点击和第二单击双击我觉得有一个时间约50至400毫秒,所以波纹管code是不是最好的但这样的伎俩:

Between first click and second click in double click I think there is a time around 50 and 400 millisecond, So the bellow code is not a best one but do the trick:

bool firstClicked = false;

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        if (e.ClickCount == 1)
        {
            firstClicked = true;
            Task tsk = new TaskFactory()
                            .StartNew(
                            () => 
                                {
                                    Thread.Sleep(SystemInformation.DoubleClickTime);
                                    if (firstClicked)
                                    {
                                        // if you want to use `e`
                                        // e.Source
                                        // Open Informational Window
                                    }
                                        firstClicked = false;
                                    }
                                        );

                                }

        }
        else if (e.ClickCount == 2)
        {
            firstClicked = false;
            // Select Item
        }
    }