Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I thought I should come back and answer this myself, since I did get it working eventually.</p> <p>I created a CursorUtil class with these functions:</p> <pre><code>public struct IconInfo { public bool fIcon; public int xHotspot; public int yHotspot; public IntPtr hbmMask; public IntPtr hbmColor; } public class CursorUtil { [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); [DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr handle); [DllImport("user32.dll", CharSet = CharSet.Auto)] extern static bool DestroyIcon(IntPtr handle); // Based on the article and comments here: // http://www.switchonthecode.com/tutorials/csharp-tutorial-how-to-use-custom-cursors // Note that the returned Cursor must be disposed of after use, or you'll leak memory! public static Cursor CreateCursor(Bitmap bm, int xHotspot, int yHotspot) { IntPtr cursorPtr; IntPtr ptr = bm.GetHicon(); IconInfo tmp = new IconInfo(); GetIconInfo(ptr, ref tmp); tmp.xHotspot = xHotspot; tmp.yHotspot = yHotspot; tmp.fIcon = false; cursorPtr = CreateIconIndirect(ref tmp); if (tmp.hbmColor != IntPtr.Zero) DeleteObject(tmp.hbmColor); if (tmp.hbmMask != IntPtr.Zero) DeleteObject(tmp.hbmMask); if (ptr != IntPtr.Zero) DestroyIcon(ptr); return new Cursor(cursorPtr); } public static Bitmap AsBitmap(Control c) { Bitmap bm = new Bitmap(c.Width, c.Height); c.DrawToBitmap(bm, new Rectangle(0, 0, c.Width, c.Height)); return bm; } </code></pre> <p>Then I wrote a Drag class (also not object-oriented, alas, but I figured you can only drag one thing at a time in a desktop app). Here is a bit of that code:</p> <pre><code> public static void StartDragging(Control c) { Dragged = c; DisposeOldCursors(); Bitmap bm = CursorUtil.AsBitmap(c); DragCursorMove = CursorUtil.CreateCursor((Bitmap)bm.Clone(), DragStart.X, DragStart.Y); DragCursorLink = CursorUtil.CreateCursor((Bitmap)bm.Clone(), DragStart.X, DragStart.Y); DragCursorCopy = CursorUtil.CreateCursor(CursorUtil.AddCopySymbol(bm), DragStart.X, DragStart.Y); DragCursorNo = CursorUtil.CreateCursor(CursorUtil.AddNoSymbol(bm), DragStart.X, DragStart.Y); //Debug.WriteLine("Starting drag"); } // This gets called once when we move over a new control, // or continuously if that control supports dropping. public static void UpdateCursor(object sender, GiveFeedbackEventArgs fea) { //Debug.WriteLine(MainForm.MousePosition); fea.UseDefaultCursors = false; //Debug.WriteLine("effect = " + fea.Effect); if (fea.Effect == DragDropEffects.Move) { Cursor.Current = DragCursorMove; } else if (fea.Effect == DragDropEffects.Copy) { Cursor.Current = DragCursorCopy; } else if (fea.Effect == DragDropEffects.None) { Cursor.Current = DragCursorNo; } else if (fea.Effect == DragDropEffects.Link) { Cursor.Current = DragCursorLink; } else { Cursor.Current = DragCursorMove; } } </code></pre> <p>You can use these methods when you set up your controls, for example in the constructor:</p> <pre><code>GiveFeedback += new GiveFeedbackEventHandler(Drag.UpdateCursor); </code></pre> <p>and in this method:</p> <pre><code> protected override void OnMouseMove(MouseEventArgs mea) { if (Drag.IsDragging(mea)) { Drag.StartDragging(this); DragDropEffects dde = DoDragDrop(Plan, DragDropEffects.Move | DragDropEffects.Copy); Drag.StopDragging(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload