DropTarget - A library to support "Drop target" functionality | CodeGuru

DropTarget – A library to support “Drop target” functionality

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 – DropFileArray.cpp / .h – MyDropTarget.cpp / […]

Written By
CodeGuru Staff
CodeGuru Staff
May 29, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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
DropFileArray.cpp / .h
MyDropTarget.cpp / .h
to your project.

(2)
#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:

[DropTargetDlg.h:]
// message constant
#define WM_DROPACTION WM_APP+1
// message handler definition
LRESULT OnDropAction(WPARAM wParam = 0, LPARAM lParam = 0);

[DropTargetDlg.cpp:]
BEGIN_MESSAGE_MAP(CDropTargetDlg, CDialog)
  // [...]
  ON_MESSAGE(WM_DROPACTION, OnDropAction)
END_MESSAGE_MAP()

(4)
Add the following to your dialog’s “OnInitDialog()”-member
function

OleInitialize(NULL); BOOL br =
   m_DropTarget.Register(this);

to register your application as a drop target.

(5)
Implement a message handler for drop action:

[DropTargetDlg.cpp:]
LRESULT CDropTargetDlg::OnDropAction(WPARAM wParam, LPARAM lParam)
{
// [...]
}  

(6)
Define a member variable for the new class instance

[DropTargetDlg.h:]
CMyDropTarget m_DropTarget;

Member functions:

Besides the existing inherited functions, MyDropTarget.cpp / .h
overrides / implements the following member functions:

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

Downloads

DropTargetDemoExe.zip is a compiled executable of the demo application.
DropTargetDemo.zip is a MSVC++ V6 workspace of the demo application.
DropTargetSrc.zip 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
DropTargetDemoExe.zip – 6.065 kBytes

Download demo project workspace
DropTargetDemo.zip – 14.618 kBytes

Download demo project source
DropTargetSrc.zip – 3.800 kBytes

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.