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

I'm developing an apllication in WinUI 3 and C++, but I'm stuck with a problem, but I think i

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745158807a4614284.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信