如何在WinUI 3中更改鼠标指针的指针?

huangapple go评论121阅读模式
英文:

How can i change the pointer of my cursor in winui3?

问题

I want to change my cursor ideally making it invisible but would settle for cross when it enters/exits a specified area.

After some research, I saw Window.Current.CoreWindow.PointerCursor always returns null for desktop which is what I use and so it just doesn't work. I've seen you can use reflection but I couldn't find any examples I could get to work, unfortunately. I was hoping someone could help me out.

private void Canvas_PointerEntered(object sender, PointerRoutedEventArgs e)
{

}

private void Canvas_PointerExited(object sender, PointerRoutedEventArgs e)
{

}

英文:

I want to change my cursor ideally making it invisible but would settle for cross when it enters/exits a specified area.

After some research, I saw Window.Current.CoreWindow.PointerCursor always returns null for desktop which is what I use and so it just doesn't work. I've seen you can use reflection but I couldn't find any examples I could get to work, unfortunately. I was hoping someone could help me out.

    private void Canvas_PointerEntered(object sender, PointerRoutedEventArgs e)
    {
      
       
        
    }
   
    private void Canvas_PointerExited(object sender, PointerRoutedEventArgs e)
    {
      
        
        
     
    }

答案1

得分: 1

为了想要答案的人:

由于我们需要访问Grid中的ProtectedCursor属性,它是Grid中的一个protected属性,我们需要创建一个自定义的Grid

public class CustomGrid : Grid
{
    public InputCursor InputCursor
    {
        get => ProtectedCursor;
        set => ProtectedCursor = value;
    }
}

CustomGrid用作要更改鼠标光标的区域。您需要设置Background以使其生效。

<local:CustomGrid
    x:Name="CustomGrid"
    Background="Transparent"
    PointerEntered="CustomGrid_PointerEntered"
    PointerExited="CustomGrid_PointerExited">
    <!-- 在这里放入您的内容。 -->
</local:CustomGrid>

然后在代码后台:

private InputCursor? OriginalInputCursor { get; set; }

private void CustomGrid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    OriginalInputCursor = this.CustomGrid.InputCursor ?? InputSystemCursor.Create(InputSystemCursorShape.Arrow);
    this.CustomGrid.InputCursor = InputSystemCursor.Create(InputSystemCursorShape.UniversalNo);
}

private void CustomGrid_PointerExited(object sender, PointerRoutedEventArgs e)
{
    if (OriginalInputCursor != null)
    {
        this.CustomGrid.InputCursor = OriginalInputCursor;
    }
}
英文:

For the people who want the answer:

Since we need to access the ProtectedCursor property, which is a protected property in Grid, we need to create a custom Grid.

public class CustomGrid : Grid
{
    public InputCursor InputCursor
    {
        get =&gt; ProtectedCursor;
        set =&gt; ProtectedCursor = value;
    }
}

Use the CustomGrid as the area you want to change the mouse cursor. You need to set the Background to make this work.

&lt;local:CustomGrid
    x:Name=&quot;CustomGrid&quot;
    Background=&quot;Transparent&quot;
    PointerEntered=&quot;CustomGrid_PointerEntered&quot;
    PointerExited=&quot;CustomGrid_PointerExited&quot;&gt;
    &lt;!--  your contents here.  --&gt;
&lt;/local:CustomGrid&gt;

then in code-behind:

private InputCursor? OriginalInputCursor { get; set; }

private void CustomControl_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    OriginalInputCursor = this.CustomGrid.InputCursor ?? InputSystemCursor.Create(InputSystemCursorShape.Arrow);
    this.CustomGrid.InputCursor = InputSystemCursor.Create(InputSystemCursorShape.UniversalNo);
}

private void CustomGrid_PointerExited(object sender, PointerRoutedEventArgs e)
{
    if (OriginalInputCursor != null)
    {
        this.CustomGrid.InputCursor = OriginalInputCursor;
    }
}

huangapple
  • 本文由 发表于 2023年6月29日 12:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定