英文:
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 => ProtectedCursor;
set => 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.
<local:CustomGrid
x:Name="CustomGrid"
Background="Transparent"
PointerEntered="CustomGrid_PointerEntered"
PointerExited="CustomGrid_PointerExited">
<!-- your contents here. -->
</local:CustomGrid>
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论