4

I am writing a small project where I would like to make use of drag and drop functionalty to ease some of the operations for the end user. To make the application a little more appealing, I would like to display the object being dragged. I have found some resources with WPF, but I don't know any WPF, so it becomes a bit tough to bite down on that whole subject for this single task. I would like to know how this can be done with "regular" C# Windows Forms. So far, all drag drop tutorials I've found just talk about the drop effects which is just a preset of a few icons.

WPF sounds like something I want to learn after this project.

Statement
  • 3,888
  • 3
  • 36
  • 45

2 Answers2

7

The blog link provided by @Jesper gives the two or three key nuggets of info, but I think it is worth bringing it into S.O. for posterity.

Set up the custom cursor

The code below allows you to use an arbitrary image for your cursor

 public struct IconInfo
  {
    public bool fIcon;
    public int xHotspot;
    public int yHotspot;
    public IntPtr hbmMask;
    public IntPtr hbmColor;
  }

    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

    public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
    {
      IconInfo tmp = new IconInfo();
      GetIconInfo(bmp.GetHicon(), ref tmp);
      tmp.xHotspot = xHotSpot;
      tmp.yHotspot = yHotSpot;
      tmp.fIcon = false;
      return new Cursor(CreateIconIndirect(ref tmp));
    }

Set up the drag and drop event handling

This is well covered in other tutorials and answers. The specific events we are concerned about here are GiveFeedback and DragEnter, on any control where you want the custom cursor to apply.

 private void DragSource_GiveFeedback(object sender, GiveFeedbackEventArgs e)
    {
      e.UseDefaultCursors = 0;
    }

   private void DragDest_DragEnter(object sender, DragEventArgs e)
    {

      Cursor.Current = CreateCursor(bitmap, 0, 0);
     }
vpipkt
  • 1,710
  • 14
  • 17
  • 2
    This code has a nasty handle leak that will crash your program after a while. Note the use of DestroyIcon() in the MSDN example code for the Bitmap.GetHicon() method. Required, but must be done *after* the cursor is no longer used. Getting the Cursor class to automatically do this for you is possible with a [Reflection hack](http://stackoverflow.com/a/1551567/17034). – Hans Passant Aug 02 '16 at 08:13
  • Excellent observation. The code above does in fact bomb "after a while", which for me was after only 6 to 10 invocations. – Jazimov Nov 19 '18 at 06:38
1

You need to hide the default cursor and create your own window containing your custom image and then move that window with the position of the mouse.

You might also take a look at http://web.archive.org/web/20130127145542/http://www.switchonthecode.com/tutorials/winforms-using-custom-cursors-with-drag-drop

UPDATE 2015-11-26

Updated the link to point to archive.org's last snapshot

Jesper Larsen-Ledet
  • 6,625
  • 3
  • 30
  • 42