Click here to Skip to main content
Licence CPOL
First Posted 26 Feb 2002
Views 120,855
Bookmarked 30 times

CDropEdit

By | 26 Feb 2002 | Article
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)

About the Author

Chris Losinger

Software Developer

United States United States

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWhen using this in a CFormView instead of a CDialog PinmemberHarold Bamford11:15 23 Jul '09  
Questionnumeric Pinmembernasma8:05 14 Jun '08  
QuestionIt doesn't work when inside a groupbox Pinmembervakka20:13 28 Feb '07  
AnswerRe: It doesn't work when inside a groupbox Pinmemberryoaska16:47 2 Mar '07  
AnswerRe: It doesn't work when inside a groupbox Pinmemberryoaska20:03 2 Mar '07  
GeneralAnother way to do it without subclassing PinmemberGrazM9:05 24 May '06  
GeneralRe: Another way to do it without subclassing PinmemberChris Losinger9:32 24 May '06  
GeneralRe: Another way to do it without subclassing PinmemberGrazM11:15 25 May '06  
GeneralA 5 after years PinmemberMircea Puiu7:54 26 Oct '05  
Quite short articleCry | :((
But long story short (so to say Smile | :) ) the idea is very good. And so is the control. Thanks.

 
SkyWalker
GeneralGoooooood! Pinmembercjwn7:11 26 Oct '05  
GeneralCustom Control Pinmemberyofnik12:40 1 Dec '03  
GeneralRe: Custom Control Pinmemberst0per15:15 5 Aug '04  
GeneralVery cool PinmemberHockey16:30 25 Aug '02  
Questionis 'now what?' a stupid question? Pinmembercedoc5:53 21 Aug '02  
AnswerRe: is 'now what?' a stupid question? PinmemberChris Losinger5:57 21 Aug '02  
GeneralRe: is 'now what?' a stupid question? Pinmembercedoc8:41 21 Aug '02  
GeneralThank you PinsussAnonymous8:36 10 Aug '02  
GeneralWorking with CEdit control PinsussJustLetMePostThisDamnMessage13:15 24 Jul '02  
GeneralRe: Working with CEdit control PinmemberChris Losinger13:19 24 Jul '02  
GeneralRe: Working with CEdit control PinmemberSilliSquid14:22 24 Jul '02  
GeneralNever believed this could happen Pinmemberjancsi5:43 11 Jul '02  
GeneralRe: Never believed this could happen PinmemberBlade[DMS]6:03 11 Jul '02  
GeneralReally smart piece of work :-) PinsubeditorNishant S0:51 11 Jul '02  
GeneralRe: Really smart piece of work :-) PinmemberChris Losinger5:27 11 Jul '02  
Generaldoes not work as dialog Pinmemberghu4:44 1 Mar '02  

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.

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