Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

Drag&Drop CListCtrl

Hello,
anybody knows a well sample how to use drag&drop on a CListCtrl? I want to enable this so the user can drag&drop database files on the CListCtrl, to open them.

Ingo
0
Ocrana
Asked:
Ocrana
1 Solution
 
eelkodegrootCommented:
Hi Ingo,

To let your program accept dropped files you have to handle and override two events: WM_CREATE and WM_DROPFILES
First implement OnCreate() and call DragAcceptFiles(TRUE). This register dynamically created windows as drop targets.
Then implement OnDropFiles() which processes the files.

Some sample code:

class MyListCtrl : public CListCtrl
{
public:
	MyListCtrl();
	virtual ~MyListCtrl();

protected:
	//{{AFX_MSG(CFileDropListCtrl)
	afx_msg void OnDropFiles(HDROP dropInfo);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	//}}AFX_MSG
};



BEGIN_MESSAGE_MAP(CFileDropListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CFileDropListCtrl)
	ON_WM_DROPFILES()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


int MyListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	DragAcceptFiles(TRUE);
	return 0;
}



void MyListCtrl::OnDropFiles(HDROP dropInfo)
{
	//
	// Get the number of pathnames (files or folders) dropped
	//
	UINT nNumFilesDropped = DragQueryFile(dropInfo, 0xFFFFFFFF, NULL, 0);

	TCHAR szFilename[MAX_PATH + 1];
	for (UNIT i = 0; i < nNumFilesDropped; ++i)
	{
		//
		// Get the pathname
		//
		DragQueryFile(dropInfo, nFile, szFilename, MAX_PATH + 1);

		//TODO: Process this file
	}

	//
	// Free the dropped-file info that was allocated by windows
	//
	DragFinish(dropInfo);
}

Open in new window


Does this answer your question?

Eelko
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now