Click here to Skip to main content
Licence CPOL
First Posted 26 Feb 2002
Views 118,863
Downloads 1,232
Bookmarked 30 times

CDropEdit

By Chris Losinger | 26 Feb 2002
Drag and drop files onto a CEdit, instead of using a file open dialog.

1
1 vote, 7.7%
2

3
4 votes, 30.8%
4
8 votes, 61.5%
5
4.58/5 - 18 votes
1 removed
μ 4.44, σa 1.58 [?]

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 Bamford12:15 23 Jul '09  
Questionnumeric Pinmembernasma9:05 14 Jun '08  
QuestionIt doesn't work when inside a groupbox Pinmembervakka21:13 28 Feb '07  
AnswerRe: It doesn't work when inside a groupbox Pinmemberryoaska17:47 2 Mar '07  
AnswerRe: It doesn't work when inside a groupbox Pinmemberryoaska21:03 2 Mar '07  
GeneralAnother way to do it without subclassing PinmemberGrazM10:05 24 May '06  
GeneralRe: Another way to do it without subclassing PinsupporterChris Losinger10:32 24 May '06  
GeneralRe: Another way to do it without subclassing PinmemberGrazM12:15 25 May '06  
GeneralA 5 after years PinmemberMircea Puiu8:54 26 Oct '05  
GeneralGoooooood! Pinmembercjwn8:11 26 Oct '05  
GeneralCustom Control Pinmemberyofnik13:40 1 Dec '03  
GeneralRe: Custom Control Pinmemberst0per16:15 5 Aug '04  
GeneralVery cool PinmemberHockey17:30 25 Aug '02  
Questionis 'now what?' a stupid question? Pinmembercedoc6:53 21 Aug '02  
AnswerRe: is 'now what?' a stupid question? PinsupporterChris Losinger6:57 21 Aug '02  
GeneralRe: is 'now what?' a stupid question? Pinmembercedoc9:41 21 Aug '02  
GeneralThank you PinsussAnonymous9:36 10 Aug '02  
GeneralWorking with CEdit control PinsussJustLetMePostThisDamnMessage14:15 24 Jul '02  
GeneralRe: Working with CEdit control PinsupporterChris Losinger14:19 24 Jul '02  
GeneralRe: Working with CEdit control PinmemberSilliSquid15:22 24 Jul '02  
GeneralNever believed this could happen Pinmemberjancsi6:43 11 Jul '02  
GeneralRe: Never believed this could happen PinmemberBlade[DMS]7:03 11 Jul '02  
GeneralReally smart piece of work :-) PinsubeditorNishant S1:51 11 Jul '02  
GeneralRe: Really smart piece of work :-) PinsupporterChris Losinger6:27 11 Jul '02  
Generaldoes not work as dialog Pinmemberghu5: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
Web03 | 2.5.120210.1 | Last Updated 27 Feb 2002
Article Copyright 2002 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid