Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CDropEdit

0.00/5 (No votes)
26 Feb 2002 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here