7

ノードのリストがあります。ドラッグ アンド ドロップで並べ替え機能を追加したいのですが、これを行う方法がわかりません。

TVirtualStringTree の OnDragDrop イベントを使用してみましたが、わかりませんでした。私はドキュメントを見ましたが、悲しいことに、単純なノードのドラッグドロップ用の最小限のサンプルコードはありません。

これは単一レベルのリストであることに注意してください。階層なし。:)

4

2 に答える 2

15

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また、でFalseに設定することを忘れないでくださいTreeOptions.AutoOptions

于 2011-12-13T10:56:59.350 に答える
2

複数のノードのドラッグ アンド ドロップ:

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;
于 2015-03-02T22:01:16.207 に答える