( Delphi Ttreeview)如何将节点拖放到所选节点的下方(或上方)

eoxn13cs  于 2023-05-17  发布在  其他
关注(0)|答案(3)|浏览(176)

在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常量,它在目标节点后插入了一个同级节点。这里给出的TDodeAttachMode的常量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.

但没有一个是指新的最后一个兄弟姐妹
我能做我想做的吗?如果是,怎么做?

juud5qan

juud5qan1#

没有在节点之后插入的选项,只能在节点之前插入。所以你必须找到放置目标之后的节点,并在它之前插入。
要在最后一个节点之后添加,请找到最后一个节点并将naAdd传递给MoveTo

goucqfw6

goucqfw62#

感谢大卫的建议。使用它们,我最终编写了以下代码来实现我所需要的。贴在这里,以防对其他人有用。(这是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;
3b6akqbq

3b6akqbq3#

if (Dst <> nil)and(Src <> Dst) then
begin
 if Dst.getNextSibling <> nil then                                         
 begin
   varNode := Dst.GetNextChild(Dst);                                       
   Src.MoveTo(varNode, naInsert);                                          
 end
 else                                                                      
 begin
   varNode := Dst.Parent;                                                    
   Src.MoveTo(varNode, naAddChild);                                          
 end;
end;

相关问题