Dev - CodeGuru


(Letztes Update: 7.2.2008)

DropTarget - A library to support "Drop target" functionality

This article was contributed by Volker Bartheld.


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

Code Description

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

Implementation: [Any files you need to import in your project are shown in Red, any code to be added is shown in Blue]
(1) Add the class definition files

to your project.


#include "MyDropTarget.h"

to your main *.cpp file (in case of this demo "DropTargetDlg.cpp".

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


// message constant #define WM_DROPACTION WM_APP+1
// message handler definition
LRESULT OnDropAction(WPARAM wParam = 0, LPARAM lParam = 0);


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

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

BOOL br = m_DropTarget.Register(this);

to register your application as a drop target.


Implement a message handler for drop action: [DropTargetDlg.cpp:]

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:

 int GetNumDroppedFiles(); // retrieve number of files dropped onto target. 0 if none
 char* GetFirstDroppedFileName(); // get first (ASCII) name of dropped file
 char* GetDroppedFileName(int iNum=0); // get any (position iNum) name of dropped file
 wchar_t* GetDroppedFileNameW(int iNum=0); // get wide char name of dropped file
 wchar_t* GetFirstDroppedFileNameW(); // get first wide char name of dropped file
 // 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.

Example: Download the attached DropTarget demonstration workspace

Final word: I hope, you find the supplied code useful. Please feel free to send comments to

Downloads is a compiled executable of the demo application. is a MSVC++ V6 workspace of the demo application. 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 - 6.065 kBytes
Download demo project workspace - 14.618 kBytes
Download demo project source - 3.800 kBytes


Date Posted: 04/23/2001