procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var p: TPoint; //上下交换位置 begin p.X := X; p.Y := Y; if ListBox1.ItemIndex <> -1 then begin si := ListBox1.ItemAtPos(p, true); if si = ListBox1.ItemIndex then t := true; end;end;
Shift: TShiftState; X, Y: Integer);
var
p: TPoint; //上下交换位置
begin
p.X := X;
p.Y := Y;
if ListBox1.ItemIndex <> -1 then begin
si := ListBox1.ItemAtPos(p, true);
if si = ListBox1.ItemIndex then
t := true;
end;end;
用 ListView1.GetItemAt(X,Y).Caption; 来获得当前鼠标所在行的数据,并存入TListItem变量
MOUSEMOVE事件
也是一样的,但存入另一个变量
MOUSEUP事件
将2变量的数据进行交换,可能要声明第3个TListItem 变量来进行中转
Left = 211
Top = 160
Width = 870
Height = 640
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ListView1: TListView
Left = 208
Top = 104
Width = 250
Height = 150
Columns = <
item
Caption = 'ID'
end
item
Caption = 'Title'
end>
DragMode = dmAutomatic
Items.Data = {
5F0000000300000000000000FFFFFFFFFFFFFFFF01000000000000000131046D
616E6500000000FFFFFFFFFFFFFFFF01000000000000000132046E616D650000
0000FFFFFFFFFFFFFFFF010000000000000001330474796875FFFFFFFFFFFF}
TabOrder = 0
ViewStyle = vsReport
OnDragDrop = ListView1DragDrop
OnDragOver = ListView1DragOver
end
endunit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := True ;
end;procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
SourceItem : TListItem;
TargetItem : TListItem;
TempItem : TListItem;
begin
SourceItem := Listview1.Selected;
TargetItem := ListView1.GetItemAt(X,Y);
if TargetItem <> nil then
begin
TempItem := Listview1.Items.Insert(TargetItem.Index+1);
TempItem.Assign(SourceItem);
SourceItem.Delete;
end;
end;end.
就要ccrun(老妖)所说的,我只是没有把ListView的RowSelect
设成true,所以不能行选,只能点在第一个列中拖动,但没有你说的那些情况阿