Click here to Skip to main content
15,867,453 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 164.8K   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

 
QuestionHow to do in CComboBox Pin
k7773-Jun-13 8:49
k7773-Jun-13 8:49 
AnswerRe: How to do in CComboBox Pin
Chris Losinger3-Jun-13 9:08
professionalChris Losinger3-Jun-13 9:08 
GeneralRe: How to do in CComboBox Pin
k7774-Jun-13 2:28
k7774-Jun-13 2:28 
GeneralRe: How to do in CComboBox Pin
Chris Losinger4-Jun-13 3:25
professionalChris Losinger4-Jun-13 3:25 
GeneralRe: How to do in CComboBox Pin
k7774-Jun-13 4:28
k7774-Jun-13 4:28 
Questionhow to use this code with may textbox? Pin
ngthtra20-Jun-12 21:08
ngthtra20-Jun-12 21:08 
AnswerRe: how to use this code with may textbox? Pin
Chris Losinger21-Jun-12 1:07
professionalChris Losinger21-Jun-12 1:07 
GeneralRe: how to use this code with may textbox? Pin
ngthtra21-Jun-12 16:22
ngthtra21-Jun-12 16:22 
GeneralRe: how to use this code with may textbox? Pin
Chris Losinger21-Jun-12 17:10
professionalChris Losinger21-Jun-12 17:10 
GeneralWhen using this in a CFormView instead of a CDialog Pin
Harold Bamford23-Jul-09 11:15
Harold Bamford23-Jul-09 11:15 
Questionnumeric Pin
nasma14-Jun-08 8:05
nasma14-Jun-08 8:05 
QuestionIt doesn't work when inside a groupbox Pin
vakka28-Feb-07 20:13
vakka28-Feb-07 20:13 
AnswerRe: It doesn't work when inside a groupbox Pin
ryoaska2-Mar-07 16:47
ryoaska2-Mar-07 16:47 
AnswerRe: It doesn't work when inside a groupbox Pin
ryoaska2-Mar-07 20:03
ryoaska2-Mar-07 20:03 
GeneralAnother way to do it without subclassing Pin
DevaRoo24-May-06 9:05
DevaRoo24-May-06 9:05 
GeneralRe: Another way to do it without subclassing Pin
Chris Losinger24-May-06 9:32
professionalChris Losinger24-May-06 9:32 
GeneralRe: Another way to do it without subclassing Pin
DevaRoo25-May-06 11:15
DevaRoo25-May-06 11:15 
GeneralA 5 after years Pin
Mircea Puiu26-Oct-05 7:54
Mircea Puiu26-Oct-05 7:54 
GeneralGoooooood! Pin
cjwn26-Oct-05 7:11
cjwn26-Oct-05 7:11 
GeneralCustom Control Pin
yofnik1-Dec-03 12:40
yofnik1-Dec-03 12:40 
GeneralRe: Custom Control Pin
st0per5-Aug-04 15:15
st0per5-Aug-04 15:15 
GeneralVery cool Pin
alex.barylski25-Aug-02 16:30
alex.barylski25-Aug-02 16:30 
Questionis 'now what?' a stupid question? Pin
cedoc21-Aug-02 5:53
cedoc21-Aug-02 5:53 
AnswerRe: is 'now what?' a stupid question? Pin
Chris Losinger21-Aug-02 5:57
professionalChris Losinger21-Aug-02 5:57 
GeneralRe: is 'now what?' a stupid question? Pin
cedoc21-Aug-02 8:41
cedoc21-Aug-02 8:41 

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.