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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

2 Answers2

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
  • 163,881
  • 10
  • 57
  • 88
  • Thanks for the suggestion. I tried this but couldn't get it to work; it appears to create a valid cursor, but nothing is displayed on the screen. Ray's code appears to have a bug in it; it requires a valid `stride` calculation, which I got from [here](https://stackoverflow.com/questions/3711947/stride-woes-from-a-transformedbitmap-object). Still no joy, though. For now, I'm going to convert my pack icons to cursors by hand and include them as resource files. – Robert Harvey Feb 12 '19 at 17:52
  • @mm8 Can this be used in Window Icon in Xaml., it is giving error. – user1066231 Jun 15 '20 at 21:40
0

I was able to use a cursor-creating method similar to what's described here (which doesn't have the stride issues in the link in mm8's answer).

In addition, the code to use a PackIconControl looks like the following.

    PackIconControl pic = new PackIconControl() { Kind = PackIconMaterialKind.CursorDefaultOutline, Width = 16, Height = 16};
    pic.Style = Application.Current.FindResource(typeof(PackIconControl)) as Style;
    pic.Foreground = Brushes.White;
    m_MyCursor = CursorUtil.ConvertToCursor(pic, new Point(4,1));

Some comments:

I had to explicitly specify a style since I hadn't added the control to the visual tree. My app's resource dictionary has the mahapps styles in it.

CursorUtil.ConvertToCursor is the method referred to at the above link.

The Point(4,1) puts the hot spot of the cursor at approximately the tip of the PackIconMaterialKind.CursorDefaultOutline arrow. Other icons would have different hot spots that are appropriate for their particular shapes.

dtm
  • 794
  • 5
  • 15