Dev - CodeGuru


DropTarget

(Letztes Update: 7.2.2008)

DropTarget - A library to support "Drop target" functionality

This article was contributed by Volker Bartheld.

screenshot

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

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:

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

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

History

Date Posted: 04/23/2001