Can I choose my own image for C # Windows Drag Drop features?

I am writing a small project in which I would like to use the drag and drop function to facilitate some operations for the end user. To make the application more attractive, I would like to display an object that is being dragged. I found some resources with WPF, but I don’t know WPF, so it becomes a little difficult to bite this whole thing for this single task. I would like to know how this can be done using "regular" C # Windows Forms. Until now, all the drag and drop tutorials that I have found just talk about drop effects, which are just a preset of a few icons.

WPF sounds like what I want to know after this project.

+3
source share
2 answers

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

You can 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

Archive.org snapshot link updated

+1
source

The blog link provided by @Jesper provides two or three key nuggets of information, but I think it's worth bringing it to SO for posterity.

Custom cursor setup

In the code below you can use a custom 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));
    }

. , , GiveFeedback DragEnter, , , .

 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);
     }
+7

Source: https://habr.com/ru/post/1793859/


All Articles