Click here to Skip to main content
12,507,674 members (52,419 online)
Click here to Skip to main content
Add your own
alternative version


84 bookmarked

Implementing Reusable Drag & Drop Classes

, 4 Jun 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
This article provides a set of reusable drag and drop classes
<!-- Download Links --> <!-- Main HTML starts here -->


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.


<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (


etc) -->

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

You may also be interested in...


Comments and Discussions

Generaldrag Outlook attach Pin
devcdd22-Dec-06 4:12
memberdevcdd22-Dec-06 4:12 
GeneralRe: drag Outlook attach Pin
devcdd24-Dec-06 2:54
memberdevcdd24-Dec-06 2:54 
QuestionHow to cut file Pin
nhoc_conxauxi18-Aug-06 2:44
membernhoc_conxauxi18-Aug-06 2:44 
QuestionHow to cut file Pin
nhoc_conxauxi18-Aug-06 2:36
membernhoc_conxauxi18-Aug-06 2:36 
GeneralCut file Pin
nhoc_conxauxi18-Aug-06 2:25
membernhoc_conxauxi18-Aug-06 2:25 
QuestionIssues with the drag images. Pin
optikflux2-Nov-05 14:12
memberoptikflux2-Nov-05 14:12 
AnswerRe: Issues with the drag images. Pin
mrmcafee16-Mar-06 6:54
membermrmcafee16-Mar-06 6:54 
GeneralCompile bug in ATL Visual C++ .NET Pin
thewizardcode15-Oct-05 9:10
memberthewizardcode15-Oct-05 9:10 

I download this example. I drop in the DragDropImpl.h and DragDropImpl.cpp. I get a compile errors.

DragDropImpl.cpp(606) : fatal error C1010: unexpected end of file while looking for precompiled header directive

Can any one point out what need to be change to get it compile.

QuestionHow to use DragDropImpl.h and DragDropImpl.cpp in MFC project Pin
chinkuanyeh3-Oct-04 6:33
memberchinkuanyeh3-Oct-04 6:33 
GeneralDrag images don't show on W2K Server part II Pin
JD_VIN28-Aug-04 10:50
memberJD_VIN28-Aug-04 10:50 
GeneralDrag images don't show on Win2000 Pin
^Mo^21-May-04 10:00
member^Mo^21-May-04 10:00 
GeneralRe: Drag images don't show on Win2000 Pin
Leon Finker21-May-04 13:26
memberLeon Finker21-May-04 13:26 
GeneralRe: Drag images don't show on Win2000 Pin
^Mo^21-May-04 15:29
member^Mo^21-May-04 15:29 
GeneralRe: Drag images don't show on Win2000 Pin
Leon Finker24-May-04 8:44
memberLeon Finker24-May-04 8:44 
GeneralRe: Drag images don't show on Win2000 Pin
^Mo^24-May-04 11:43
member^Mo^24-May-04 11:43 
Generalit works on xp pro and ONE question Pin
baboguru28-Jun-04 16:30
memberbaboguru28-Jun-04 16:30 
GeneralRe: it works on xp pro and ONE question Pin
Leon Finker29-Jun-04 14:24
memberLeon Finker29-Jun-04 14:24 
GeneralRe: it works on xp pro and ONE question Pin
baboguru30-Jun-04 15:14
memberbaboguru30-Jun-04 15:14 
GeneralRe: it works on xp pro and ONE question Pin
Leon Finker30-Jun-04 15:43
memberLeon Finker30-Jun-04 15:43 
GeneralAccess violation with MFC Pin
^Mo^13-Apr-04 7:55
member^Mo^13-Apr-04 7:55 
GeneralRe: Access violation with MFC Pin
Leon Finker13-Apr-04 15:28
memberLeon Finker13-Apr-04 15:28 
GeneralAnother thing Pin
^Mo^21-Apr-04 10:24
member^Mo^21-Apr-04 10:24 
GeneralRe: Another thing Pin
Leon Finker21-Apr-04 14:27
memberLeon Finker21-Apr-04 14:27 
Generalmemory leaks! Pin
FPF5-Sep-03 2:16
memberFPF5-Sep-03 2:16 
GeneralRe: memory leaks! Pin
Leon Finker5-Sep-03 5:53
memberLeon Finker5-Sep-03 5:53 
GeneralRe: memory leaks! Pin
FPF7-Sep-03 19:57
memberFPF7-Sep-03 19:57 
GeneralRe: memory leaks! Pin
Leon Finker8-Sep-03 3:02
memberLeon Finker8-Sep-03 3:02 
QuestionHow do I store raw data from my application to a disk file through drag and drop ... Pin
Anonymous10-May-03 14:00
sussAnonymous10-May-03 14:00 
GeneralCompiler Error Pin
White Lie20-Feb-03 16:24
memberWhite Lie20-Feb-03 16:24 
GeneralRe: Compiler Error Pin
Jim Crafton20-Feb-03 17:02
memberJim Crafton20-Feb-03 17:02 
GeneralWon't compile... Pin
NutritiousTreat11-Jan-03 13:22
memberNutritiousTreat11-Jan-03 13:22 
GeneralRe: Won't compile... Pin
Leon Finker12-Jan-03 7:27
memberLeon Finker12-Jan-03 7:27 
GeneralInvalid Drag & Drop Image in ListView Pin
Park Dong-Hyun30-Oct-02 20:43
memberPark Dong-Hyun30-Oct-02 20:43 
GeneralNo drag image on XP Pin
andre moreira14-Sep-02 9:17
sussandre moreira14-Sep-02 9:17 
QuestionHow to make DROP of big files non-blocking ? Pin
Benjamin.Mayrargue14-May-02 5:36
memberBenjamin.Mayrargue14-May-02 5:36 
AnswerRe: How to make DROP of big files non-blocking ? Pin
Leon Finker14-May-02 6:46
memberLeon Finker14-May-02 6:46 
GeneralRe: How to make DROP of big files non-blocking ? Pin
Benjamin.Mayrargue14-May-02 10:57
memberBenjamin.Mayrargue14-May-02 10:57 
GeneralRe: How to make DROP of big files non-blocking ? Pin
Leon Finker14-May-02 11:27
memberLeon Finker14-May-02 11:27 
GeneralRe: How to make DROP of big files non-blocking ? Pin
Benjamin.Mayrargue14-May-02 21:39
memberBenjamin.Mayrargue14-May-02 21:39 
GeneralDrag from Explorer to My app Pin
Preetham25-Mar-02 18:38
memberPreetham25-Mar-02 18:38 
GeneralRe: Drag from Explorer to My app Pin
Anonymous6-May-02 7:19
memberAnonymous6-May-02 7:19 
GeneralRe: Drag from Explorer to My app Pin
Jim Crafton31-May-02 3:26
memberJim Crafton31-May-02 3:26 
GeneralRe: Drag from Explorer to My app Pin
Anonymous24-Feb-03 0:38
sussAnonymous24-Feb-03 0:38 
GeneralCant compile the project Pin
peterboheme18-Mar-02 22:09
memberpeterboheme18-Mar-02 22:09 
GeneralRe: Cant compile the project Pin
Leon Finker19-Mar-02 5:54
memberLeon Finker19-Mar-02 5:54 
Questionhow to save a CF_DIB type data to my file? Pin
hury16-Dec-01 22:55
memberhury16-Dec-01 22:55 
Generalit's good :) but one things:(( Pin
Anonymous27-Jun-01 23:55
memberAnonymous27-Jun-01 23:55 
GeneralRe: it's good :) but one things:(( Pin
Leon Finker4-Jul-01 4:57
memberLeon Finker4-Jul-01 4:57 
General2 things Pin
Leon Finker7-Jun-01 7:33
memberLeon Finker7-Jun-01 7:33 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160927.1 | Last Updated 5 Jun 2001
Article Copyright 2001 by Leon Finker
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid