Click here to Skip to main content
Click here to Skip to main content

CDropEdit

By , 26 Feb 2002
 

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionhow to use this code with may textbox?memberngthtra20 Jun '12 - 21:08 
in this example, you only use one textbox but How do I have to handle if I have two or three textbox?
AnswerRe: how to use this code with may textbox?memberChris Losinger21 Jun '12 - 1:07 
repeat steps 2-6 for each edit control.

GeneralRe: how to use this code with may textbox? [modified]memberngthtra21 Jun '12 - 16:22 
If I work on C++ MFC dialog using 2010, how do I must edit this code to run in VS2010.
what value is pcWndTest replaced by?

modified 21 Jun '12 - 23:36.

GeneralRe: how to use this code with may textbox?memberChris Losinger21 Jun '12 - 17:10 
i don't think you will need any changes.
 
but, if you get any errors when compiling this code, post them here.

GeneralWhen using this in a CFormView instead of a CDialogmemberHarold Bamford23 Jul '09 - 11:15 
I found that this works well in a CFormView if I change one of your steps from:
 
m_dropEdit.SubclassDlgItem(IDC_YOUR_EDIT_ID, this);
 
to
 
m_dropEdit.SubclassDlgItem(IDC_YOUR_EDIT_ID, AfxGetMainWnd());
 
Probably most already know this but I so rarely do this kind of thing that it took me 1/2 hour to figure it out.
 
Good article. Thanks!
Questionnumericmembernasma14 Jun '08 - 8:05 

how to install a numeric editor component
like the one in windows date and time wizard
 
edit with two pointers to increase & decrease time
 

QuestionIt doesn't work when inside a groupboxmembervakka28 Feb '07 - 20:13 
When I put CDropEdit inside a groupbox (CStatic element with property "AcceptFiles" to "true") it doesn't accept dropped files or folders. When I set groupbox property "Dissabled" to "true" it works, but the groupbox title becomes gray. Is there another way to fix this problem?
Thanks.
 
Vadim
AnswerRe: It doesn't work when inside a groupboxmemberryoaska2 Mar '07 - 16:47 
oh man, did you ever figure out a way to get around this? It's funny that I ran into this problem only a day after you. I was going nuts making sure I covered everything in the example, and I was beginning to think my problem was the fact that the dialog with my CDropEdit is in a tab control. But then I read your comment, disabled the group box, and it worked perfectly...
AnswerRe: It doesn't work when inside a groupboxmemberryoaska2 Mar '07 - 20:03 
I found the solution in another thread. The problem was the z-order of my controls. Your group box was probably added before your CDropEdit control- try deleting the group box and replacing it.
 
Hope that works for you too!

GeneralAnother way to do it without subclassingmemberGrazM24 May '06 - 9:05 
I've used this method successfully in Dialogs. It doesn't use COM and doesn't use SubClassing, The control class handles the processing independantly of the Dialog, so it can be as simple or as complex as you need.
 
Create a new DropEdit class that inherits from CEdit.
 
// ======================= DropEdit.h =================================
#pragma once
// CDropEdit
 
class CDropEdit : public CEdit
{
DECLARE_DYNAMIC(CDropEdit)
 
public:
CDropEdit();
virtual ~CDropEdit();
 
protected:
void OnDropFiles(HDROP hDropInfo);
DECLARE_MESSAGE_MAP()
};
 

In the implementation file process the ON_WM_DROPFILES message
 
// ========================= DropEdit.cpp ==============
 
// DropEdit.cpp : implementation file
//
 
#include "stdafx.h"
#include "DropEdit.h"
// CDropEdit
 
IMPLEMENT_DYNAMIC(CDropEdit, CEdit)
 
BEGIN_MESSAGE_MAP(CDropEdit, CEdit)
ON_WM_DROPFILES()
END_MESSAGE_MAP()
 
CDropEdit::CDropEdit()
{
 
}
 
CDropEdit::~CDropEdit()
{
}
 
void CDropEdit::OnDropFiles(HDROP hDropInfo)
{
TCHAR szFileName[_MAX_PATH];
DragQueryFile(hDropInfo,0,szFileName, _MAX_PATH - 1);
SetWindowText(szFileName);
}
 
In your application files declare a CDropEdit control variable .
 
// ================== TestDlg.h ===================
// TestDlg.h
#include "DropEdit.h"
 
Class CTestDlg : public CDialog
{
 
public:
CDropEdit m_DropEdit;
CString m_FilePath;
 
};
 
Finally add the Dialog initalization and message entries
// =================== TestDlg.cpp ==============
 
// TestDlg.cpp
 
// IDC_FILENAME is a standard Edit control in the Dialog
 
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_FILENAME, m_FilePath);
DDX_Control(pDX, IDC_FILENAME, m_DropEdit);
}
 
BOOL CTestDlg::OnInitDialog()
{
// insert one line to enable drag/drop
 
::DragAcceptFiles(m_DropEdit.m_hWnd,TRUE);
 
}
 
Now when you drop a file into the edit control, the DropEdit class handles the message internally, and the file path just appears as window text in the edit control.
 
You get the path by using UpDateData() or GetDlgItemText(...) API calls, whichever you prefer.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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