是否可以在 UWP 应用程序中更改鼠标指针

Is it possible to change mouse pointer in UWP app

是否可以更改甚至隐藏 UWP 应用程序中的鼠标指针? 我唯一能找到的是这个:

Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = null;

但在 UWP 中,这不起作用。

不,这不可能隐藏光标,但您可以使用其他图标,例如:

  • 箭头
  • 交叉
  • 自定义
  • 帮助
  • IBeam

使用 xaml 按钮并在按钮控件中添加 PointerEntered 事件,例如:

<Button Name="button"  BorderThickness="2" PointerEntered="button_PointerEntered"  PointerExited="button_PointerExited">Button</Button>

和 C# 代码:

 private void button_PointerEntered(object sender, PointerRoutedEventArgs e)
    {
        Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 1);
    }
    private void button_PointerExited(object sender, PointerRoutedEventArgs e)
    {
        Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 1);
    }

是的,这可以通过设置 Window.Current.CoreWindow.PointerCursor。如果将其设置为 null,则隐藏指针。否则,您可以使用 CoreCursorType 枚举来设置特定的系统点。例如使用它来设置箭头类型:

Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 0);

您还可以使用资源文件添加自定义指针。详情请see this blogpost.

Windows Community Toolkit 安装 NuGet 包 Microsoft.Toolkit.Uwp.UI

这样做之后,你可以使用下面的code:

<Page ...
 xmlns:extensions="using:Microsoft.Toolkit.Uwp.UI.Extensions">

<UIElement extensions:Mouse.Cursor="Hand"/>

您可以在 UWP 中隐藏光标(C++ 示例)

            Windows::UI::Core::CoreWindow^ window = Windows::UI::Core::CoreWindow::GetForCurrentThread();
            window->PointerPosition = Point(ScreenXMid, ScreenYMid);
            window->PointerCursor = nullptr;

这个例子来自Microsoft Community Toolkit

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:ui="using:Microsoft.Toolkit.Uwp.UI">

    <Grid>
        <Border Width="220"
                Height="120"
                Margin="50,100,20,20"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                ui:FrameworkElementExtensions.Cursor="UniversalNo"
                Background="DeepSkyBlue">
            <TextBlock Margin="4"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Text="Element with UniversalNo cursor"
                       TextWrapping="Wrap" />
        </Border>
        <Border Width="220"
                Height="120"
                Margin="20"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                ui:FrameworkElementExtensions.Cursor="Wait"
                Background="Orange">
            <TextBlock Margin="4"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Text="Element with Wait cursor"
                       TextWrapping="Wrap" />
        </Border>

        <Button Margin="20,240,20,20"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Content="Standard button with no custom cursor" />
        <Button Margin="20,290,20,20"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                ui:FrameworkElementExtensions.Cursor="Hand"
                Content="Button with Hand cursor, just like on web" />
    </Grid>
</Page>