樹控件對應的類:
CTreeControl
添加樹節點:
1 HTREEITEM hRootNode = m_tvwTree.InsertItem(_T("人")); 2 HTREEITEM hNode = m_tvwTree.InsertItem(_T("老人"), hRootNode, TVI_LAST); 3 HTREEITEM hChildNode = m_tvwTree.InsertItem(_T("男人"), hNode, TVI_LAST); View CodeNM_CLICK消息處理函數:
1 void CTestTreecontrolDlg::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult) 2 { 3 CPoint PT; 4 ::GetCursorPos(&PT); 5 ::ScreenToClient(pNMHDR->hwndFrom, &PT); 6 7 UINT uFlags; 8 HTREEITEM hSelectedItem = m_tvwTree.HitTest(PT, &uFlags); 9 if(NULL == hSelectedItem) 10 { 11 return; 12 } 13 14 if(uFlags & TVHT_ONITEM) 15 { 16 CString str = m_tvwTree.GetItemText(hSelectedItem); 17 AfxMessageBox(str); 18 } 19 20 *pResult = 0; 21 } View CodeTVN_SELCHANGED消息處理函數:
1 void CTestTreecontrolDlg::OnTvnSelchangedTree1(NMHDR *pNMHDR, LRESULT *pResult) 2 { 3 LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR); 4 5 HTREEITEM hSelectedItem = m_tvwTree.GetSelectedItem(); 6 if(NULL == hSelectedItem) 7 { 8 return; 9 } 10 11 CString str = m_tvwTree.GetItemText(hSelectedItem); 12 if(str.IsEmpty()) 13 { 14 return; 15 } 16 17 AfxMessageBox(str); 18 19 *pResult = 0; 20 } View Code