如果要用控件的话tlmdmemo就可以了,我看了半天源码,由于它是从自己的类中派生
出来的,所以没有什么利用的价值,要用就直接用吧。
不用的话,下面有些简单的代码,不太完善,不过100分我看还是值的
unit drag;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure Memo1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Memo1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure Memo1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
CanMove:Boolean=False;
CurrentStart,CurrentLength:Integer;
OnSelect:Boolean=False;
MousePD:Boolean=False;
implementation
{$R *.DFM}
procedure TForm1.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var Row,Col,i,j:Integer;
Str:String;
begin
if CanMove then begin
screen.Cursor:=crArrow;
end else
screen.cursor:=crDefault;
if not OnSelect then begin
if not MousePD then begin
j:=0;
Row:=1;
Str:=Copy(Memo1.Lines.Text,0,Memo1.SelStart);
while not (Pos(#13#10,Str)=0) do begin
str:=copy(str,Pos(#13#10,Str)+2,Memo1.SelStart-Pos(#13#10,Str));
Row:=Row+1;
end;
Col:=Length(Str);
Str:=Memo1.SelText;
while not (Pos(#13#10,Str)=0) do begin
delete(Str,1,Pos(#13#10,Str)+1);
inc(j);
end;
i:=SendMessage(TMemo(Sender).Handle,EM_CHARFROMPOS,0,MakeLParam(x,y));
CanMove:=(i>=(row-1)*65538-2*row+2+(col mod 2)+Memo1.SelStart)and(i<=(row+j-1)*65538+Memo1.SelStart+Memo1.SelLength-row-j);
end else begin
if CanMove then begin
screen.Cursor:=crDrag;
SetCaretPos(x-2,y-2);//???
end else
screen.cursor:=crDefault;
end;
end;
end;
procedure TForm1.Memo1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if CanMove then begin
Memo1.SelStart:=CurrentStart;
Memo1.SelLength:=CurrentLength;
MousePD:=True;
Abort;
end else begin
OnSelect:=True;
end;
end;
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
CurrentStart:=TMemo(Sender).SelStart;
CurrentLength:=TMemo(Sender).SelLength;
end;
procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if MousePD and CanMove then begin
Memo1.Perform(EM_REPLACESEL,0,0);
// 此处有些问题,需移动光标到此,但setcaretpos不行!
// Memo1.ScreenToClient(Memo1.ClientToScreen(point(x,y)));// Perform(EM_SETSEL,10,10);
// Memo1.PasteFromClipboard;
end;
OnSelect:=False;
MousePD:=False;
CurrentStart:=TMemo(Sender).SelStart;
CurrentLength:=TMemo(Sender).SelLength;
end;
end.