-2

I use Delphi XE5 and have to realize the possibility for the user to drag different files (docx, xlsx, pictures (jpeg, gif, ...), E-Mails, ... into a component. The component should show the filename or the header of the E-Mail. Than i want to save this file into a Blob-field in the SQL-Server Database.

Can anyone tell me whats the best possibility if possible with a short example. Can i realize this with the standard delphi components or is it necessary to install special components.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Chris
  • 57
  • 8

1 Answers1

2

Your component needs to be derived from TWinControl (TCustomControl, etc) so it has its own HWND to drag things onto. Then:

  1. if you just want to capture dragged filenames, your component can process the WM_DROPFILES window message. This is a legacy message, but it still works. However, it only works when dragging physical files from the file system onto your window. For more advanced scenarios (dragging virtual data, dragging data from other apps, etc), you need to...

  2. a. implement the IDropTarget interface instead. Write an object that implements IDropTarget, or use a third-party implementation such as from Ander's Melander's Drag&Drop suite.

    b. register that object for your component's HWND using RegisterDragDrop() after it has been created (the best place to do that in in your component's overridden CreateWnd() method), and unregister the object using RevokeDragDrop() when the HWND is being destroyed (such as in the overridden DestroyWnd() method).

    c. when the user drags something onto your component's window, your IDropTarget.DragEnter() implementation will be called. Examine the provided IDataObject to see if it holds a data format + transport scheme that you support (there can be multiple combinations at one time). Each piece of data will be identified by a unique clipboard format identifier, and a TYMED value indicating how the data is transferred. Some clipboard formats are pre-defined (see Shell Clipboard Formats), and some can be registered dynamically at runtime (see RegisterClipboardformat()). If you find a combination that you support, return DROPEFFECT_COPY to accept the drag, otherwise return DROPEFFECT_NONE to reject the drag.

    d. if the user drops something onto your component's window (if accepted by DragEnter()), your IDropTarget.Drop() implementation will be called. Extract the data from the provided IDataObject and use it as needed. When dropping physical files, you will receive their paths+names and/or their ITEMIDLIST identifiers, then you can open and read the files as needed. When dropping virtual data/files, you will get the actual data instead (which is usually transferred as a block of memory in an HGLOBAL that you can access using GlobalLock(), or as an IStream interface, but there are other possibilities available).

See MSDN for more details:

Transferring Shell Objects with Drag-and-Drop and the Clipboard

Shell Data Object

Handling Shell Data Transfer Scenarios

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770