How do I use a Pack Icon as a mouse cursor?

Refresh

February 2019

Views

0 time

1

I have the following code which I'm using to provide mouse cursor feedback on a drag and drop operation. It uses a local cursor file.

private void UserControl_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    if (e.Effects == DragDropEffects.None)
    {
        e.UseDefaultCursors = true;
        e.Handled = true;
        return;
    }

    if (cursor == null)
    {
        StreamResourceInfo s = Application.GetResourceStream(new Uri(@"pack://application:,,,/Schedule/Week/ContentCopy.cur", UriKind.RelativeOrAbsolute));
        cursor = new Cursor(s.Stream);
        Mouse.SetCursor(cursor);
        e.UseDefaultCursors = false;
    }
    e.Handled = true;
}

Now I want to change this code to use a Pack Icon from the Material Design in Xaml library.

I can get the icon like this in code:

using MaterialDesignThemes.Wpf;

var icon = new PackIcon { Kind = PackIconKind.DocumentCopy };

But I don't know how to convert it to a stream that's suitable for consumption by the Cursor object.

1 answers

1

A PackIcon is a Control that wraps a Path element and it cannot be directly used as a cursor.

What you could do is to try to create a cursor from the PackIcon element using @Ray Burns's ConvertToCursor method from here.

The other option would be to simply take a screen shot of the icon, save it as an image and use some tool to create a cursor from it. There are plenty of "convert png to cursor" and similar tools available online.

mm8