CDropEdit






4.57/5 (13 votes)
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:
- First, call
::CoInitialize(NULL);
in yourCWinApp::InitInstance
function. Also call::CoUninitialize();
in yourCWinApp::ExitInstance.
- Add a normal edit control to your dialog. Be sure to check its "Accept Files" property.
- In the header for your dialog class, declare a member
variable of type
CDropEdit
(be sure to#include "CDropEdit.h"
!) - In your dialog's
OnInitDialog
, call: - If you want the edit control to handle directories, call:
- If you want the edit control to handle files, call:
- That's it
CDropEdit m_dropEdit;
m_dropEdit.SubclassDlgItem(IDC_YOUR_EDIT_ID, this);
m_dropEdit.SetUseDir(TRUE);
m_dropEdit.SetUseDir(FALSE);
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.