Virtual Developer Workshop: Containerized Development with Docker

Recently, I completed a CDragEdit class. With this class, user can copy/move string between edit controls or other editors just by drag-and-drop.

The drag_edit_src.zip is the source code of CDragEdit and the drag_edit_test.zip is the test program, including its source code also, of CDragEdit. Both of zip files have one README.TXT. In this file, I have described how to use it and how it works. For your convenience, I also listed the content of README.TXT at the bottom of this mail.

In the past time, if you want to copy/move string from one edit control to another edit control or copy/move string from one edit window of foreign application to a edit control, only you can do is copy, cut, and paste via clipboard. For such a inconvenience, I implemented a CDragEdit control which is based on the standard CEdit control so that you can override the existent application's edit control as a drag-drop enabled control very easily. Besides that the CDragEdit's drag-drop capability is implemented via OLE's UDT (Uniform Data Transfer) so you can use its drag-drop to share data with most applications. e.g. Visual C++, WordPad, Word, ...,etc.

If you have any question about the CDragEdit, don't hesitate to e-mail to me.

* Class hierarchy of CDragEdit


* How to use the CDragEdit ?

To use a drag edit control in an existing dialog box

In your dialog definition file, you need to include the "CDragEdit.h" first, and then declare CDragEdit member variable corresponding to each edit control that you want it become a drag-drop enabled control. After you have declared the CDragEdit variables, you have to use DDX_Control() to subclass the standard edit control as a CDragEdit control in DoDataExchange() and call CDragEdit::Init(), in OnInitialDialog(), to initiate the CDragEdit and enable its drag-drop capability.

To use a drag edit control in toolbar

Calling CDragEdit::Create() to create a drag edit control and then call CDragEdit::Init() to initiate this edit control and enable its drag-drop capability.

For example, you want to use two drag edit controls in the CTestDragEDitDlg. You need to add some codes in your xxxDlg.h and xxxDlg.cpp as below:

////////////////////// //TestDragEditDlg.h ////////////////////// #include
"CDragEdit.h" //include it in your xxxDlg.h

class CTestDragEditDlg : public CDialog
	// Dialog Data
	enum { IDD = IDD_DRAGEDIT };
	CDragEdit m_edit2; //declare a CDragEdit variable 
	CDragEdit m_edit1; //for each edit control

void CTestDragEditDlg::DoDataExchange(CDataExchange* pDX)
	DDX_Control(pDX, IDC_EDIT2, m_edit2); //subclass the standard edit
	DDX_Control(pDX, IDC_EDIT1, m_edit1); //control as a CDragEdit

BOOL CTestDragEditDlg::OnInitDialog() 
	    //You need to initiate CDragEdit first 

How the CDragEdit works ?

As I mentioned, the drag-drop of CDragEdit is implemented via OLE drag-and-drop. So, I implemented a CDEDropTarget, which is based on COleDropTarget, and a CDEDataSource, which is based on COleDataSource. The CDEDropTaregt serves the CDragEdit as a data comsumer and the CCDEDataSource serves the CDragEdit as a data producer. Drag-drop data transfers usually begin when the user press the left button of mouse. So, in CDragEdit, simply handle the WM_LBUTTONDOWN messagem, coping selected string into a CDEDataSource object using CacheGlobalData(), and call CDEDataSource::DoDragDrop() to tranfer string to a drop target.

In CDragEdit::Init(), I register this window as a drop target by calling CDEDropTarget::Register(). Once a CDragEdit is registered with OLE to be a drop target, it becomes eligible to receive OnDropEnter(), OnDropOver(), OnDropLeave(), and OnDrop() notifications from OLE. In OnDrop(), the CDragEdit will retrieve data with CF_TEXT format from COleDataObject and place at the dropped point.

For more information, please refer to the comment of source code.


  • How do I swap contents from one edit box to another

    Posted by Legacy on 02/19/2004 08:00am

    Originally posted by: Chris

    I am new at this programming stuff and seen some good stuff on this site. Just wondering if someone could give me an example of how to swap the contents of one edit box to another edit box?
    Please Help !!


  • How do i apply it in borland c++ builder?

    Posted by Legacy on 10/13/2002 07:00am

    Originally posted by: Tony

    How do i apply it in borland c++ builder?
    Could you post a example using bcb?

  • Property pages

    Posted by Legacy on 08/12/2002 07:00am

    Originally posted by: Wendell Peeples

    How do you get the total edit boe on page one to transfer the number to the first edit box of page two.  The problem I am having is my program calculates the total on the first page for example:  My property page has three edit boxes, when you enter an amount lets say 20 in box 1 and 50 in box two , box three display the answer 70.  How do you get that total in the edit box on page 2.  These are two different files.

  • How do I drag a row of text from one application and make it an image in the other another

    Posted by Legacy on 06/17/2002 07:00am

    Originally posted by: Ryan

    I was just wondering if any of you knew how to drag text from one application to another. I know how to drag images I just do not know how to drag and drop text from one application and drop it as an image.

    Ryan Henry

  • How do I append a text in Edit control.

    Posted by Legacy on 03/21/2002 08:00am

    Originally posted by: S.R.Krishnan

    The eg you put was great.My need is as follows.I have got a huge list of texts to be appended to the existing text control.This might happen at regular intervals of time.Iam also forced to use Edit control only.(Though Richedit provied better alternative).

    How will I add a text.

    Is there any message like LB_ADDSTRING for a listbox.


  • ExEditors Library

    Posted by Legacy on 03/19/2002 08:00am

    Originally posted by: Mike Philips

    A complete collection of editors in a single file:



  • CListBox Drag and Drop

    Posted by Legacy on 03/07/2002 08:00am

    Originally posted by: amit khera

    How do I drag a list item from one CListBox to another?

  • using hot key to copy.....

    Posted by Legacy on 09/08/2001 07:00am

    Originally posted by: jonse

    How to get the string marked by me by using hot key ??

    I want to copy string from any application(just as like txt,words and IE) to IDC_EDIT2 by using hot key .....

    Can you help me...??

  • Display a text file in an edit Control when an open and save button is pressed

    Posted by Legacy on 05/11/2001 07:00am

    Originally posted by: sharon

    I am very happy to see that your code and commants are nice
    I want to know more about edit control.Any Text file has to be Opened or Saved on an Edit control. when ever I press an Open Button it has to open a text file and when pressed on save it has to be save the text file. Please kindly let me know about it.

  • Correct your PosFromChar Function

    Posted by Legacy on 03/23/2001 08:00am

    Originally posted by: Qing

    I am very happy to see your code. when I used SetMargins
    Function, I found little bug in your PosFromChar Function.

    Here is the fix:

    CPoint CDragEdit::PosFromChar(UINT uChar)
    CRect r; // added
    GetRect(&r); // added
    if (0==uChar)
    return CPoint(r.left,r.top); // changed
    CPoint pt=CDragEditBase::PosFromChar(uChar);
    if (pt.x<0 && pt.y<0) {
    int nLine, nPos;
    //get dc and select current using font
    CClientDC dc(this);
    //get position of previous char
    if (nPos==0) {
    //if current char is the first char
    //we get the current y from previous y plus font height
    CSize szFont=dc.GetTextExtent("A",1);
    pt.x=r.left; // changed
    else {
    char szBuf[MAXLINELEN];
    CSize szFont=dc.GetTextExtent(&szBuf[nPos-1],1);
    return pt;

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date