如何在C ++ Builder中将VirtualTree记录从一个VT引用到另一个VT?

时间:2018-07-23 11:06:32

标签: c++builder virtualtreeview c++builder-xe5

有人可以帮助我在C ++ Builder中使用VirtualTree吗?

我有VT1,其中有记录:

image

还有VT2,里面有记录:

image

我将值从VT1复制到VT2

void __fastcall TForm3::CopyItemsFromVT1ToVT2(TVirtualStringTree *VT_List, PVirtualNode NodeList, TVirtualStringTree *VT_Tree, PVirtualNode NodeTree) {
   ((PTreeData)VT_Tree->GetNodeData(NodeTree))->Id=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Id;
   ((PTreeData)VT_Tree->GetNodeData(NodeTree))->Type=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Type;
   ((PTreeData)VT_Tree->GetNodeData(NodeTree))->Date=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Date;
   ((PTreeData)VT_Tree->GetNodeData(NodeTree))->Time=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Time;
   ((PTreeData)VT_Tree->GetNodeData(NodeTree))->Char_Code=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Char_Code;
   ((PTreeData)VT_Tree->GetNodeData(NodeTree))->Number_Code=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Number_Code;
   ((PTreeData)VT_Tree->GetNodeData(NodeTree))->Message=((TForm1::PTreeData)VT_List->GetNodeData(NodeList))->Message;
}

如何从VT1VT2引用记录?这是正确的吗?

((PTreeData)VT2->GetNodeData(ParentNodeTarget)) = ((PTreeData)VT1->GetNodeData(ParentNodeSource))

1 个答案:

答案 0 :(得分:0)

假设TreeDatastructclass且实现了operator=(而不是手动复制单个字段),则可以让编译器为您完成由编译器生成的默认值),例如:

struct TreeData
{
    ...
};
typedef TreeData *PTreeData;

...

VT1->NodeDataSize = sizeof(TreeData);
VT2->NodeDataSize = sizeof(TreeData);

...

void __fastcall TForm3::CopyItemsFromVT1ToVT2(TVirtualStringTree *VT_List, PVirtualNode NodeList, TVirtualStringTree *VT_Tree, PVirtualNode NodeTree)
{
    *static_cast<PTreeData>(VT_Tree->GetNodeData(NodeTree)) = *static_cast<PTreeData>(VT_List->GetNodeData(NodeList));
}

但是,如果您想让VT2个节点简单地指向与VT1个节点相同的数据而不进行复制,那么您需要在任何节点之外分配TreeData个实例,然后将TreeData*指针存储在节点内部,例如:

struct TreeData
{
    ...
};
typedef TreeData *PTreeData, **PPTreeData;

...

VT1->NodeDataSize = sizeof(PTreeData);
VT2->NodeDataSize = sizeof(PTreeData);

...

PTreeData data = new TreeData;
// initialize data as needed ...
*static_cast<PPTreeData>(VT1->GetNodeData(SomeNode)) = data;

...

// OnFreeNode event handler for VT1...
void __fastcall TForm3::VT1FreeNode(TBaseVirtualTree *Sender, PVirtualNode Node)
{
    delete *static_cast<PPTreeData>(Sender->GetNodeData(Node));
}

...

void __fastcall TForm3::CopyItemsFromVT1ToVT2(TVirtualStringTree *VT_List, PVirtualNode NodeList, TVirtualStringTree *VT_Tree, PVirtualNode NodeTree)
{
    *static_cast<PPTreeData>(VT_Tree->GetNodeData(NodeTree)) = *static_cast<PPTreeData>(VT_List->GetNodeData(NodeList));
}