Click here to Skip to main content
15,170,331 members
Articles / Desktop Programming / WTL
Posted 4 Jun 2001


83 bookmarked

Implementing Reusable Drag & Drop Classes

Rate me:
Please Sign up or sign in to vote.
4.63/5 (17 votes)
4 Jun 2001CPOL2 min read
This article provides a set of reusable drag and drop classes


I tried to create as generic a Drag and Drop classes as possible. Here is what I came up with. I think it's a good starting point if you're looking to add drag and drop support to your app.

The demo project has sample code for various clipboard formats:

  • CF_DIB



Some screenshots of image drag and drop from static window:

(Fig. 1. Dragging from static window to WordPad)

(Fig. 2. Using Clipboard)

(Fig. 3. Pasting the above clipboard contents into WordPad)


To enable your window as a DropTarget:

  • Derive a class from CIDropTarget.
  • Override OnDrop. Return true or false from this method. If true, base class will free the medium. If false, it won't free the medium.
  • Call ::RegisterDragDrop for your window.
  • Add Supported formats by calling CIDropTarget::AddSuportedFormat.
  • Optionally override other methods such as DragOver and DragLeave.
    I used it for the tree to highlight the current item.


class CTreeDropTarget : public CIDropTarget
      virtual bool OnDrop(FORMATETC* pFmtEtc, STGMEDIUM& medium, DWORD *pdwEffect)
            if(pFmtEtc->cfFormat == CF_TEXT && medium.tymed == TYMED_HGLOBAL)
              // Handle it
          return true;
     // etc...

In your Window derived class create a member of CTreeDropTarget. Then initialize it like this:

// ...
m_pDropTarget = new CTreeDropTarget(m_hWnd);
// create the supported formats:
FORMATETC ftetc={0}; 
ftetc.cfFormat = CF_TEXT; 
ftetc.dwAspect = DVASPECT_CONTENT; 
ftetc.lindex = -1; 
ftetc.tymed = TYMED_HGLOBAL; 
// ...

That's all for drop target.

To enable your window as the Drag and Drop source:

  • Catch the Windows message that initiates the drag and drop such as TVN_BEGINDRAG.
  • In the message function handler create new CIDataObject and CIDropSource.
  • Create the clipboard formats and medium for those formats.
  • Call SetData to add the clipboard formats and medium to DataObject. Second parameter to SetData indicates if DataObject should take the ownership of medium or not. If set to TRUE, then DataObject takes the ownership of your medium, you don't need to free it. Otherwise it will make a copy of your medium without releasing the one you gave it.


 LRESULT OnBegindrag(...)
   CIDropSource* pdsrc = new CIDropSource;
   CIDataObject* pdobj = new CIDataObject(pdsrc);
   // Init the supported format
   FORMATETC fmtetc = {0}; 
   fmtetc.cfFormat = CF_TEXT; 
   fmtetc.dwAspect = DVASPECT_CONTENT; 
   fmtetc.lindex = -1; 
   fmtetc.tymed = TYMED_HGLOBAL;
  // Init the medium used
  STGMEDIUM medium = {0};
  medium.tymed = TYMED_HGLOBAL;
  // medium.hGlobal = init to something
  // Add it to DataObject
  pdobj->SetData(&fmtetc,&medium,TRUE); // Release the medium for me
  // add more formats and medium if needed
  // Initiate the Drag & Drop
  ::DoDragDrop(pdobj, pdsrc, DROPEFFECT_COPY, &dwEffect);

To use the shell's drag image manager (comes with Windows 2000):

You don't need to add the support for it if you are acting as drop target. It is encapsulated in CIDropTarget class.
If you're acting as data source:

  • Create an instance of CDragSourceHelper before calling ::DoDragDrop.
  • Call CDragSourceHelper::InitializeFromWindow or CDragSourceHelper::InitializeFromBitmap.

Adding the Copy/Paste through clipboard is not much work either.


LRESULT OnContextMenu(...)
  // ...
   CIDataObject* pdobj = new CIDataObject(NULL);  
   // Init FORMATETC and STGMEDIUM just like before
   // Add the format and medium to Dataobject
  // Add data to clipboard
  OleFlushClipboard(); //render the data on clipboard, so it's available even if we close the app 
 // ...



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leon Finker
United States United States
No Biography provided

Comments and Discussions

Generaldrag Outlook attach Pin
devcdd22-Dec-06 5:12
Memberdevcdd22-Dec-06 5:12 
GeneralRe: drag Outlook attach Pin
devcdd24-Dec-06 3:54
Memberdevcdd24-Dec-06 3:54 
QuestionHow to cut file Pin
nhoc_conxauxi18-Aug-06 3:44
Membernhoc_conxauxi18-Aug-06 3:44 
QuestionHow to cut file Pin
nhoc_conxauxi18-Aug-06 3:36
Membernhoc_conxauxi18-Aug-06 3:36 
GeneralCut file Pin
nhoc_conxauxi18-Aug-06 3:25
Membernhoc_conxauxi18-Aug-06 3:25 
QuestionIssues with the drag images. Pin
optikflux2-Nov-05 15:12
Memberoptikflux2-Nov-05 15:12 
AnswerRe: Issues with the drag images. Pin
mrmcafee16-Mar-06 7:54
Membermrmcafee16-Mar-06 7:54 
GeneralCompile bug in ATL Visual C++ .NET Pin
thewizardcode15-Oct-05 10:10
Memberthewizardcode15-Oct-05 10:10 
QuestionHow to use DragDropImpl.h and DragDropImpl.cpp in MFC project Pin
chinkuanyeh3-Oct-04 7:33
Memberchinkuanyeh3-Oct-04 7:33 
GeneralDrag images don't show on W2K Server part II Pin
JD_VIN28-Aug-04 11:50
MemberJD_VIN28-Aug-04 11:50 
GeneralDrag images don't show on Win2000 Pin
Maarten Kools21-May-04 11:00
professionalMaarten Kools21-May-04 11:00 
GeneralRe: Drag images don't show on Win2000 Pin
Leon Finker21-May-04 14:26
MemberLeon Finker21-May-04 14:26 
GeneralRe: Drag images don't show on Win2000 Pin
Maarten Kools21-May-04 16:29
professionalMaarten Kools21-May-04 16:29 
GeneralRe: Drag images don't show on Win2000 Pin
Leon Finker24-May-04 9:44
MemberLeon Finker24-May-04 9:44 
GeneralRe: Drag images don't show on Win2000 Pin
Maarten Kools24-May-04 12:43
professionalMaarten Kools24-May-04 12:43 
Generalit works on xp pro and ONE question Pin
baboguru28-Jun-04 17:30
Memberbaboguru28-Jun-04 17:30 
GeneralRe: it works on xp pro and ONE question Pin
Leon Finker29-Jun-04 15:24
MemberLeon Finker29-Jun-04 15:24 
GeneralRe: it works on xp pro and ONE question Pin
baboguru30-Jun-04 16:14
Memberbaboguru30-Jun-04 16:14 
GeneralRe: it works on xp pro and ONE question Pin
Leon Finker30-Jun-04 16:43
MemberLeon Finker30-Jun-04 16:43 
GeneralAccess violation with MFC Pin
Maarten Kools13-Apr-04 8:55
professionalMaarten Kools13-Apr-04 8:55 
Hi, first of all: Great class!

Second: I had access violations when using it in an MFC project. The problem is the CEnumFormatEtc class. You've created on of your own, but apparently MFC also implements a class with the same name. So every time AddRef was called in CIDataObject::EnumFormatEtc() it somehow ended up in the destructor of MFC's version of CEnumFormatEtc (don't ask me how). Anyway, this kept raising an access violation all the time.

As a solution, I put all your classes in a seperate namespace, and that seemed to solve the problem.
GeneralRe: Access violation with MFC Pin
Leon Finker13-Apr-04 16:28
MemberLeon Finker13-Apr-04 16:28 
GeneralAnother thing Pin
Maarten Kools21-Apr-04 11:24
professionalMaarten Kools21-Apr-04 11:24 
GeneralRe: Another thing Pin
Leon Finker21-Apr-04 15:27
MemberLeon Finker21-Apr-04 15:27 
Generalmemory leaks! Pin
FPF5-Sep-03 3:16
MemberFPF5-Sep-03 3:16 
GeneralRe: memory leaks! Pin
Leon Finker5-Sep-03 6:53
MemberLeon Finker5-Sep-03 6:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.