DropTarget - A library to support "Drop target" functionality


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Environment: MSVC++6.0 SP3, Win98/2000/NT4(SP5)

Code Description


DropTarget adds a "Drop target" (Drag&Drop client) functionality to your C++ code


[Any files you need to import in your project are shown in Red, any code to be added is shown in Blue]

Add the class definition files
- DropFileArray.cpp / .h
- MyDropTarget.cpp / .h
to your project.

#include "MyDropTarget.h" to your main *.cpp file
(in case of this demo "DropTargetDlg.cpp".

Define a message that will be recieved by your application if files are dropped on it:

// message constant
// message handler definition
LRESULT OnDropAction(WPARAM wParam = 0, LPARAM lParam = 0);

BEGIN_MESSAGE_MAP(CDropTargetDlg, CDialog)
  // [...]

Add the following to your dialog's "OnInitDialog()"-member function

OleInitialize(NULL); BOOL br =

to register your application as a drop target.

Implement a message handler for drop action:

LRESULT CDropTargetDlg::OnDropAction(WPARAM wParam, LPARAM lParam)
// [...]

Define a member variable for the new class instance

CMyDropTarget m_DropTarget;

Member functions:

Besides the existing inherited functions, MyDropTarget.cpp / .h overrides / implements the following member functions:

 // retrieve number of files dropped onto target. 0 if none
 int GetNumDroppedFiles(); 
 // get first (ASCII) name of dropped file
 char* GetFirstDroppedFileName(); 
 // get any (position iNum) name of dropped file
 char* GetDroppedFileName(int iNum=0); 
 // get wide char name of dropped file
 wchar_t* GetDroppedFileNameW(int iNum=0); 
 // get first wide char name of dropped file
 wchar_t* GetFirstDroppedFileNameW(); 
 // handle file dropping
 BOOL OnDrop( CWnd* pWnd, COleDataObject* pDataObject, 
                    DROPEFFECT dropEffect, CPoint point );

OnDrop() retrieves a struct containing a (wide char) list of dropped files via the COleDataObject-pointer. The list is then separated into single file names which are filled into an array derived from CArray in order no to limit the possible number of files that can be dropped onto the application.

Normal character set (Win3.X, 9X) and wide character sets (WinNT4 & W2K) are transparently supported via in-line conversion functions.


Download the attached DropTarget demonstration workspace

Final word:

I hope, you find the supplied code useful.
Please feel free to send comments to VolkerBartheld@reetcom.de


DropTargetDemoExe.zip is a compiled executable of the demo application.
DropTargetDemo.zip is a MSVC++ V6 workspace of the demo application.
DropTargetSrc.zip are the complete sources to be added to your project for compilation with MSVC++ V6.0 (SP3) including all necessary libraries.

Download demo project executable
DropTargetDemoExe.zip - 6.065 kBytes

Download demo project workspace
DropTargetDemo.zip - 14.618 kBytes

Download demo project source
DropTargetSrc.zip - 3.800 kBytes

This article was originally published on May 29th, 2001

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date