Drag N Drop Text To Dialog

I know how to drag and drop a file to a dialog. I already have this in place.
My problem is that I also would like to be able to drag and drop text (mainly specific contents of a file) to my dialog.

I have a text file that says: "My dog is my best friend."
Now if I select just "dog is my" and drag it from the text file, is there a way to get the event so I can process the drag text "dog is my".
I would like to keep the DropFiles part working as well.

NOTE: this text file is not part of my app, it could be any text editor, text field, html file... anything that gives the cursor signal to dragDrop.
ALSO: I have copy & paste working but would like to also have the dragDrop text.

chris rrrsoftware/pipelineAsked:
Who is Participating?
To get this functionality you working you must ensure that other application initializes drag process. Open Notepad, type some text and try to drag it - it doesn't work. This means, you must control initiating of drag process from your application using Windows hooks, invisible window which covers the whole screen or some other techique. In such case you must recognize text in selected screen area which doesn't belong to your program and initiale dragging process. The easy part is to accept drop in your application.
The challenging part is recignizing text in selected screen area. There is no general way to get this text from other application. Usually this is done using text recognition from image - not trivial task. You must decide whether you really need this or simple Paste functionality is OK for you.
Dariusz DziaraProgrammerCommented:
To implement Ole Drag & Drop (which is something different than File Drag & Drop) you need 3 COM objects that implements 3 COM interfaces:
IDataObject - carries data (in your case in CF_TEXT format)
IDropSource - for example decides if continue dragging
IDropTarget - used by drag & drop target window (in your case it may be EDIT box).

If you use MFC the task is much easier - there are C++ classes that implements these interfaces.
COleDropSource(), COleDropTarget(), COleDataObject(), COleDataSource()

Dariusz DziaraProgrammerCommented:
For example Windows Wordpad implements this feature. You can use it for tests.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

chris rrrsoftware/pipelineAuthor Commented:
OK did a little test and I found that richEdit boxes give this functionality. If I place this in my dialog, then I could use it (but I don't want there to be a text box there). Or I could subclass it , and do my drawing right inside the control instead of my dialog? ... don't know.

Any Ideas?

If not I believe that I will just close this question. Sounds like more time than I can spend on it.

Dariusz DziaraProgrammerCommented:
The following is from MSDN:

HOWTO: Use Drag-Drop in an Edit Control or a Combo Box
Article ID: Q86724

The information in this article applies to:
Microsoft Windows Software Development Kit (SDK)
Microsoft Win32 Software Development Kit (SDK)

In the Microsoft Windows environment, an application can register an edit control or a combo box as a drag-drop client through the DragAcceptFiles function. The application must also subclass the control to process the WM_DROPFILES message that Windows sends when the user drops a file.

The following seven steps demonstrate how to implement drag-drop in an edit control. The procedure to implement drag-drop in a combo box is identical.

1. Add SHELL.LIB to the list of libraries required to build the file.

2. Add the name of the subclass procedure (MyDragDropProc) to the

   EXPORTS section of the module definition (DEF) file.

3. Include the SHELLAPI.H file in the application's source code.
4. Declare the following procedure and variables:

      BOOL FAR PASCAL MyDragDropProc(HWND, unsigned, WORD, LONG);

      FARPROC lpfnDragDropProc, lpfnOldEditProc;
      char    szTemp64[64];

5. Add the following code to the initialization of the dialog box:

      case WM_INITDIALOG:
         // ... other code

         // ------- edit control section --------
         hWndTemp = GetDlgItem(hDlg, IDD_EDITCONTROL);
         DragAcceptFiles(hWndTemp, TRUE);

         // subclass the drag-drop edit control
         lpfnDragDropProc = MakeProcInstance(MyDragDropProc, hInst);

         if (lpfnDragDropProc)
            lpfnOldEditProc = SetWindowLong(hWndTemp, GWL_WNDPROC,

6. Write a subclass window procedure for the edit control.

      BOOL FAR PASCAL MyDragDropProc(HWND hWnd, unsigned message,
                                     WORD wParam, LONG lParam)
         int wFilesDropped;

         switch (message)
         case WM_DROPFILES:
            // Retrieve number of files dropped
            // To retrieve all files, set iFile parameter
            // to -1 instead of 0
            wFilesDropped = DragQueryFile((HDROP)wParam, 0,
                  (LPSTR)szTemp64, 63);

            if (wFilesDropped)
               // Parse the file path here, if desired
               SendMessage(hWnd, WM_SETTEXT, 0, (LPSTR)szTemp64);


            return CallWindowProc(lpfnOldEditProc, hWnd, message,
                  wParam, lParam);
         return TRUE;

7. After the completion of the dialog box procedure, free the edit
   control subclass procedure.

      if (lpfnDragDropProc)
chris rrrsoftware/pipelineAuthor Commented:
I cannot get this to work with MFC?
I assume the dragDropProc would have to be a global var, but it still doesn't work.

I get cast errors:

Here :
         lpfnDragDropProc = MakeProcInstance(MyDragDropProc, hInst);


Here :
         if (lpfnDragDropProc)
            lpfnOldEditProc = SetWindowLong(hWndTemp, GWL_WNDPROC,
chris rrrsoftware/pipelineAuthor Commented:
Oh, and the lib file is not shell.lib it is shell32.lib
Dariusz DziaraProgrammerCommented:
Do you want it to work in MFC ?

Derive you EDIT interface class from CEdit (add new MFC class, choose CEdit as a base class)

Add to the class derived from CEdit:



BEGIN_MESSAGE_MAP(...)          // in .cpp file
<- here

Add function declaration


in class declaration



and before


add implementation to .cpp file


"Oh, and the lib file is not shell.lib it is shell32.lib"

Is it a problem ? You have 32-bit aplication so you should use 32-bit version of library. It has its version of DragQueryFile().
Dariusz DziaraProgrammerCommented:
In OnInitDialog() handler call m_edit.DragAcceptFiles(TRUE);

Where m_edit is of type CYourEditDerivedClass (use clas Wizard to add it)
chris rrrsoftware/pipelineAuthor Commented:
Thanks you mrblue for your time
I have Updated from 200pts to 300pts

One quick question...
Is is going to be possible to do make the OnDropFiles work on the dialog and not on the CEdit subclass?
Earlier I posted that the drag and drop works fine if I use a RichEdit control.
 ** See post: Date: 05/08/2006 01:21AM PDT

I am hoping to be able to use my dialogs normal drag and drop for files and for text.
I can easily test if file exists and know if it is text or file.

I have drag and drop working on my dialog right now, but if I drag text then I don't even get the event.
Dariusz DziaraProgrammerCommented:
"I have drag and drop working on my dialog right now, but if I drag text then I don't even get the event."

I am not sure if I understand you well. What does it mean "working" if you "don't get event" ?

If you want some window to accept WM_DROPFILES you need call
for that window.

For dialog box in MFC just call

You have also do the same thing for dialog class as for derived CEdit class - define message handler in dialog class.

chris rrrsoftware/pipelineAuthor Commented:
Sorry for the confusion.
I have drag and drop working if I drag a file from explorer ( I get an event if I drop over my dialog if it is related to a file). (this must have to do with dropFile?)
However, if I drag from a textfield or text editor ( some text ), when I move my cursor over my dialog, I get the dragDrop icon, But if I drop the text over my dialog I get no event.

Hope this makes sense.
Dariusz DziaraProgrammerCommented:
As  I mentioned earlier there are two different types of Drag And Drop: simple File Drag And Drop & much more sophisticated Ole Drag & Drop.
When you drag text from Wordpad the second type will be used. Unterfortunately no WM_DROPFILES will be posted.


1. To start Ole Drag And Drop in
construct COleDataSource object & initilaize it with selection of type CF_TEXT in your case.
This step is required if you want to be DnD source. You don't need it.


2. To window class that you want to be drop target add variable of type
derived from COleDropTarget;

CYourOleDropTarget m_droptarget;

Pass somehow pointer to your window class (to access window class from inside CYourOleDropTarget object) - in constructor maybe or call some method to do it.

Override following members:

OnDrop                 - corresponds to WM_DROPFILES handler


In WM_CREATE or WM_INITDIALOG handler register your window class as DnD target

m_droptarget.Register(this /*your window class*/);

And this is enough to Drag & Drop text from Wordpad to your apllication
chris rrrsoftware/pipelineAuthor Commented:
I will give it a shot.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.