Drag text between edit controls

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

CObject
|
+-CCmdTarget
  |
  +-CWnd
    |
    +-CEdit
      |
      +-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
	//{{AFX_DATA(CTestDragEditDlg)
	enum { IDD = IDD_DRAGEDIT };
	CDragEdit m_edit2; //declare a CDragEdit variable 
	CDragEdit m_edit1; //for each edit control
	//}}AFX_DATA
	...
}

//////////////////////
//TestDragEditDlg.cpp
//////////////////////
void CTestDragEditDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDragEditDlg)
	DDX_Control(pDX, IDC_EDIT2, m_edit2); //subclass the standard edit
	DDX_Control(pDX, IDC_EDIT1, m_edit1); //control as a CDragEdit
	//}}AFX_DATA_MAP
}

BOOL CTestDragEditDlg::OnInitDialog() 
{
	...
	    //You need to initiate CDragEdit first 
	m_edit1.Init();
	    m_edit2.Init();
	...
}

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.



Comments

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

    Posted by Legacy on 02/19/2004 12: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 !!

    Chris

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

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

    Originally posted by: Tony

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

    Reply
  • Property pages

    Posted by Legacy on 08/12/2002 12: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.
    

    Reply
  • 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 12: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.

    Thanks
    Ryan Henry

    Reply
  • How do I append a text in Edit control.

    Posted by Legacy on 03/21/2002 12: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.

    Krishnan.S.R.

    Reply
  • ExEditors Library

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

    Originally posted by: Mike Philips

    A complete collection of editors in a single file:
    
    

    http://www.exontrol.com/sg.jsp?content=products/exeditors

    Regards,
    Mike

    Reply
  • CListBox Drag and Drop

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

    Originally posted by: amit khera

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

    Reply
  • using hot key to copy.....

    Posted by Legacy on 09/08/2001 12: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...??

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

    Posted by Legacy on 05/11/2001 12: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.
    thanks
    sharon.

    Reply
  • Correct your PosFromChar Function

    Posted by Legacy on 03/23/2001 12: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;
    _CharToLinePos((int)uChar,&nLine,&nPos);
    //get dc and select current using font
    CClientDC dc(this);
    dc.SelectObject(GetFont());
    //get position of previous char
    pt=CDragEditBase::PosFromChar(uChar-1);
    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.y+=szFont.cy;
    pt.x=r.left; // changed
    }
    else {
    char szBuf[MAXLINELEN];
    GetLine(nLine,szBuf,sizeof(szBuf));
    CSize szFont=dc.GetTextExtent(&szBuf[nPos-1],1);
    pt.x+=szFont.cx;
    }
    dc.SelectStockObject(SYSTEM_FONT);
    }
    return pt;
    }

    Reply
  • 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.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds