首页  编辑  

移动树节点

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

(*//

标题:移动树节点

说明:上、下、左、右移动

设计:Zswang

日期:2002-06-08

支持:wjhu111@21cn.com

//*)

///////Begin Source

function TreeNodeMove(mTreeNode: TTreeNode; mAnchorKind: TAnchorKind;

 mIsTry: Boolean = False): Boolean;

var

 vTreeNode: TTreeNode;

begin

 Result := Assigned(mTreeNode);

 if not Result then Exit;

 case mAnchorKind of

   akTop: begin

     vTreeNode := mTreeNode.GetPrev;

     while Assigned(vTreeNode) do begin

       if vTreeNode = mTreeNode.GetPrevSibling then begin

         if not mIsTry then mTreeNode.MoveTo(vTreeNode, naInsert);

         Exit;

       end else if (vTreeNode.Level = mTreeNode.Level) then begin

         if not mIsTry then mTreeNode.MoveTo(vTreeNode, naAdd);

         Exit;

       end else if (vTreeNode <> mTreeNode.Parent) and

         (vTreeNode.Level + 1 = mTreeNode.Level) then begin

         if not mIsTry then mTreeNode.MoveTo(vTreeNode, naAddChild);

         Exit;

       end;

       vTreeNode := vTreeNode.GetPrev;

     end;

   end;

   akBottom: begin

     vTreeNode := mTreeNode.GetNext;

     while Assigned(vTreeNode) do begin

       if vTreeNode = mTreeNode.GetNextSibling then begin

         if not mIsTry then vTreeNode.MoveTo(mTreeNode, naInsert);

         Exit;

       end else if (vTreeNode.Level = mTreeNode.Level) then begin

         if not mIsTry then mTreeNode.MoveTo(vTreeNode, naAddFirst);

         Exit;

       end else if vTreeNode.Level + 1 = mTreeNode.Level then begin

         if not mIsTry then mTreeNode.MoveTo(vTreeNode, naAddChildFirst);

         Exit;

       end;

       vTreeNode := vTreeNode.GetNext;

     end;

   end;

   akLeft: begin

     vTreeNode := mTreeNode.Parent;

     if Assigned(vTreeNode) then begin

       if not mIsTry then mTreeNode.MoveTo(vTreeNode, naInsert);

       Exit;

     end;

   end;

   akRight: begin

     vTreeNode := mTreeNode.GetNextSibling;

     if Assigned(vTreeNode) then begin

       if not mIsTry then mTreeNode.MoveTo(vTreeNode, naAddChildFirst);

       Exit;

     end;

   end;

 end;

 Result := False;

end; { TreeNodeMove }

///////End Source

///////Begin Demo

procedure TForm1.TreeView1KeyDown(Sender: TObject; var Key: Word;

 Shift: TShiftState);

begin

 if not (ssCtrl in Shift) then Exit;

 case Key of

   VK_UP: TreeNodeMove(TTreeView(Sender).Selected, akTop);

   VK_DOWN: TreeNodeMove(TTreeView(Sender).Selected, akBottom);

   VK_LEFT: TreeNodeMove(TTreeView(Sender).Selected, akLeft);

   VK_RIGHT: TreeNodeMove(TTreeView(Sender).Selected, akRight);

 end;

end;

procedure TForm1.TreeView1GetSelectedIndex(Sender: TObject;

 Node: TTreeNode);

begin

 CheckBox1.Checked := TreeNodeMove(TTreeView(Sender).Selected, akTop, True);

 CheckBox2.Checked := TreeNodeMove(TTreeView(Sender).Selected, akBottom, True);

 CheckBox3.Checked := TreeNodeMove(TTreeView(Sender).Selected, akLeft, True);

 CheckBox4.Checked := TreeNodeMove(TTreeView(Sender).Selected, akRight, True);

end;

///////End Demo