最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

winui 3 - How to get pointer position from PointerRoutedEventArgs en C++WinRT - Stack Overflow

programmeradmin0浏览0评论

I'm developing an apllication in WinUI 3 and C++, but I'm stuck with a problem, but I think it must be something simple. I try to get the mouse position in the event OnPointerPressed. Here is my code for the handler function:

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);
    winrt::Windows::Foundation::Point pointerPosition = pp.Position();     // ERROR 

    // More code...
}

I assign the event over a TextBlock and try to get the coordinates of muse click relative to that TextBlock, but this code doesn't compile. They tell me an error in the call to pp.Position():

C3779 'winrt::impl::consume_Windows_UI_Input_IPointerPointwinrt::Windows::UI::Input::IPointerPoint::Position': Can't use a function returning 'auto' before being defined'

Maybe it is simple, but I can't see how to get click position.

Thanks!

I'm developing an apllication in WinUI 3 and C++, but I'm stuck with a problem, but I think it must be something simple. I try to get the mouse position in the event OnPointerPressed. Here is my code for the handler function:

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);
    winrt::Windows::Foundation::Point pointerPosition = pp.Position();     // ERROR 

    // More code...
}

I assign the event over a TextBlock and try to get the coordinates of muse click relative to that TextBlock, but this code doesn't compile. They tell me an error in the call to pp.Position():

C3779 'winrt::impl::consume_Windows_UI_Input_IPointerPointwinrt::Windows::UI::Input::IPointerPoint::Position': Can't use a function returning 'auto' before being defined'

Maybe it is simple, but I can't see how to get click position.

Thanks!

Share Improve this question edited Feb 23 at 10:35 wohlstad 30.3k17 gold badges61 silver badges94 bronze badges asked Feb 23 at 9:49 Vicente FlichVicente Flich 294 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Well, I achieve to compile. My mistake I think were in the spacename, because I used winrt::Windows::... when I need to use winrt::Microsoft::... for winui 3. Besides, I have include the correct header:

    #include <winrt/Microsoft.UI.Input.h>
    
    void MyWindow:OnPointerPressed(IInspectable const& sender, PointerRoutedEventArgs const& e)
    {
        auto tb = sender.try_as<TextBlock>();
        if (!tb) return;
    
        winrt::Microsoft::UI::Input::PointerPoint pp = e.GetCurrentPoint(tb);
        winrt::Microsoft::Foundation::Point pointerPosition = pp.Position();     // ERROR 
    
        // More code...
    }

and it worked!

You need to use pp.Position without parentheses. Since Position is a property, you don't use parentheses. Try this:

winrt::Windows::Foundation::Point pointerPosition = pp.Position;
发布评论

评论列表(0)

  1. 暂无评论