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



是否有可能在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);

您还可以使用资源文件添加自定义指针。有关详细信息,请参阅此博客文章。

从Windows社区工具包中安装Nuget软件包 Microsoft.toolkit.uwp.ui

这样做后,您可以使用以下代码:

<Page ...
 xmlns:extensions="using:Microsoft.Toolkit.Uwp.UI.Extensions">
<UIElement extensions:Mouse.Cursor="Hand"/>

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>

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

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

最新更新