Drag&Drop CListCtrl

Posted on 2011-10-08
Last Modified: 2012-06-27
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.

Question by:Ocrana
    1 Comment
    LVL 1

    Accepted Solution

    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
    	virtual ~MyListCtrl();
    	afx_msg void OnDropFiles(HDROP dropInfo);
    	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    BEGIN_MESSAGE_MAP(CFileDropListCtrl, CListCtrl)
    int MyListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    	if (CListCtrl::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	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

    Open in new window

    Does this answer your question?


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now