在 WPF 中,如何捕获 Canvas 上的鼠标并仍然找到光标悬停在哪些控件上?

发布于 2024-10-03 09:01:50 字数 436 浏览 0 评论 0原文

我有一个自定义 Canvas 控件(继承自 Canvas),覆盖在大面积的用户控件上。这个想法是在用户控件之间绘制路径(即连接线)。

为了捕获鼠标移动,我在 MouseDown 上调用 Mouse.Capture(theCanvas)。这工作得很漂亮,但是画布下的用户控件显然不再接收鼠标事件。 Mouse.DirectlyOver 始终显示画布,因此我无法通过查看当前位置并查看它结束于哪个用户控件来真正伪造它。

因此,我仍然需要 Canvas 来绘制路径,但是如何通过以下方法之一解决此问题:

  1. 在 Canvas 下查看并查看其下方最上面的控件是什么?
  2. 获取此 MouseDown ->跟踪鼠标移动 -> MouseUp 工作流程无需鼠标捕获即可在画布上工作?

任何其他想法欢迎...

I have a custom Canvas control (inherited from Canvas) overlaid over a large area of User Controls. The idea is to draw paths between user controls (i.e. connector lines).

To capture mouse movement, I call Mouse.Capture(theCanvas) on MouseDown. This works beautifully, but the user controls under the canvas obviously no longer receive mouse events. Mouse.DirectlyOver always shows the canvas, so I can't really fake it by peeking at the current position and seeing which user control it's over.

So, I still need the Canvas for drawing paths, but how can I solve this one of the following ways:

  1. Peek under the Canvas and see what the topmost control is right under it?
  2. Get this MouseDown -> Track MouseMoves -> MouseUp workflow to work on the canvas without mouse captures?

Any other ideas welcome...

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

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

发布评论

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

评论(1

小草泠泠 2024-10-10 09:01:50

我同意这是你的两个选择。如果您只想将一些点击转发到用户控件,请选择选项 1,并点击测试画布下的控件。

如果您需要用户控件的行为就像没有任何东西覆盖它们(文本框、按钮等),那么我建议使用 PreviewMouseMove 用户控件父控件上的事件,因为这可以在控件到达事件之前拾取并可选择“处理”事件,但它不会如果未将handle 设置为 true,则不会阻止该事件

I'd agree that those are your two options. If you want to only forward some clicks to your usercontrols, then go with option 1, and hit test the controls under the canvas.

If you need your usercontrols to behave as though there is nothing covering them (textboxes, buttons etc), then i'd recommend using the PreviewMouseMove event on the user control's parent, as this can pick up and optionally "handle" events before the controls get at the event, but it won't block the event if you don't set handled to true

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