This article is part of the drag and drop interface samples.
- Serializing ASCII Data
- Modeless child dialog
- Modeless sibling dialog
- The drag source
- The MFC drop target
- 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, then 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 it's for the drop target, the IDropSource
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 docs 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!)
- Create your
CWnd
-based class (CYourClass
, e.g. your dialog) as usual. - Include the files TBTextTarget.h and TBTextTarget.cpp in your project.
- 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
- Don't forget to
#include "TBTextTarget.h"
- At a point where this window is created (for example:
OnInitDialog
is great), tell Windows that you are drag&drop-enabled:
BOOL CDropDialog::OnInitDialog()
{
CDialog::OnInitDialog();
::RegisterDragDrop(GetSafeHwnd(), this);
}
- Add this function to
CYourClass
(it's pure virtual in TBTextTarget
because it depends 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 whatever 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);
InsertRow(t2);
idx = t1.Find('\n');
}
}
License
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.