The Drop Files Components pack includes three components that support drag and drop from Windows Explorer along with subsidiary components that can filter dropped files.
The components are:
-
TPJDropFiles
-
A container control that catches files dragged and dropped onto its client area or the client area of child controls.
-
TPFormDropFiles
-
A non-visual component that catches files dragged and dropped from Explorer onto the form that contains the component.
-
TPJCtrlDropFiles
-
A non-visual component that catches files dragged and dropped from Explorer onto an associated control.
-
TPJExtFileFilter
-
A file filter component that filters files according to their extension.
-
TPJWildCardFileFilter
-
A file filter component that filters files according to a DOS-style wild card.
-
TPJFileFilter
-
An abstract base class for file filter components. Developers should derive any new filter components from this class.
Some design time assistance is provided by component and property editors.
Features
Key features of TPJDropFiles, TPJCtrlDropFiles and TPJFormDropFiles are:
-
An OnDropFiles event is triggered when files are dropped.
-
The names of dropped files are made available through the Files array property. For convenience, the name of the first such file is also stored in the FileName property.
-
The list of dropped files can be configured to include only files, only folders or all files and folders.
-
Dropped folders can be recursed so that all the files and sub-folders in each of the dropped folders are listed.
-
Filters can be applied to the dropped files and folders. This is done either by linking to a filter component (see above) or by handling the OnFileFilter event and deciding which files and folders to pass through the filter in the event handler.
-
The mouse co-ordinates where the files were dropped can be retrieved, as can a reference to any control under the mouse cursor at the time.
-
The OnBeforeDrop event can be used to perform pre-processing before the files are actually dropped.
-
The IsFolder array property tells if the equivalent item from Files array is a file or a folder.
-
The owning window can be brought to the front when files are dropped.
-
Some design time assistance is provided by component and property editors.
Demo code
The source code for two demo programs is included:
-
The main demo program exercises all three components and the filter components.
-
A program that demonstrates how to use TPJCtrlDropFiles with frames.
How the components work
TPJDropFiles notifies Windows that it accepts file drop operations by using the DragAcceptFiles API call. It then responds to WM_DROPFILES messages sent by Windows whenever files are dropped on its window.
TPJFormDropFiles and TPJCtrlDropFiles act in a similar way to TPJDropFiles except that they subclass an associated window and respond to WM_DROPFILES messages on the window's behalf. TPJFormDropFiles subclasses its owner form while TPJCtrlDropFiles subclasses the window of an associated TWinControl. The last two components derive from a common abstract base class.
For further information about how to handle files dropped on an application from Explorer, please see my article "How to catch files dragged and dropped on an application from Explorer".