动态加载树结构到MFC的树控制

翻译|其它|编辑:郝浩|2005-05-25 09:08:00.000|阅读 1378 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


这篇文章示范了如何将一个树结构加载到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
}
}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP