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; LRESULT OnInitDialog(UINT , WPARAM , LPARAM , BOOL& ) { ... HWND hWndTemp = GetDlgItem(IDC_EDIT_SOURCE); m_CFDragDropWindow.SetCallBack(ReadFiles,this); m_CFDragDropWindow.Attach(hWndTemp); return 0; } static HRESULT ReadFiles(FileVector vecStrings,HWND hwnd, void * P) { USES_CONVERSION; CMainDlg * pThis = (CMainDlg*)P; // use this to call // non-static methods CComBSTR strTemp = vecStrings[0]; ::SetWindowText(hwnd,W2A(strTemp)); return S_OK; } };
The CFDragDropWindow Class
#ifndef CF_DRAGDROP_WINDOW_ #define CF_DRAGDROP_WINDOW_ #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 { public: CFDragDropWindow(){m_DragDropNotif = 0;} static BOOL CALLBACK EditDrop(HWND, unsigned, WPARAM, LPARAM); void Attach(HWND hwnd) { ATLASSERT(m_DragDropNotif != 0); // call SetCallBack first CWindow::Attach(hwnd); ::DragAcceptFiles(m_hWnd,TRUE); m_pDragDropProc = (WNDPROC)MakeProcInstance (EditDrop, hInst); if (m_pDragDropProc) m_pOldProc = (WNDPROC)::SetWindowLong(m_hWnd, GWL_WNDPROC, (DWORD)(WNDPROC)m_pDragDropProc); ProcData pd; pd.pContainer = m_pContainer ; pd.OldProc = m_pOldProc; m_ProcMap.insert(ProcMap::value_type(hwnd,pd)); } 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; } ~CFDragDropWindow() { if (m_pDragDropProc) FreeProcInstance(m_pDragDropProc); } protected: static void * m_pContainer; static DRAGDROPNOTIF m_DragDropNotif; static WNDPROC m_pDragDropProc, m_pOldProc; static FileVector m_vecStrings; static ProcMap m_ProcMap; }; #define USING_CFDRAGDROP 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, unsigned, WPARAM, LPARAM); 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: m_vecStrings.clear(); while(true) { wFilesDropped = DragQueryFile((HDROP)wParam, i++,szTemp, 255); if(!wFilesDropped) break; m_vecStrings.push_back(szTemp); } DragFinish((HDROP)wParam); iter = m_ProcMap.find(hWnd); m_DragDropNotif(m_vecStrings,hWnd,iter-> second.pContainer); break; default: iter = m_ProcMap.find(hWnd); wndProc = iter->second.OldProc; return CallWindowProc((WNDPROC)wndProc, hWnd, message, wParam, lParam); } return TRUE; } #endif