获取触摸坐标处的 UI 元素

发布于 2024-12-28 23:57:32 字数 78 浏览 0 评论 0原文

我确信我已经读过,有一种方法可以在 WPF 应用程序中获取 TouchDown 上的坐标,并找出此触摸“下方”的 UI 元素。有人可以帮忙吗?

I'm sure I've read that there is a way of getting co-ordinates on TouchDown in a WPF app and finding out what UI elements are 'underneath' this touch. Can anyone help?

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

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

发布评论

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

评论(2

蓝天白云 2025-01-04 23:57:32

您必须对可视化树进行命中测试

以下是鼠标单击的示例(但触摸在这方面或多或少是相同的):

// Respond to the left mouse button down event by initiating the hit test.
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // Perform the hit test against a given portion of the visual object tree.
    HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);

    if (result != null)
    {
        // Perform action on hit visual object.
    } 
}

HitTest 可以为您提供多种点击视觉效果。

You have to do a hit test against the visual tree.

Here is an example with mouse click (but touch is more or less the same thing in this respect):

// Respond to the left mouse button down event by initiating the hit test.
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // Perform the hit test against a given portion of the visual object tree.
    HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);

    if (result != null)
    {
        // Perform action on hit visual object.
    } 
}

Other overload of HitTest can give you multiple hit visuals.

剩一世无双 2025-01-04 23:57:32

让你的UI元素像这样扩展UIElement类:

class MyUIElement : UIElement
    {
        protected override void OnManipulationStarting(System.Windows.Input.ManipulationStartingEventArgs e)
        {
            base.OnManipulationStarting(e);
            UIElement involvedUIElement = e.Source as UIElement;

            // to cancel the touch manipulaiton:
            e.Cancel();
        }
    }

涉及的UIElement应该保存引发触摸事件的UI元素,如果你需要取消对特定元素的操作,你只需要调用e.Cancel();

我希望这会有所帮助!

Let your UI elements extend UIElement class like this:

class MyUIElement : UIElement
    {
        protected override void OnManipulationStarting(System.Windows.Input.ManipulationStartingEventArgs e)
        {
            base.OnManipulationStarting(e);
            UIElement involvedUIElement = e.Source as UIElement;

            // to cancel the touch manipulaiton:
            e.Cancel();
        }
    }

involvedUIElement should hold the UI elements that raised the touch event, if you need to cancel the manipulation for specific elements you just need to call e.Cancel();

I hope this will help!

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