Is it possible to change mouse pointer in UWP app
Asked Answered
M

5

18

Is it possible to change or even hide the mouse-pointer in a UWP app? The only thing I can find is this :

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

But in UWP, this doesn't work.

Mineralize answered 22/6, 2016 at 0:17 Comment(0)
B
17

Yes this can be done by settings the Window.Current.CoreWindow.PointerCursor. If you set it to null, the pointer is hidden. Otherwise you can use the CoreCursorType enumeration to set a specific system point. For instance use this to set the Arrow type:

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

You can also add custom pointers by using a resource file. For details, see this blogpost.

Blimey answered 22/6, 2016 at 13:2 Comment(1)
I think to custom pointer is too tedious and how do I easy to custom pointer.Margarito
T
19

No this is not possible to hide cursor but you can use another icons like:

  • Hand
  • Arrow
  • Cross
  • Custom
  • Hand
  • Help
  • IBeam

Use xaml Button and add PointerEntered event inside Button Control like:

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

and c# code:

 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);
    }
Thumbstall answered 22/6, 2016 at 4:9 Comment(2)
do you know any way to do this completely in xaml without using code behind?Hyperbola
Short, Simple, fab explanation! +1Slyke
B
17

Yes this can be done by settings the Window.Current.CoreWindow.PointerCursor. If you set it to null, the pointer is hidden. Otherwise you can use the CoreCursorType enumeration to set a specific system point. For instance use this to set the Arrow type:

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

You can also add custom pointers by using a resource file. For details, see this blogpost.

Blimey answered 22/6, 2016 at 13:2 Comment(1)
I think to custom pointer is too tedious and how do I easy to custom pointer.Margarito
C
11

Install the NuGet Package Microsoft.Toolkit.Uwp.UI from the Windows Community Toolkit.

After doing so, you can use the following code:

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

<UIElement extensions:Mouse.Cursor="Hand"/>
Chub answered 10/7, 2018 at 14:59 Comment(1)
The code above is outdated. Read the updated version here ( learn.microsoft.com/en-us/windows/communitytoolkit/extensions/… ) or the answer by @NoWar down below.Xebec
E
2

This example from 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>
Elliellicott answered 8/10, 2021 at 3:58 Comment(0)
P
1

You can hide the cursor in UWP (C++ example)

            Windows::UI::Core::CoreWindow^ window = Windows::UI::Core::CoreWindow::GetForCurrentThread();
            window->PointerPosition = Point(ScreenXMid, ScreenYMid);
            window->PointerCursor = nullptr;
Purloin answered 3/2, 2019 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.