下面是最简单的例子,完全可以实现拖动上下移的效果,但如果还要实现超出范围时自动滚动
,可以参考上面的代码。
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.