Delphi 拖拽的用法

1.如果把DragMode设置成dmAutoMatic 自动就会有拖拽效果。

2.如果把DragMode设置成dmManual,则需要调用BeginDrag才起作用,这个函数有两个参数(Immediate: Boolean; Threshold: Integer)

Immediate = true 则拖拽操作会立刻开始,鼠标变成dragCursor类型,Immediate = false 当达到Threshold设定的值时,会产生拖拽操作。

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Edit1: TEdit;

Label1: TLabel;

ListBox1: TListBox;

procedure Button1Click(Sender: TObject);

procedure Edit1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;

State: TDragState; var Accept: Boolean);

procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);

private

{ Private declarations }

public

{ Public declarations }

end;

TF1 = class(TForm1)

end;

var

Form1: TForm1;

implementation

uses Unit2, Unit3;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var

F2: TF1;

F3: TForm1;

begin

f3 := f2;

end;

procedure TForm1.Edit1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

if Button = mbleft then

Edit1.BeginDrag(False, 10);

end;

procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

if Button = mbleft then

Label1.BeginDrag(False, 10);

end;

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;

State: TDragState; var Accept: Boolean);

begin

if (Source = Label1) or (Source = Edit1) then

Accept := True;

end;

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);

begin

if (Sender = ListBox1) and (Source = Label1) then

begin

ListBox1.Items.Add(TLabel(Source).Caption);

end;

if (Sender = ListBox1) and (Source = Edit1) then

begin

ListBox1.Items.Add(TEdit(Source).Text + ' haha ');

end;

end;

end.