Implementing a DropTarget in ATL

Environment: ATL

Implementing a DropTarget in ATL

Making a control a drop-target in ATL is not something you do very easily. The only help I found in MSDN was an example that didn't work (surprise) until I changed the datatypes of two parameters, as though they did it on purpose. That frustration out of the way, I decided to write a class that will make implementing a drop-target easy as pie. Here it is.

This class takes care of the drag-target functionality in ATL for any CWindow derivable control(= anything with an hWnd). Functions/macros you need to use are the following:

USING_CFDRAGDROP—This is a macro that needs to be included globally.

Because CFDragDropWindow has some static functions and members that need to be defined ONCE and ONCE only, include this macro in such a way that the compiler will only see it once, in the container-class that uses this class define a static function of the DRAGDROPNOTIF prototype, such as:

static HRESULT ReadFiles(FileVector vecStrings,
                         HWND hwnd,
                         void * P){ ... }

in which the first argument will be a vector with the names of the dropped files and the second argument the HWND of the control that the drop occurred on and the third argument the this pointer of your container-class. Next, call CFDragDropWindow's SetCallBack with this function as its first parameter and the 'this' pointer of your container-class as its second argument.

Then, call Attach with the hWnd belonging to the control you want to superclass. Now, when one or more files are dropped on the control, the DRAGDROPNOTIF function in your container-class will get called with as its arguments the dropped filenames and the 'this' pointer of your class (which you can easily cast into your class' datatype so you can use non-static methods/properties).

Example: Using CFDragDrop

class CMainDlg: public CDialogImpl<CMainDlg>
    // the common ATL Dialog, for example

  CFDragDropWindow m_CFDragDropWindow;

  HWND hWndTemp = GetDlgItem(IDC_EDIT_SOURCE);
  return 0;
  static HRESULT ReadFiles(FileVector vecStrings,HWND hwnd,
                           void * P)
    CMainDlg * pThis = (CMainDlg*)P;    // use this to call
                                        // non-static methods
    CComBSTR strTemp = vecStrings[0];
    return S_OK;

The CFDragDropWindow Class

#include <shellapi.h>
#pragma comment(lib,"shell32.lib")
#include <vector>
typedef std::vector<CComBSTR> FileVector;
#include <map>

December 2002
Copyright © 2002 Frans Nagel */

struct ProcData
  void * pContainer;
  WNDPROC OldProc;
typedef std::map<HWND,ProcData,std::less<HWND> > ProcMap;

typedef HRESULT (*DRAGDROPNOTIF)(FileVector,
                                 HWND hwndDropTarget,
                                 void * pContainer);
class CFDragDropWindow  : public CWindow

  CFDragDropWindow(){m_DragDropNotif = 0;}
  static BOOL CALLBACK EditDrop(HWND, unsigned, WPARAM, LPARAM);
  void Attach(HWND hwnd)
    ATLASSERT(m_DragDropNotif != 0);    // call SetCallBack first
         m_pDragDropProc = (WNDPROC)MakeProcInstance
                           (EditDrop, hInst);
         if (m_pDragDropProc)
         m_pOldProc = (WNDPROC)::SetWindowLong(m_hWnd,
    ProcData pd;
    pd.pContainer = m_pContainer ;
    pd.OldProc = m_pOldProc;
  void SetCallBack(DRAGDROPNOTIF ntf,void * pContainer)
    //the function loaded up here needs to be
    //implemented by the container class
    m_DragDropNotif = ntf;
    m_pContainer = pContainer;
    if (m_pDragDropProc)
  static void * m_pContainer;
  static DRAGDROPNOTIF m_DragDropNotif;
  static WNDPROC m_pDragDropProc, m_pOldProc;
  static FileVector m_vecStrings;
  static ProcMap m_ProcMap;
DRAGDROPNOTIF CFDragDropWindow::m_DragDropNotif;\
WNDPROC CFDragDropWindow::m_pDragDropProc;\
WNDPROC CFDragDropWindow::m_pOldProc;\
ProcMap CFDragDropWindow::m_ProcMap;\
void * CFDragDropWindow::m_pContainer;\
FileVector CFDragDropWindow::m_vecStrings;\
BOOL CALLBACK CFDragDropWindow::EditDrop(HWND,
BOOL CALLBACK CFDragDropWindow::EditDrop(HWND hWnd,
                                  unsigned message,
                                  WPARAM wParam,
                                  LPARAM lParam)\
     int wFilesDropped,i = 0;\
  char szTemp[MAX_PATH];\
  ProcMap::iterator iter;\
    WNDPROC wndProc;\
    switch (message)\
      case WM_DROPFILES:\
      wFilesDropped = DragQueryFile((HDROP)wParam, i++,szTemp,
        iter = m_ProcMap.find(hWnd);\
    iter = m_ProcMap.find(hWnd);\
      wndProc = iter->second.OldProc;\
        return CallWindowProc((WNDPROC)wndProc, hWnd, message,\
              wParam, lParam);\
     return TRUE;\



Download demo project - 35 Kb


  • VS.Net 2003 doesn't like stl:map

    Posted by kenkyee on 05/03/2005 01:42pm

    Won't load your ATL object and blows up during regsvr32. Reimplement ProcMap using std::vector and iterate through it looking for the hwnd instead (add an hwnd variable to ProcData and tuck it there).

  • some mods to get it to compile in VS.Net

    Posted by kenkyee on 05/02/2005 06:30pm

    In FDragDropWindow.h, I had to comment out this line: /*BOOL CALLBACK CFDragDropWindow::EditDrop(HWND, unsigned, WPARAM, LPARAM);*/\ Also had to modify char szTemp[MAX_PATH];\ to WCHAR szTemp[MAX_PATH];\ But now, I get a cryptic regsvr32 error when I build my project after adding a CFDragDropWindow member variable: error PRJ0019: A tool returned an error code from "Performing registration"

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds