Click here to Skip to main content
15,879,096 members
Articles / Desktop Programming / MFC
Article

CDropEdit

Rate me:
Please Sign up or sign in to vote.
4.57/5 (14 votes)
26 Feb 2002CPOL1 min read 165.3K   1.9K   35   42
Drag and drop files onto a CEdit, instead of using a file open dialog.

Introduction

This slight variation on the standard CEdit control allows users to drag and drop a file onto the control, instead of typing the path to the file. When a file (or folder) is dropped onto this control, the path to that file becomes the window text. This is an alternative to using a typical file-browse dialog.

Why

This is just another thing I do to make my apps easy to use. I never rely on this as the only way to get a path into the control, just another option. Plus, the code here can be adopted to almost any other control, so you can drag onto combo boxes, list boxes, etc., so I do this to any control that can accept a file name.

How

Using this class is fairly easy:

  1. First, call ::CoInitialize(NULL); in your
    CWinApp::InitInstance
    function. Also call ::CoUninitialize(); in your CWinApp::ExitInstance.
  2. Add a normal edit control to your dialog. Be sure to check its "Accept Files" property.
  3. In the header for your dialog class, declare a member variable of type CDropEdit (be sure to
    #include
    "CDropEdit.h"
    !)
  4. CDropEdit m_dropEdit;
  5. In your dialog's OnInitDialog, call:
  6. m_dropEdit.SubclassDlgItem(IDC_YOUR_EDIT_ID, this);
  7. If you want the edit control to handle directories, call:
  8. m_dropEdit.SetUseDir(TRUE);
  9. If you want the edit control to handle files, call:
  10. m_dropEdit.SetUseDir(FALSE);
  11. That's it

More

This code just shows the basic technique for getting the names of dropped files. It's fairly easy to modify this code to handle multiple dropped files, if you're filling a list box, for example. And, it's pretty simple to change this code to do other things with the dropped files, like display them, or execute them, or delete them, or chop them into little bits, or send them as attachments to 1,000 strangers, etc...

Have fun.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
GeneralNever believed this could happen Pin
jancsi11-Jul-02 5:43
jancsi11-Jul-02 5:43 
GeneralRe: Never believed this could happen Pin
Blade[DMS]11-Jul-02 6:03
Blade[DMS]11-Jul-02 6:03 
GeneralReally smart piece of work :-) Pin
Nish Nishant11-Jul-02 0:51
sitebuilderNish Nishant11-Jul-02 0:51 
GeneralRe: Really smart piece of work :-) Pin
Chris Losinger11-Jul-02 5:27
professionalChris Losinger11-Jul-02 5:27 
Generaldoes not work as dialog Pin
1-Mar-02 4:44
suss1-Mar-02 4:44 
GeneralRe: does not work as dialog Pin
Chris Losinger1-Mar-02 12:42
professionalChris Losinger1-Mar-02 12:42 
GeneralRe: does not work as dialog Pin
Tim Smith1-Mar-02 13:04
Tim Smith1-Mar-02 13:04 
GeneralJust a minor detail Pin
Pål K Tønder27-Feb-02 21:08
Pål K Tønder27-Feb-02 21:08 
If I was picky, I could remark that the cursor should not signal drop acceptance of files when only directories were allowed, or directories when only files were allowed. Luckily I'm not (and I know that DragAcceptFiles doesn't provide much flexibility).

Have a nice day Wink | ;)
GeneralRe: Just a minor detail Pin
Chris Losinger28-Feb-02 1:31
professionalChris Losinger28-Feb-02 1:31 
GeneralRe: Just a minor detail Pin
Christian Skovdal Andersen28-Feb-02 10:22
Christian Skovdal Andersen28-Feb-02 10:22 
GeneralRe: Just a minor detail Pin
Tim Smith1-Mar-02 13:13
Tim Smith1-Mar-02 13:13 
Generaldarn... Pin
bryce27-Feb-02 15:09
bryce27-Feb-02 15:09 
GeneralRe: darn... Pin
st0per5-Aug-04 15:44
st0per5-Aug-04 15:44 

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.