【问题标题】:Is it possible to change mouse pointer in UWP app是否可以在 UWP 应用程序中更改鼠标指针
【发布时间】:2022-01-20 04:50:08
【问题描述】:

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

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

但在 UWP 中,这不起作用。

【问题讨论】:

    标签: uwp mouse-cursor


    【解决方案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

    【讨论】:

    • 我觉得自定义指针太繁琐了,如何轻松自定义指针。
    【解决方案2】:

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

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

    使用 xaml Button 并在 Button Control 中添加 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);
        }
    

    【讨论】:

    • 你知道有什么方法可以在 xaml 中完全做到这一点,而无需使用后面的代码吗?
    • 简短、简单、精彩的解释! +1
    【解决方案3】:

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

    这样做后,您可以使用以下code

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        这个例子来自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>
        

        【讨论】:

          猜你喜欢
          • 2013-03-22
          • 2012-04-15
          • 1970-01-01
          • 2021-09-07
          • 2013-09-24
          • 1970-01-01
          • 2011-06-01
          • 2017-07-03
          • 2017-01-07
          相关资源
          最近更新 更多