Click here to Skip to main content
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 Pinmemberk7773-Jun-13 8:49 
AnswerRe: How to do in CComboBox PinmemberChris Losinger3-Jun-13 9:08 
GeneralRe: How to do in CComboBox Pinmemberk7774-Jun-13 2:28 
GeneralRe: How to do in CComboBox PinmemberChris Losinger4-Jun-13 3:25 
GeneralRe: How to do in CComboBox Pinmemberk7774-Jun-13 4:28 
Questionhow to use this code with may textbox? Pinmemberngthtra20-Jun-12 21:08 
AnswerRe: how to use this code with may textbox? PinmemberChris Losinger21-Jun-12 1:07 
GeneralRe: how to use this code with may textbox? [modified] Pinmemberngthtra21-Jun-12 16:22 
GeneralRe: how to use this code with may textbox? PinmemberChris Losinger21-Jun-12 17:10 
GeneralWhen using this in a CFormView instead of a CDialog PinmemberHarold Bamford23-Jul-09 11:15 
Questionnumeric Pinmembernasma14-Jun-08 8:05 
QuestionIt doesn't work when inside a groupbox Pinmembervakka28-Feb-07 20:13 
AnswerRe: It doesn't work when inside a groupbox Pinmemberryoaska2-Mar-07 16:47 
AnswerRe: It doesn't work when inside a groupbox Pinmemberryoaska2-Mar-07 20:03 
GeneralAnother way to do it without subclassing PinmemberGrazM24-May-06 9:05 
GeneralRe: Another way to do it without subclassing PinmemberChris Losinger24-May-06 9:32 
GeneralRe: Another way to do it without subclassing PinmemberGrazM25-May-06 11:15 
GeneralA 5 after years PinmemberMircea Puiu26-Oct-05 7:54 
GeneralGoooooood! Pinmembercjwn26-Oct-05 7:11 
GeneralCustom Control Pinmemberyofnik1-Dec-03 12:40 
GeneralRe: Custom Control Pinmemberst0per5-Aug-04 15:15 
GeneralVery cool PinmemberHockey25-Aug-02 16:30 
Questionis 'now what?' a stupid question? Pinmembercedoc21-Aug-02 5:53 
AnswerRe: is 'now what?' a stupid question? PinmemberChris Losinger21-Aug-02 5:57 
GeneralRe: is 'now what?' a stupid question? Pinmembercedoc21-Aug-02 8:41 
GeneralThank you PinsussAnonymous10-Aug-02 8:36 
GeneralWorking with CEdit control PinsussJustLetMePostThisDamnMessage24-Jul-02 13:15 
GeneralRe: Working with CEdit control PinmemberChris Losinger24-Jul-02 13:19 
GeneralRe: Working with CEdit control PinmemberSilliSquid24-Jul-02 14:22 
GeneralNever believed this could happen Pinmemberjancsi11-Jul-02 5:43 
GeneralRe: Never believed this could happen PinmemberBlade[DMS]11-Jul-02 6:03 
GeneralReally smart piece of work :-) PinsubeditorNishant S11-Jul-02 0:51 
GeneralRe: Really smart piece of work :-) PinmemberChris Losinger11-Jul-02 5:27 
Generaldoes not work as dialog Pinmemberghu1-Mar-02 4:44 
GeneralRe: does not work as dialog PinmemberChris Losinger1-Mar-02 12:42 
GeneralRe: does not work as dialog PinmemberTim Smith1-Mar-02 13:04 
GeneralJust a minor detail PinmemberPål K Tønder27-Feb-02 21:08 
GeneralRe: Just a minor detail PinmemberChris Losinger28-Feb-02 1:31 
GeneralRe: Just a minor detail PinmemberChristian Skovdal Andersen28-Feb-02 10:22 
GeneralRe: Just a minor detail PinmemberTim Smith1-Mar-02 13:13 
Generaldarn... Pinmemberbryce27-Feb-02 15:09 
GeneralRe: darn... Pinmemberst0per5-Aug-04 15: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 | Mobile
Web02 | 2.8.141022.2 | Last Updated 27 Feb 2002
Article Copyright 2002 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid