保存位置可以通过用ListItem.Data属性进行排序实现。给Form加两个私有函数,<br>一个用来给各个ListItem.Data赋初值(SetData),在为ListView添加Item之后调用;<br>一个用来释放Data所占的内存(ClearData),在清除Items之前调用——<br><br>procedure TForm1.SetData;<br>var<br> I: Integer;<br> P: Pointer;<br>begin<br> for I := 0 to ListView1.Items.Count - 1 do begin<br> with ListView1.Items do begin<br> GetMem(P, SizeOf(Integer));<br> Data := P;<br> Integer(Data^) := Index;//初值为各ListItem的位置的序号<br> end;<br> end;<br>end;<br><br>procedure TForm1.ClearData;<br>var<br> I: Integer;<br>begin<br> for I := 0 to ListView1.Items.Count - 1 do<br> with ListView1.Items do FreeMem(Data, SizeOf(Integer));<br>end;<br><br>在ListView的OnCompare事件里进行排序——<br><br>procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;<br> Data: Integer; var Compare: Integer);<br>begin<br> if (Item1.Data <> nil) and (Item2.Data <> nil) then<br> Compare := Integer(Item1.Data^) - Integer(Item2.Data^);//按Data大小排序<br>end;<br><br>把ListView.DragMode设为dmAutomatic。然后在它的OnDragOver和OnDragDrop事件里分别写——<br><br>procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;<br> State: TDragState; var Accept: Boolean);<br>begin<br> Accept := Sender = Source;<br>end;<br><br>procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);<br>var<br> Index: Integer;<br> Item: TListItem;<br>begin<br> with ListView1 do begin<br> Item := GetItemAt(X, Y);<br> if (Item = nil) or (Selected = nil) then Exit;<br> Index := Integer(Item.Data^);<br> Integer(Item.Data^) := Integer(Selected.Data^);<br> Integer(Selected.Data^) := Index;<br> AlphaSort;//重新排序用以交换位置<br> end;<br>end;