CTreeCtrl m_treeCtrl; // Declare CTreeCtrl object // Create tree control window with styles WS_VISIBLE and WS_CHILD m_treeCtrl.Create(WS_VISIBLE | WS_CHILD, CRect(0, 0, 100, 100), this, 1); // Add root item to tree control with caption "Root" HTREEITEM hRoot = m_treeCtrl.InsertItem("Root"); // Add child items to root item with captions "Child 1" and "Child 2" HTREEITEM hChild1 = m_treeCtrl.InsertItem("Child 1", hRoot); HTREEITEM hChild2 = m_treeCtrl.InsertItem("Child 2", hRoot);
// Declare member function to handle tree control selection changing event afx_msg void OnTvnSelchangingTreeCtrl(NMHDR* pNMHDR, LRESULT* pResult); BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx) ON_NOTIFY(TVN_SELCHANGING, IDC_TREE_CTRL, OnTvnSelchangingTreeCtrl) END_MESSAGE_MAP() // Implement member function to handle tree control selection changing event void CMyDialog::OnTvnSelchangingTreeCtrl(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; HTREEITEM hSelectedItem = m_treeCtrl.GetSelectedItem(); // Do something with selected item ... *pResult = 0; }Package library: CTreeCtrl is a class in MFC (Microsoft Foundation Classes) library, which is part of the Microsoft Visual Studio IDE.