Platform: Windows 95, Windows NT
Q: How do I drag and drop selected text inbetween memo components?
A: Without getting too deep into component creation, here's a very
simple way of accomplishing drag and drop of selected text.
Create a new component (TMyMemo) descending off of TMemo.
Make the type declaration look something like this:
type
TMyMemo = class(TMemo)
private
FLastSelStart : Integer;
FLastSelLength : Integer;
procedure WMLButtonDown(var Message: TWMLButtonDown);
message WM_LBUTTONDOWN;
published
property LastSelStart : Integer read FLastSelStart
write FLastSelStart;
property LastSelLength : Integer read FLastSelLength
write FLastSelLength;
end;
Make the implementation of WMLButtonDown look like this:
procedure TMyMemo.WMLButtonDown(var Message: TWMLButtonDown);
var
Ch : Integer;
begin
if SelLength > 0 then begin
Ch := LoWord(Perform(EM_CHARFROMPOS,0,
MakeLParam(Message.XPos,Message.YPos)));
LastSelStart := SelStart;
LastSelLength := SelLength;
if (Ch >= SelStart) and (Ch <= SelStart+SelLength-1) then
BeginDrag(True)
else
inherited;
end
else
inherited;
end;
Now, install this component into a package, start a brand new project
in Delphi 3 and drop two TMyMemos down.
Make them both have an OnDragOver event handler looking like this:
procedure TForm1.MyMemo1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := Source is TMyMemo;
end;
Make them both have an OnDragDrop event handler looking like this:
procedure TForm1.MyMemo1DragDrop(Sender, Source: TObject;
X, Y: Integer);
var
Dst, Src : TMyMemo;
Ch : Integer;
Temp : String;
begin
Dst := Sender as TMyMemo;
Src := Source as TMyMemo;
Ch := LoWord(Dst.Perform(EM_CHARFROMPOS,0,MakeLParam(X,Y)));
if (Src = Dst) and (Ch >= Src.LastSelStart) and
(Ch <= Src.LastSelStart+Src.LastSelLength-1) then
Exit;
Dst.Text := Copy(Dst.Text,1,Ch)+Src.SelText+
Copy(Dst.Text,Ch+1,Length(Dst.Text)-Ch);
Temp := Src.Text;
Delete(Temp,Src.LastSelStart+1,Src.LastSelLength);
Src.Text := Temp;
end;
Launch the app and put some text into the memos, and watch what
happens as you drag and drop inbetween the two memos, or even drag
and drop text from one location in one memo component to another
location in the same memo component.