You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++/WinRT中如何从PointerRoutedEventArgs获取指针位置?

解决WinUI 3 C++中获取相对TextBlock鼠标点击位置的编译错误

你的编译错误是因为把WinRT属性当成方法调用了。在C++/WinRT中,PointerPoint的Position是属性,不是方法,不需要加括号()。

修正后的代码:

void MyWindow::OnPointerPressed(IInspectable const& sender, PointerRoutedEventArgs const& e)
{
    auto tb = sender.try_as<TextBlock>();
    if (!tb) return;

    winrt::Windows::UI::Input::PointerPoint pp = e.GetCurrentPoint(tb);
    // 直接访问Position属性,而非调用方法
    winrt::Windows::Foundation::Point pointerPosition = pp.Position;

    // 后续代码...
}

说明:

  • WinRT API的属性在C++/WinRT绑定中是以成员变量的形式暴露的,只需要直接访问即可,不需要像C#那样加括号调用方法。
  • e.GetCurrentPoint(tb)的用法是正确的,它会返回相对于传入的TextBlock控件的坐标,修正后就能正常获取点击位置。

内容的提问来源于stack exchange,提问作者Vicente Flich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:52:01