//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TravelAllChild(int &ChildCount, TTreeNode* pNode)
{
    ++ChildCount;
    if (pNode->HasChildren)
    {
        for (int i=0; i<pNode->Count; i++)
        {
            TravelAllChild(ChildCount, pNode->Item[i]);
        }
    }
}
//---------------------------------------------------------------------------
int __fastcall TForm1::GetChildCount(TTreeNode *pNode)
{
    int r=0;
    if (pNode)
    {
        TravelAllChild(r, pNode);
    }
    r--;
    return r;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FAddNode(int   Index,WideString      WStrName)
{
    int                 i,j,TreeCount;
    AnsiString          StrName;
    TTreeNode           *TNode;


    TNode = TreeView1->Items->AddChild(TreeView1->Items->Item[Index],WStrName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CopyExecute(TObject *Sender)
{
//複製

    //取得目前選取到的NODE
    Copy_Node = TreeView1->Selected;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PasteExecute(TObject *Sender)
{
//貼上
    TTreeNode           *tNode;
    TTreeNode           *fNode,*Item;
    int                 NodeCount;//底下總子NODE數
    int                 AddNum; //目前選的NODE值與來源NODE值的差




    //(來源)選取的NODE的子NODE總數
    NodeCount = GetChildCount(Copy_Node);


   //來源有子NODE時
    if (NodeCount > 0)
    {
        for (int i=0;i<NodeCount+1;i++)
        {

            tNode = TreeView1->Items->Item[i+Copy_Node->AbsoluteIndex];


            //兩者差 = 目的 - 來源
            AddNum = TreeView1->Selected->AbsoluteIndex-Copy_Node->AbsoluteIndex;

            FAddNode(AddNum+tNode->AbsoluteIndex,tNode->Text);

        }
    }
    else
    {

        FAddNode(TreeView1->Selected->AbsoluteIndex,Copy_Node->Text);
    }


    TreeView1->Selected->Expanded = true;
}
//---------------------------------------------------------------------------

