Drag-and-Drop Images

Last week I posted a request for help with drag-and-drop images (you
know, like the file name and icon outline that the Explorer shows). My
'naive implementation' couldn't get the pictures in another instance of
the same form, and it never changed the image but kept using the first
image the app generated. I got no feedback, but did solve the problem.
Here's a quick sketch, in case anyone's interested.

The DragOver event is sent by the form that's being dragged over, not
necessarily the form that initiated the dragging. So, if you reference
Self and expect it to contain the image list you plopped on the form, it
won't work: You have to use Source and Sender. (Fwiw, I was dragging and
dropping from/to a TPaintBox: when dragging over the initial form,
Source and Sender were the form; when dragging over another copy, Source
and Sender were the TPaintBox.)

While you have to use DragLock and ShowDragImage to start showing an
image, calling DragUnlock is not enough to reset the drag state: Windows
creates an internal image list when you call DragLock, and you have to
call EndDrag to clear that. (Sadly, EndDrag is not in the DragLock doc's
"see also".) EndDrag calls DragUnlock.

Basically, add your image in the OnStartDrag event; delete it in the
OnEndDrag event. In OnDragOver, pay attention to the State parameter:
on dsDragEnter, do Image.SetDragImage, Image.DragLock,
Image.ShowDragImage; on dsDragMove do Image.DragMove; on dsDragLeave, do
Image.HideDragImage and Image.EndDrag.


http://www.midnightbeach.com/jon   Personal Pages
http://www.midnightbeach.com/jon/pubs Programming Publications
http://www.midnightbeach.com/hs             Home School Resource List