WPF 中忽略单击
我有一个用户控件,其中我想在单击与双击的情况下执行不同的操作。我正在处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,确实没有办法做到这一点。问题在于你正在与现实作斗争。 :-) 实际上,双击需要点击 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.