Click here to Skip to main content
15,891,846 members
Articles / Desktop Programming / MFC

How to Implement Drag and Drop Between Your Program and Explorer

Rate me:
Please Sign up or sign in to vote.
4.91/5 (105 votes)
13 Dec 2002 863.6K   11.4K   278  
A step-by-step description of how to drag and drop files between your application and Explorer windows.
// MultiFilerView.h : interface of the CMultiFilerView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_MULTIFILERVIEW_H__D626641B_521D_4EE0_AAFB_96734DF04F15__INCLUDED_)
#define AFX_MULTIFILERVIEW_H__D626641B_521D_4EE0_AAFB_96734DF04F15__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct IDropTargetHelper;    // in case the newest PSDK isn't available.

class CMultiFilerView : public CListView
{
protected: // create from serialization only
	CMultiFilerView();
	DECLARE_DYNCREATE(CMultiFilerView)

// Attributes
public:
	CMultiFilerDoc* GetDocument();

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMultiFilerView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual DROPEFFECT OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
	virtual DROPEFFECT OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
	virtual BOOL OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point);
	virtual void OnDragLeave();
	protected:
	virtual void OnInitialUpdate(); // called first time after construct
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CMultiFilerView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:
    CImageList         m_imglist;
    COleDropTarget     m_droptarget;

    IDropTargetHelper* m_piDropHelper;
    bool               m_bUseDnDHelper;

    BOOL ReadHdropData ( COleDataObject* pDataObject );

// Generated message map functions
protected:
	//{{AFX_MSG(CMultiFilerView)
	afx_msg void OnRclick(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnSelectAll();
	afx_msg void OnInvertSelection();
	afx_msg void OnRemoveFromList();
	afx_msg void OnClearList();
	afx_msg void OnDestroy();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in MultiFilerView.cpp
inline CMultiFilerDoc* CMultiFilerView::GetDocument()
   { return (CMultiFilerDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MULTIFILERVIEW_H__D626641B_521D_4EE0_AAFB_96734DF04F15__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

Comments and Discussions