我有一个节点列表。我想添加一个拖放到重新排列的功能,但我不知道该怎么做。
我尝试使用TVirtualStringTree的OnDragDrop事件,但我无法理解。我查看了文档,遗憾的是没有用于普通节点拖放的最小示例代码。
请注意,它只是一个单级列表。没有层次。 :)
答案 0 :(得分:11)
如果您通过GetNodeData获取数据,则拖放操作可以像这样实现:
uses
ActiveX;
将拖动事件分配给树:
OnDragAllowed :
procedure TForm1.vt1DragAllowed(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
var Allowed: Boolean);
begin
Allowed := True;
end;
OnDragOver :
procedure TForm1.vt1DragOver(Sender: TBaseVirtualTree; Source: TObject; Shift: TShiftState;
State: TDragState; Pt: TPoint; Mode: TDropMode; var Effect: Integer; var Accept: Boolean);
begin
Accept := (Source = Sender);
end;
OnDragDrop :
procedure TForm1.vt1DragDrop(Sender: TBaseVirtualTree; Source: TObject; DataObject: IDataObject;
Formats: TFormatArray; Shift: TShiftState; Pt: TPoint; var Effect: Integer; Mode: TDropMode);
var
pSource, pTarget: PVirtualNode;
attMode: TVTNodeAttachMode;
begin
pSource := TVirtualStringTree(Source).FocusedNode;
pTarget := Sender.DropTargetNode;
case Mode of
dmNowhere: attMode := amNoWhere;
dmAbove: attMode := amInsertBefore;
dmOnNode, dmBelow: attMode := amInsertAfter;
end;
Sender.MoveTo(pSource, pTarget, attMode, False);
end;
另请不要忘记在toAutoDeleteMoveNodes
中将TreeOptions.AutoOptions
设置为False。
答案 1 :(得分:0)
多个节点拖放:
procedure TForm1.vst(Sender: TBaseVirtualTree;
Source: TObject; DataObject: IDataObject; Formats: TFormatArray;
Shift: TShiftState; Pt: TPoint; var Effect: Integer; Mode: TDropMode);
var
pSource, pTarget: PVirtualNode;
attMode: TVTNodeAttachMode;
List: TList<PVirtualNode>;
begin
pTarget := Sender.DropTargetNode;
case Sender.GetNodeLevel(pTarget) of
0:
case Mode of
dmNowhere:
attMode := amNoWhere;
else
attMode := amAddChildLast;
end;
1:
case Mode of
dmNowhere:
attMode := amNoWhere;
dmAbove:
attMode := amInsertBefore;
dmOnNode, dmBelow:
attMode := amInsertAfter;
end;
end;
List:= TList<PVirtualNode>.create();
pSource := Sender.GetFirstSelected();
while Assigned(pSource) do
begin
List.Add(pSource);
pSource := Sender.GetNextSelected(pSource);
end;
for pSource in List do
Sender.MoveTo(pSource, pTarget, attMode, False);
List.Free;
end;