(Delphi Ttreeview) 如何drag/drop 节点低于(或高于)所选节点

(Delphi Ttreeview) how to drag/drop node to below (or above) selected node

在 TtreeView1.DragDrop 中使用

等语句
 targetnode := TreeView1.GetNodeAt(x,y);
 ...
 TreeView1.Selected.MoveTo(targetnode , naInsert ) ;

用鼠标移动一个节点,插入到ie上面,同层已有节点的前面。

我想改变行为,这样如果我向下拖动,节点就会移动到目标下方,但如果我向上拖动,它就会移动到目标上方(否则我可以拖动到新的底部位置但是不是新的顶部,反之亦然)。 我正在尝试的结构是

targetnode := TreeView1.GetNodeAt(x,y);
...
if DraggedItem.Index > targetnode.Index then //we are dragging upwards, insert before
    TreeViewStructure.Selected.MoveTo(targetnode , naInsert ) 
else                                        //we are dragging downwards, insert after
    TreeViewStructure.Selected.MoveTo(targetnode , ???) ;

但我找不到在目标节点 之后插入同级 的 TNodeAttachMode 常量。 此处给出的 TNodeAttachMode 常量 http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/ComCtrls_TNodeAttachMode.html 是 ...

naAdd: The new or relocated node becomes the last sibling of the other node.
naAddFirst: The new or relocated node becomes the first sibling of the other node.  
naInsert:The new or relocated node becomes the sibling immediately before the other node.  
naAddChild:The new or relocated node becomes the last child of the other node.  
naAddChildFirst:The new or relocated node becomes the first child of the other node.  

但其中 none 指的是新的最后一个兄弟姐妹。

我可以为所欲为吗?如果是怎么办?

没有在节点后插入的选项,只能在节点前插入。所以你必须找到放置目标之后的节点,并在它之前插入。

要在最后一个节点之后添加,找到最后一个节点并将naAdd传递给MoveTo

感谢大卫的建议。使用它们,我最终编写了以下代码来实现我所需要的。发布在这里以防其他人使用。 (这是 OnDragDrop 中的相关代码)

 if ( Assigned(targetnode))  //we are over a target node
    and (DraggedNode.Level = targetnode.Level  then  //we are dragging within the same sub level
         begin
         if targetnode.Index = targetnode.Parent.Count -1  then   //target is the last node so do an naAdd to drop node at the end
              TreeViewStructure.Selected.MoveTo(targetnode , naAdd )
         else
              TreeViewStructure.Selected.MoveTo(targetnode , naInsert )   //drop before target using naInsert
         end;