VC++中怎样在指定的TreeCtrl节点中添加子节点?
比如有下面结构的TreeCtrl:|+----水果+----五谷想在水果节点下添加“香蕉”子节点,在VC++中怎样编程?...
比如有下面结构的TreeCtrl:|+----水果+----五谷想在水果节点下添加“香蕉”子节点,在VC++中怎样编程?
展开
2个回答
2013-08-17
展开全部
CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1);
ASSERT(pCtrl != NULL);
// Insert a root item using the structure. We must
// initialize a TVINSERTSTRUCT structure and pass its
// address to the call.
TVINSERTSTRUCT tvInsert; // 这是定义了一个父节点
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("United States");
HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert); // 插入父节点,记住前面这个hCountry 下面又把它作为了父节点
HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT,
_T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL); //这里插入一个hCountry 的子节点
HTREEITEM hWA = pCtrl->InsertItem(_T("Washington"), // 这里又插入一个hCountry 的子节点并且放在hPA的后面(是铅迟同一个辈分的)
0, 0, hCountry, hPA);
pCtrl->InsertItem(_T("Pittsburgh"), hPA, TVI_SORT); // 这里连续插入了三个hPA的子节点
pCtrl->InsertItem(_T("Harrisburg"), hPA, TVI_SORT);
pCtrl->InsertItem(_T("Altoona"), hPA, TVI_SORT);
pCtrl->InsertItem(_T("Seattle"), hWA, TVI_SORT); //这里插入了3个hWA的子节点
pCtrl->InsertItem(_T("Kalaloch"), hWA, TVI_SORT);
pCtrl->InsertItem(_T("Yakima"), hWA, TVI_SORT); 根据上面的方法,你可以插入迟孝自码激稿己的节点了下面给你函数原型看看 HTREEITEM InsertItem( LPTVINSERTSTRUCT lpInsertStruct );HTREEITEM InsertItem( UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter );HTREEITEM InsertItem( LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST );HTREEITEM InsertItem( LPCTSTR lpszItem, int nImage, int nSelectedImage, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST);
ASSERT(pCtrl != NULL);
// Insert a root item using the structure. We must
// initialize a TVINSERTSTRUCT structure and pass its
// address to the call.
TVINSERTSTRUCT tvInsert; // 这是定义了一个父节点
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("United States");
HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert); // 插入父节点,记住前面这个hCountry 下面又把它作为了父节点
HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT,
_T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL); //这里插入一个hCountry 的子节点
HTREEITEM hWA = pCtrl->InsertItem(_T("Washington"), // 这里又插入一个hCountry 的子节点并且放在hPA的后面(是铅迟同一个辈分的)
0, 0, hCountry, hPA);
pCtrl->InsertItem(_T("Pittsburgh"), hPA, TVI_SORT); // 这里连续插入了三个hPA的子节点
pCtrl->InsertItem(_T("Harrisburg"), hPA, TVI_SORT);
pCtrl->InsertItem(_T("Altoona"), hPA, TVI_SORT);
pCtrl->InsertItem(_T("Seattle"), hWA, TVI_SORT); //这里插入了3个hWA的子节点
pCtrl->InsertItem(_T("Kalaloch"), hWA, TVI_SORT);
pCtrl->InsertItem(_T("Yakima"), hWA, TVI_SORT); 根据上面的方法,你可以插入迟孝自码激稿己的节点了下面给你函数原型看看 HTREEITEM InsertItem( LPTVINSERTSTRUCT lpInsertStruct );HTREEITEM InsertItem( UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter );HTREEITEM InsertItem( LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST );HTREEITEM InsertItem( LPCTSTR lpszItem, int nImage, int nSelectedImage, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST);
2016-04-22 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
HTREEITEM InsertItem( LPCTSTR lpszItem,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST );
HTREEITEM InsertItem( LPCTSTR lpszItem,
int nImage,
int nSelectedImage,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST);
这两个函数都可以使用。区别是一个有图标一个没图标
最后一个参搭陵穗数可以不使用。
主要是hParent 要填对就OK了。知卜
这是我汪贺用过的一个初始化函数,供参考
void CTree::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
m_pTree = &GetTreeCtrl();
m_pTree->ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
m_pImageList = new CImageList(); // 树子节点的图标
CWinApp* pApp=AfxGetApp();
m_pImageList->Create(32,32,ILC_COLOR8|ILC_MASK,3,3);
m_pImageList->Add(pApp->LoadIcon(IDI_ComputerOff));
m_pImageList->Add(pApp->LoadIcon(IDI_ComputerOn));
m_pImageList->Add(pApp->LoadIcon(IDI_Video));
m_pTree->SetImageList(m_pImageList, TVSIL_NORMAL);
m_hRoot = m_pTree->InsertItem(_T("IPTV"),0,0);
m_pTree->SetItemImage(m_hRoot,1,0);
m_hCh1=m_pTree->InsertItem(_T("Channel1"),0,0,m_hRoot);
m_pTree->SetItemImage(m_hCh1,2,2);
m_hCh2=m_pTree->InsertItem(_T("Channel2"),0,0,m_hRoot);
m_pTree->SetItemImage(m_hCh2,2,2);
m_hCh3=m_pTree->InsertItem(_T("Channel3"),0,0,m_hRoot);
m_pTree->SetItemImage(m_hCh3,2,2);
}
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST );
HTREEITEM InsertItem( LPCTSTR lpszItem,
int nImage,
int nSelectedImage,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST);
这两个函数都可以使用。区别是一个有图标一个没图标
最后一个参搭陵穗数可以不使用。
主要是hParent 要填对就OK了。知卜
这是我汪贺用过的一个初始化函数,供参考
void CTree::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
m_pTree = &GetTreeCtrl();
m_pTree->ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
m_pImageList = new CImageList(); // 树子节点的图标
CWinApp* pApp=AfxGetApp();
m_pImageList->Create(32,32,ILC_COLOR8|ILC_MASK,3,3);
m_pImageList->Add(pApp->LoadIcon(IDI_ComputerOff));
m_pImageList->Add(pApp->LoadIcon(IDI_ComputerOn));
m_pImageList->Add(pApp->LoadIcon(IDI_Video));
m_pTree->SetImageList(m_pImageList, TVSIL_NORMAL);
m_hRoot = m_pTree->InsertItem(_T("IPTV"),0,0);
m_pTree->SetItemImage(m_hRoot,1,0);
m_hCh1=m_pTree->InsertItem(_T("Channel1"),0,0,m_hRoot);
m_pTree->SetItemImage(m_hCh1,2,2);
m_hCh2=m_pTree->InsertItem(_T("Channel2"),0,0,m_hRoot);
m_pTree->SetItemImage(m_hCh2,2,2);
m_hCh3=m_pTree->InsertItem(_T("Channel3"),0,0,m_hRoot);
m_pTree->SetItemImage(m_hCh3,2,2);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询