I’m building a desktop app in C# with Windows Forms. I have a custom Control, and I’d like to be able to drag and drop it within my application (not outside). Right now I’m implementing that with the usual DoDragDrop/OnDragOver/OnDragDrop methods. Is there any way to continuously paint the control as it gets dragged around–sort of what you see with JQuery’s drag-and-drop? I want the actual control to stay in place, but I want to paint a copy of its appearance as the user drags it. Ideally the copy would even be semi-transparent, but that’s more a “nice to have.”
The only way I can think to do this is to put the paint code in the main form’s OnPaint method, but that seems like an inelegant solution. Any other ideas? Are things any easier if the Control paints itself as just a Bitmap?
I thought I should come back and answer this myself, since I did get it working eventually.
I created a CursorUtil class with these functions:
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:
You can use these methods when you set up your controls, for example in the constructor:
and in this method: