Click here to Skip to main content
12,755,080 members (37,430 online)
Click here to Skip to main content
Add your own
alternative version


53 bookmarked
Posted 9 Nov 2000

A generic IDropTarget COM class for dropped text

, 9 Nov 2000
Rate this:
Please Sign up or sign in to vote.
Simple step by step article explaining how to implement a drop target using OLE.

This article is part of the drag and drop interface samples.

  1. Serializing ASCII Data
  2. Modeless child dialog
  3. Modeless sibling dialog
  4. The drag source
  5. The MFC drop target
  6. The TBTextTarget class

Doing it the OLE way... an interface class to derive from

If you are a MFC purist and only want to have CObject derived classes in your project, than consider the pure MFC drop target.

You will find the sample project in step3generic or you may download only the TBTextTarget class

A generic IDropTarget COM class for dropped text

I promised you "a better way" and I fulfill this promise now - at least half of it because its for the drop target, the IDropSouce and IDataObject parts are missing, but the MFC support for the begin of dragging out of CListCtrl is quite good (perhaps someone else can take this as an exercise and place these wrappers in the comments for this article :)

The OLE doc's say: "If you want your (window) class to be drop-enabled, implement a IDropTarget interface." Nice - what's that? In plain English I'd say: your drop-enabled class needs a couple of functions which do the right things. The bundle of these functions make out "the interface" and because some interfaces are "at the top" of others you have - a kind of class hierarchy!

Due to the fact that we are talking about text dragging the private data format described in the other articles of this series. Implement the drag sources as common interface (CF_TEXT, see "Data is going abroad...") for the drop part and do the following (and compare with the pure MFC stuff!)

  1. Create your CWnd-based class (CYourClass, e.g. your dialog) as usual
  2. include the files TBTextTarget.h and TBTextTarget.cpp in your project
  3. Go to the class definition of CYourClass and derive it from TBTextTarget too: e.g. the line
    class CDropDialog : public CDialog 
    will become
    class CDropDialog : public CDialog, TBTextTarget 
  4. Don't forget to #include "TBTextTarget.h"
  5. At a point where this window is created (for example: OnInitDialog is great), tell Windows that you are drag&drop-enabled:
    BOOL CDropDialog::OnInitDialog()
            ::RegisterDragDrop(GetSafeHwnd(), this);
            // the other stuff follows ....
  6. Add this function to CYourClass (it's pure virtual in TBTextTarget because it depents on YOU what you do with YOUR data):
    void ProcessData(CString Data)

    This function will be called when a text was dragged into your window. Do with it what ever you like (or need). For example:

    CDropDialog::ProcessData(CString Data)
       CString t1(Data), t2;
       int idx = t1.Find('\n');
       while (idx !=-1)
          t2 = t1.Left(idx);
          t1 = t1.Mid(idx+1);
          idx = t1.Find('\n');


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Thomas Blenkers
Web Developer
Germany Germany
PhD Chemist,
programming with MSVC & MFC since 1996

You may also be interested in...

Comments and Discussions

QuestionHow to drag a file to activex control in ie. [modified] Pin
leaf-v19-Aug-06 22:15
memberleaf-v19-Aug-06 22:15 
QuestionHow to drag and drop hyperlinks? Pin
Alien Killer3-Sep-03 15:21
sussAlien Killer3-Sep-03 15:21 
GeneralWhy is it the Drop position is not accurate Pin
Anthony_Yio6-Mar-03 0:40
memberAnthony_Yio6-Mar-03 0:40 
GeneralRe: Why is it the Drop position is not accurate Pin
Anthony_Yio6-Mar-03 23:46
memberAnthony_Yio6-Mar-03 23:46 
Questioncan i drag drop a bitmap using clipboard CF_BITMAP Pin
Shoki23-Mar-02 3:27
memberShoki23-Mar-02 3:27 
GeneralCorrection: InterfaceView.obj : error LNK2001: unresolved external ... Pin
Thomas Blenkers3-Jun-01 12:45
memberThomas Blenkers3-Jun-01 12:45 
QuestionCan not download the source code. Pin
Anonymous15-Nov-00 0:56
memberAnonymous15-Nov-00 0:56 
AnswerRe: Can not download the source code. Pin
Thomas Blenkers3-Jun-01 12:49
memberThomas Blenkers3-Jun-01 12:49 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 10 Nov 2000
Article Copyright 2000 by Thomas Blenkers
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid