在文件中存储的是树型结构的数据,
读出来后,我想在程序里保存到一个树型结构当中(不是树型控件)
这个树型结构应该怎样做

解决方案 »

  1.   

    这篇文章示范了如何将一个树结构加载到MFC的树控制中。该数据成员是动态加载的,而且整个树结构改变很容易。每个接点有一个当双击该接点时对应执行函数的指针,每个结点的结构如下: typedef struct tagItem
    {
    tagITEM * item;
    char * name;
    int (*func)();
    int image;
    int simage;
    } Item;
    item:树的其他分支的指针(如果没有为NULL) name:该结点显示在树控制中的字符串 func:在树控制上双击该结点是执行函数的指针 image:该结点使用的图象列表的索引 simage:该接点被选中时使用的图象列表的索引 使用例子:int substring1()
    {
    AfxMessageBox("substring1");
    return 0;
    }int substring2()
    {
    AfxMessageBox("substring2");
    return 0;
    }int string2()
    {
    AfxMessageBox("string2");
    return 0;
    }Item BRANCH1[] = 
    {
    {NULL, "SubString1", substring1, 0, 0},
    {NULL, "SubString2", substring2, 0, 0},
    {NULL, "", NULL, 0, 0}
    };Item root[] = 
    {
    {NULL, "String1", NULL, 0, 0}, // Root node with a branch on it
    {BRANCH1, NULL, NULL, 0, 0}, // Another Item array for the branch
    {NULL, "String2", string2, 1, 1}, // Single node.
    {NULL, "", NULL, 0, 0} // last element - needed for the loading function to tell when it's at the last node.
    };
    加载时,我使用的递归方法:void CTestView::InsertData(HTREEITEM parent, Item * item)
    {
    HTREEITEM newparent;while(item->name != "") // while we are not at the last member of the structure
    {
    if(item->item == NULL) // if this is a parent node
    {
    // Insert the item into the tree
    newparent = m_pTree->InsertItem(
    TVIF_IMAGE|TVIF_PARAM|TVIF_SELECTEDIMAGE|TVIF_STATE|TVIF_TEXT,
    item->name,
    item->image,
    item->simage,
    (item->func == NULL ? TVIS_BOLD : NULL),
    TVIS_BOLD,
    (LPARAM)item->func,
    parent,
    TVI_LAST);
    }
    else
    {
    InsertData(newparent, item->item); // call this function again with the new structure
    }item++; // go to the next member of the array
    }
    }