Click here to Skip to main content
11,428,876 members (60,671 online)
Click here to Skip to main content

CDropEdit

, 26 Feb 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Chris Losinger
Software Developer
United States United States
Chris Losinger is the president of Smaller Animals Software, Inc..

Comments and Discussions

 
QuestionHow to do in CComboBox Pin
k7773-Jun-13 9:49
memberk7773-Jun-13 9:49 
AnswerRe: How to do in CComboBox Pin
Chris Losinger3-Jun-13 10:08
memberChris Losinger3-Jun-13 10:08 
GeneralRe: How to do in CComboBox Pin
k7774-Jun-13 3:28
memberk7774-Jun-13 3:28 
GeneralRe: How to do in CComboBox Pin
Chris Losinger4-Jun-13 4:25
memberChris Losinger4-Jun-13 4:25 
GeneralRe: How to do in CComboBox Pin
k7774-Jun-13 5:28
memberk7774-Jun-13 5:28 
Questionhow to use this code with may textbox? Pin
ngthtra20-Jun-12 22:08
memberngthtra20-Jun-12 22:08 
AnswerRe: how to use this code with may textbox? Pin
Chris Losinger21-Jun-12 2:07
memberChris Losinger21-Jun-12 2:07 
GeneralRe: how to use this code with may textbox? [modified] Pin
ngthtra21-Jun-12 17:22
memberngthtra21-Jun-12 17:22 
GeneralRe: how to use this code with may textbox? Pin
Chris Losinger21-Jun-12 18:10
memberChris Losinger21-Jun-12 18:10 
GeneralWhen using this in a CFormView instead of a CDialog Pin
Harold Bamford23-Jul-09 12:15
memberHarold Bamford23-Jul-09 12:15 
Questionnumeric Pin
nasma14-Jun-08 9:05
membernasma14-Jun-08 9:05 
QuestionIt doesn't work when inside a groupbox Pin
vakka28-Feb-07 21:13
membervakka28-Feb-07 21:13 
AnswerRe: It doesn't work when inside a groupbox Pin
ryoaska2-Mar-07 17:47
memberryoaska2-Mar-07 17:47 
AnswerRe: It doesn't work when inside a groupbox Pin
ryoaska2-Mar-07 21:03
memberryoaska2-Mar-07 21:03 
GeneralAnother way to do it without subclassing Pin
GrazM24-May-06 10:05
memberGrazM24-May-06 10:05 
GeneralRe: Another way to do it without subclassing Pin
Chris Losinger24-May-06 10:32
memberChris Losinger24-May-06 10:32 
GeneralRe: Another way to do it without subclassing Pin
GrazM25-May-06 12:15
memberGrazM25-May-06 12:15 
GeneralA 5 after years Pin
Mircea Puiu26-Oct-05 8:54
memberMircea Puiu26-Oct-05 8:54 
GeneralGoooooood! Pin
cjwn26-Oct-05 8:11
membercjwn26-Oct-05 8:11 
GeneralCustom Control Pin
yofnik1-Dec-03 13:40
memberyofnik1-Dec-03 13:40 
GeneralRe: Custom Control Pin
st0per5-Aug-04 16:15
memberst0per5-Aug-04 16:15 
GeneralVery cool Pin
Hockey25-Aug-02 17:30
memberHockey25-Aug-02 17:30 
Questionis 'now what?' a stupid question? Pin
cedoc21-Aug-02 6:53
membercedoc21-Aug-02 6:53 
AnswerRe: is 'now what?' a stupid question? Pin
Chris Losinger21-Aug-02 6:57
memberChris Losinger21-Aug-02 6:57 
GeneralRe: is 'now what?' a stupid question? Pin
cedoc21-Aug-02 9:41
membercedoc21-Aug-02 9:41 
GeneralThank you Pin
Anonymous10-Aug-02 9:36
sussAnonymous10-Aug-02 9:36 
GeneralWorking with CEdit control Pin
JustLetMePostThisDamnMessage24-Jul-02 14:15
sussJustLetMePostThisDamnMessage24-Jul-02 14:15 
GeneralRe: Working with CEdit control Pin
Chris Losinger24-Jul-02 14:19
memberChris Losinger24-Jul-02 14:19 
GeneralRe: Working with CEdit control Pin
SilliSquid24-Jul-02 15:22
memberSilliSquid24-Jul-02 15:22 
GeneralNever believed this could happen Pin
jancsi11-Jul-02 6:43
memberjancsi11-Jul-02 6:43 
GeneralRe: Never believed this could happen Pin
Blade[DMS]11-Jul-02 7:03
memberBlade[DMS]11-Jul-02 7:03 
GeneralReally smart piece of work :-) Pin
Nishant S11-Jul-02 1:51
subeditorNishant S11-Jul-02 1:51 
GeneralRe: Really smart piece of work :-) Pin
Chris Losinger11-Jul-02 6:27
memberChris Losinger11-Jul-02 6:27 
Generaldoes not work as dialog Pin
ghu1-Mar-02 5:44
memberghu1-Mar-02 5:44 
GeneralRe: does not work as dialog Pin
Chris Losinger1-Mar-02 13:42
memberChris Losinger1-Mar-02 13:42 
GeneralRe: does not work as dialog Pin
Tim Smith1-Mar-02 14:04
memberTim Smith1-Mar-02 14:04 
GeneralJust a minor detail Pin
Pål K Tønder27-Feb-02 22:08
memberPål K Tønder27-Feb-02 22:08 
GeneralRe: Just a minor detail Pin
Chris Losinger28-Feb-02 2:31
memberChris Losinger28-Feb-02 2:31 
GeneralRe: Just a minor detail Pin
Christian Skovdal Andersen28-Feb-02 11:22
memberChristian Skovdal Andersen28-Feb-02 11:22 
GeneralRe: Just a minor detail Pin
Tim Smith1-Mar-02 14:13
memberTim Smith1-Mar-02 14:13 
Generaldarn... Pin
bryce27-Feb-02 16:09
memberbryce27-Feb-02 16:09 
GeneralRe: darn... Pin
st0per5-Aug-04 16:44
memberst0per5-Aug-04 16:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 27 Feb 2002
Article Copyright 2002 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid