【发布时间】:2022-01-20 04:50:08
【问题描述】:
是否可以在 UWP 应用中更改甚至隐藏鼠标指针? 我唯一能找到的是这个:
Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = null;
但在 UWP 中,这不起作用。
【问题讨论】:
标签: uwp mouse-cursor
是否可以在 UWP 应用中更改甚至隐藏鼠标指针? 我唯一能找到的是这个:
Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = null;
但在 UWP 中,这不起作用。
【问题讨论】:
标签: uwp mouse-cursor
是的,这可以通过设置
Window.Current.CoreWindow.PointerCursor。如果将其设置为 null,则指针将被隐藏。否则,您可以使用CoreCursorType 枚举来设置特定的系统点。例如使用它来设置箭头类型:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 0);
您还可以使用资源文件添加自定义指针。详情请see this blogpost。
【讨论】:
不,这不可能隐藏光标,但您可以使用其他图标,例如:
使用 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);
}
【讨论】:
从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>
【讨论】: