这是在一个中拖动的例子,如果看明白了,两个也是一样的。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
DragItem, TargetItem:TListItem;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
TargetIndex: integer;
begin
DragItem := ListView1.Selected;
TargetIndex := ListView1.DropTarget.Index;
if TargetIndex>DragItem.Index then
TargetItem:=ListView1.Items.Insert(TargetIndex+1)
else TargetItem:=ListView1.Items.Insert(TargetIndex);
TargetItem.Assign(DragItem);
DragItem.Delete;
end;
procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if Source=ListView1 then accept :=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.DragMode := dmAutomatic;
ListView1.ViewStyle := vsList;
end;
end.