WPF 中忽略单击

发布于 2024-10-01 15:47:36 字数 943 浏览 0 评论 0原文

我有一个用户控件,其中我想在单击与双击的情况下执行不同的操作。我正在处理 mouseLeftButtonDown 事件,如下所示:

 private void cueCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        switch (e.ClickCount)
        {
            case 1:
                {
                    cueCanvas.Focus();
                    e.Handled = true;
                    _mouseStartPoint = Mouse.GetPosition(null);
                    break;
                }
            case 2:
                {
                    double pos = Mouse.GetPosition(cueCanvas).X;
                    TimeSpan time = ConvertFromPosition(pos);
                    AddNewEvent(time);
                    e.Handled = true;
                    break;
                }
        }
    }

问题是 WPF 并不真正关心您单击鼠标的次数,因此即使我即将获得第二个单击事件,我也会获得第一个单击事件(在这种情况下)双击)。有没有人想出一种方法来解决这个问题?我意识到我可以尝试变得聪明并设置一些计时器,以便在第二次点击进入时第一次点击被“取消”(我意识到这就是框架无论如何都会做的事情)。如果这是唯一的答案,有谁知道我们如何查询系统以获得适当的点击延迟?

I have a user control wherein I would like to do something different in the case of a single click vs. double click. I'm handling mouseLeftButtonDown event as such:

 private void cueCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        switch (e.ClickCount)
        {
            case 1:
                {
                    cueCanvas.Focus();
                    e.Handled = true;
                    _mouseStartPoint = Mouse.GetPosition(null);
                    break;
                }
            case 2:
                {
                    double pos = Mouse.GetPosition(cueCanvas).X;
                    TimeSpan time = ConvertFromPosition(pos);
                    AddNewEvent(time);
                    e.Handled = true;
                    break;
                }
        }
    }

The problem is that WPF doesn't really care how many times you've clicked the mouse so I get the first click event even though I'm about to get a second one (in the case of a double-click). Has anyone come up with a way to work around this? I realize I could try to get clever and set some timers such that the first click gets "canceled" in the event that the second one comes in (I realize this is what the framework would be doing anyway). If that's the only answer, does anyone know how we query the system for the appropriate click delay?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错爱 2024-10-08 15:47:36

据我所知,确实没有办法做到这一点。问题在于你正在与现实作斗争。 :-) 实际上,双击需要点击 2 次。如果快速双击,您希望忽略单击。

您需要等待一小段时间才能查看是否有第二次点击。您可以使用 SystemInformation 查询该间隔来自 WinForms 的类,或者直接调用 Win32 API

To my knowledge, there's really no way to do this. The problem is that you're fighting reality. :-) In reality, there are 2 clicks to a double-click. You want single click to be ignored if quickly followed by a double-click.

You'll have wait a short interval to see if it's followed by a second click. You can query for that interval using the SystemInformation class from WinForms, or just call the Win32 API directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文