Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / MFC
Article

Enhanced CListCtrl that accepts and filters dropped files and folders

Rate me:
Please Sign up or sign in to vote.
5.00/5 (20 votes)
6 Feb 2000 251.6K   3K   89   40
This article explains how to support file drag and drop in your CWnd-derived object

Demo project


Overview
How do I use it in my project?
How does it work?
Acknowledgements


Overview

CFileDropListCtrl - a class derived from CListCtrl that accepts files and/or folders dropped from Explorer.

  • Filters file types based on their extension
  • Resolves shortcuts
  • Checks for duplicate items
  • Allows custom processing of dropped items through an optional user callback function

It was developed to overcome the usability problems of CFileDialog for multiple file selection, and help cater for more experienced users.

I was using CFileDialog to select files to add to a list control. This was OK, but selecting multiple files from different folders is often tedious with the small size of CFileDialog on NT and 95 (it can be resized on 98 & 2000). Its a lot easier to locate files with Windows Explorer and drag and drop them onto the list control. There are less steps required for the user and its more intuitive for experienced ones.

The class has been tested with UNICODE, compiles cleanly on warning level 4 and is const correct.

How do I use it in my project?

You can quickly add this to new or existing projects by substituting it for your original CListCtrl and specifying the type of dropped items you want to accept.

By default, the list inserts the items itself - CListCtrl::InsertItem(0, csFilename) is called for each one. This will work with any style of list you have (Small Icon, Large Icon, List, Report). Note that if you've associated an image list, the default image (index 0) will be used and in Report view, the filename will be inserted into the first column.

If you want to do something fancier, say having a few columns in report view and showing the size and attributes of each file, you can! Give the control a callback function and it will notify you each time a suitable file is dropped - its then up to you to insert it, see below for details.

CFileDropListCtrl has two public members used to update and retrieve settings:

BOOL SetDropMode(const CFileDropListCtrl::DROPLISTMODE& dropMode)
DROPLISTMODE GetDropMode() const

struct DROPLISTMODE
{
    UINT iMask;
    CString csFileExt;
    LPFN_DROP_FILES_CALLBACK pfnCallback;
};
iMask: specifies what type of items to accept - a combination of these flags
FileDropListCtrl::DL_ACCEPT_FILESallow files to be dropped
CFileDropListCtrl::DL_ACCEPT_FOLDERSallow folders to be droppped
CFileDropListCtrl::DL_FILTER_EXTENSIONonly accept files with the specified extension. Specify in csFileExt
CFileDropListCtrl::DL_USE_CALLBACKreceive a callback for each item dropped, specified in pfnCallback (you have responsibility for inserting items into the list)
CFileDropListCtrl::DL_ALLOW_DUPLICATESaccept pathnames even if they already in the list (ignored if you are handling insertion through a callback function)

csFileExt: the file extension on which to filter. Use the format ".extension". Ignored unless DL_FILTER_EXTENSION is specified.

pfnCallback: address of your callback function. Ignored unless DL_USE_CALLBACK is specified.
See step 5 below for detailed info on the format of the callback and how to use it.

SetDropMode() return Values:
- TRUE if the mode was changed successfully
- FALSE if the mode is invalid (specifying DL_USE_CALLBACK, but not populating pfnCallback). The default settings will be used (accept files and folders with no duplicates)


Thats the interface, now step-by-step instructions on how to add and use it in your project:

  1. In resource editor, add a list control to your dialog and check the "Accept Files" property (on the Extended styles page).

    Set the list style to "List", unless you are using a callback function to insert items yourself. Remember that if you choose a "Report" style, you MUST insert one column before any items will be inserted.

  2. Use classwizard to assign a CListCtrl member variable to your list, e.g. m_List.
  3. Now in the header file of your dialog class:
    #include "FileDropListCtrl.h"

    And change the member variable type of your list from CListCtrl to CFileDropListCtrl
    i.e. CFileDropListCtrl m_List;

  4. Now you can specify what kind of items you want the list control to accept. In this case, its best to put it in your dialog class' OnInitDialog():
    CFileDropListCtrl::DROPLISTMODE dropMode;
    
    dropMode.iMask = CFileDropListCtrl::DL_ACCEPT_FILES | 
                          CFileDropListCtrl::DL_FILTER_EXTENSION;
    dropMode.csFileExt = _T(".txt");
    
    m_List.SetDropMode(dropMode);

    this will set the list to only accept files with an extension of ".txt".
    Note: The default mode is to accept all types of files and folders, but not to allow duplicate entries.

  5. Optionally specify a callback function. Without this, the control will take care of inserting items into the list, albeit simply.

    If you want to do something fancier you should install a callback - the control will use this to notify you each time a suitable file is dropped - its then up to you to insert it.

    Declare the callback as a static member function in your dialog class or as a global if you like:

    static HRESULT CALLBACK OnListFileDropped(CListCtrl* pList,
                                              const CString& csPathname,
                                              const UINT& iPathType    );
    pListpointer to the list control the item was dropped onto
    csPathnamefully qualified path of the item
    iPathTypeindicates the type of item.
    CFileDropListCtrl::DL_FOLDER_TYPE or
    CFileDropListCtrl::DL_FILE_TYPE

    In OnInitDialog(), register the callback along with other info - similar to step 4.

    dropMode.iMask |= CFileDropListCtrl::DL_USE_CALLBACK;
    dropMode.pfnCallback = CMyDialog::OnListFileDropped;

    Heres an example of how you might customise insertion by displaying the size of a dropped file:

    HRESULT CMyDialog::OnListFileDropped(CListCtrl* pList,
                                         const CString& csPathname,
                                         const UINT& iPathType    );
    {
        // Only do this for files.
        if(CFileDropListCtrl::DL_FILE_TYPE == iPathType)
        {
            // Get its size
            // returns DWORD in reality
            csFileSize = GetFileSize(csPathname); 
    
            // Insert the filename in column 1
            int nItem = pList->InsertItem(0, csPathname, IMAGE_INDEX);
    
            // And the size in column 2
            pList->SetItemText(nitem, 1, csFileSize);
        }
        return S_OK;
    }
  6. Thats all the code done. Now add the input library "shlwapi.lib" to your projects Link settings. This is for PathFindExtension(), one of many useful Shell Utility functions.
  7. Sorted, drag 'n' drop!...

How does it work?

The method used to handle dropped files is generic and can be applied to any CWnd derived object (e.g. a CEdit). You just need to handle and override 2 messages - WM_CREATE and WM_DROPFILES:
  1. CWnd::OnCreate() - call DragAcceptFiles(TRUE) to register dynamically created windows as drop targets
  2. CWnd::OnDropFiles() to process the files:

As an example, heres how you could handle WM_DROPFILES in a subclassed CEdit control:

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

    //
    // Iterate through them and do some funky stuff
    //
    TCHAR szFilename[MAX_PATH + 1];
    for (UINT nFile = 0 ; nFile < nNumFilesDropped; nFile++)
    {
        //
        // Get the pathname
        //
        DragQueryFile(dropInfo, nFile, szFilename, MAX_PATH + 1);

        //
        // Do something with it...pretty useless, but only example
        //
        CString csText;
        GetWindowText(csText);
        SetWindowText(csText + _T("; ") + szFilename);
    }

    //
    // Windows allocates the memory for file information,
    // so we must clean it up
    //
    DragFinish(dropInfo);
}
Also, you'll probably want to expand any dropped shortcuts before processing the filename, so take a look at CFileDropListCtrl::ExpandShortcut(). It uses the COM interface IShellLink for resolving them.

Acknowledgements

Thanks to these blokes for helping me get this working in double quick time!

  • Handling of droppped files adapted from CDropEdit, 1997 Chris Losinger
  • Shortcut expansion code adapted from CShortcut, 1996 Rob Warner

I'd appreciate a mail if you've any comments, suggestions, or bugs ;-)

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
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIn a CListCtrl , how do I drag and drop the items within the list? Pin
16-Oct-01 3:46
suss16-Oct-01 3:46 
AnswerRe: In a CListCtrl , how do I drag and drop the items within the list? Pin
David Fleming7-Mar-02 4:39
David Fleming7-Mar-02 4:39 
QuestionHow can I drop more extensions than one? Pin
19-Jul-01 3:01
suss19-Jul-01 3:01 
AnswerRe: How can I drop more extensions than one? Pin
Stuart Carter19-Jul-01 3:23
Stuart Carter19-Jul-01 3:23 
GeneralRe: How can I drop more extensions than one? Pin
20-Jul-01 9:31
suss20-Jul-01 9:31 
GeneralRe: How can I drop more extensions than one? Pin
Stuart Carter1-Aug-01 6:45
Stuart Carter1-Aug-01 6:45 
GeneralMaybe something better Pin
Deelip21-Sep-03 21:31
Deelip21-Sep-03 21:31 
GeneralRe: Maybe something better Pin
HatemElbehairy18-Jun-04 21:48
HatemElbehairy18-Jun-04 21:48 
GeneralRe: How can I drop more extensions than one? Pin
20-Jul-01 9:31
suss20-Jul-01 9:31 
QuestionHow Could I.. Pin
RobJones13-Jun-01 7:02
RobJones13-Jun-01 7:02 
AnswerRe: How Could I.. Pin
Stuart Carter13-Jun-01 21:55
Stuart Carter13-Jun-01 21:55 
GeneralRe: How Could I.. Pin
RobJones14-Jun-01 4:49
RobJones14-Jun-01 4:49 
GeneralRe: How Could I.. Pin
Nitron24-Jan-03 9:53
Nitron24-Jan-03 9:53 
Generalitem drag out from the list ctrl Pin
Farooq5-Jun-00 13:17
Farooq5-Jun-00 13:17 
GeneralRe: item drag out from the list ctrl Pin
Stuart Carter6-Jun-00 3:32
Stuart Carter6-Jun-00 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.