上移下移(100分)

  • 主题发起人 主题发起人 visual_cjiajia
  • 开始时间 开始时间
V

visual_cjiajia

Unregistered / Unconfirmed
GUEST, unregistred user!
打开DELPHI的控件 面板可以用鼠标拖动LISTBOX,LISTVIEW
的项目上移下移
请问如何作出BORLAND公司的这种效果?
 
设置DragMode等,请参考:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=0394609
 
不对啊,他们做的效果只是把两个ITEM的位置对调
根本不象BORLAND那样可以真正意义上的上移下移
 
procedure DragControl(WinControl: TWincontrol);
const SC_DRAGMOVE = $F012;
begin
ReleaseCapture;
WinControl.Perform(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
end;
以上就可以實現你想要的!




 
你看了其中Drag&Drop的部分了吗?
===============================================================
来自:dq, 时间:2000-11-24 15:46:00, ID:402853
-------------------
为了方便说明,假定不存在动态生成或删除的Item(动态的你可以自己根据实际情况做调整)

在ListView创建的时候为每一个Item指定排序索引:
procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
P: Pointer;
begin
for I := 0 to ListView1.Items.Count - 1 do begin
with ListView1.Items do begin
GetMem(P, SizeOf(Integer));
Data := P;
Integer(Data^) := Index;
end;
end;
end;

别忘了释放内存:
procedure TForm1.FormDestroy(Sender: TObject);
var
I: Integer;
begin
for I := 0 to ListView1.Items.Count - 1 do
with ListView1.Items do FreeMem(Data, SizeOf(Integer));
end;

下面是实现滚动:
procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
n: Integer;
T: TListItem;
begin
with ListView1 do begin
Accept := GetItemAt(X, Y) <> nil;
T := nil;
if Y <= 2 - Font.Height then begin
T := TopItem;
if T <> Items[0] then T := GetNextItem(T, sdAbove, [isNone]);
end;
if Y >= ClientHeight then begin
n := TopItem.Index + VisibleRowCount - 1;
if n < Items.Count then begin
T := Items[n];
T := GetNextItem(T, sdBelow, [isNone]);
end;
end;
if T <> nil then begin
T.MakeVisible(False);
Refresh;//可能是必须的,不然会留下痕迹
end;
end;
end;

重新排序在这里调用:
procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
n: Integer;
T1, T2: TListItem;
begin
T1 := ListView1.DropTarget;
if T1 = nil then Exit;
T2 := ListView1.Selected;
n := Integer(T1.Data^);
Integer(T1.Data^) := Integer(T2.Data^);
Integer(T2.Data^) := n;
ListView1.AlphaSort;//触发OnCompare事件
end;

排序的实现就很简单了:
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
begin
if (Item1.Data <> nil) and (Item2.Data <> nil) then
Compare := Integer(Item1.Data^) - Integer(Item2.Data^);
end;
 
下面是最简单的例子,完全可以实现拖动上下移的效果,但如果还要实现超出范围时自动滚动
,可以参考上面的代码。

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.
 
下面那段代码可以了,谢谢!!
但procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
n: Integer;
T1, T2: TListItem;
begin
T1 := ListView1.DropTarget;
if T1 = nil then Exit;
T2 := ListView1.Selected;
n := Integer(T1.Data^);
Integer(T1.Data^) := Integer(T2.Data^);
Integer(T2.Data^) := n;
ListView1.AlphaSort;//触发OnCompare事件
end;
中n := Integer(T1.Data^);这句在我这运行时出错,???
 
但procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
n: Integer;
T1, T2: TListItem;
begin
T1 := ListView1.DropTarget;
if T1 = nil then Exit;
T2 := ListView1.Selected;
n := Integer(T1.Data^);
Integer(T1.Data^) := Integer(T2.Data^);
Integer(T2.Data^) := n;
ListView1.AlphaSort;//触发OnCompare事件
end;
中的n := Integer(T1.Data^);有运行时错误
 
procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
P: Pointer;
begin
for I := 0 to ListView1.Items.Count - 1 do begin
with ListView1.Items do begin
GetMem(P, SizeOf(Integer));
Data := P;
Integer(Data^) := Index;
end;
end;
end;
加上这一段了?
 
谢谢你,不够还是用你后来那段最好,
有简单,有能达到目的!
 
后退
顶部