Mirror

How to drag and drop text from a TRichEdit to other components (Views: 102)


Problem/Question/Abstract:

I would like to select text in a TRichEdit control, then drag and drop the text on another (non TRichEdit) component (ie. TEdit or TMemo). Simulating this behavior would be fine. The drag- related events are not firing when I drag text, so I assume the drag and drop behavior is embedded in the Windows control. But I don't see any drag-related messages in the Windows SDK online help.

Answer:

I've got a unit "uGenDragDrop", that implements IDropTarget (amongst others), and allows you to easily add OLE Drag and Drop support to any Delphi component (i.e. allow you to drag and drop not only within a Delphi application, but also in and out of Delphi applications).

Here is a snippet of code that implements OLE drop support for a TMemo on a form.

uses
  uGenDragDrop;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DTMemo := TDropTarget.Create(Memo1);
  DTMemo.AddFormat(CF_TEXT, [asComplete], [meGlobMemory]);
end;

procedure TForm1.Memo1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := TRUE;
end;

procedure TForm1.Memo1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  Memo1.Lines.Add((Source as TStorageMedium).GetText);
end;

<< Back to main page