1 //Add button2 voidCmfcapplication8dlg::onbnclickedbuttonadd ()3 {4 //adding nodes to the tree5 CString StrText;6 GetDlgItemText (idc_edit1, strText);7 if(strtext.getlength () = =0) {8AfxMessageBox (TEXT ("Please enter text"));9 return;Ten } OneHtreeitem HItem = M_tree. GetSelectedItem ();//gets the currently selected node A if(HItem = =NULL) -HItem =Tvi_root; - theTvinsertstruct ts = {0 }; -Ts.hparent =HItem; -Ts.hinsertafter =Tvi_last; -Ts.item.pszText =Strtext.getbuffer (); +Ts.item.mask =Tvif_text; -Htreeitem Hnewitem = M_tree. InsertItem (&ts); +M_tree. SelectItem (Hnewitem);//Select the newly joined node AM_tree. EnsureVisible (Hnewitem);//The tree is too thought highly to see. at } - - //Delete button - voidCmfcapplication8dlg::onbnclickedbuttondel () - { - //gets the currently selected node inHtreeitem HItem = M_tree. GetSelectedItem ();//gets the currently selected node - if(HItem = =NULL) { toAfxMessageBox (TEXT ("Please select a node")); + return; - } theHtreeitem hparent =M_tree. GetParentItem (HItem); * M_tree. DeleteItem (HItem); $ M_tree. SelectItem (hparent);Panax Notoginseng } - the //Modify Button + voidcmfcapplication8dlg::onbnclickedbuttonupdate () A { the //gets the currently selected node +Htreeitem HItem = M_tree. GetSelectedItem ();//gets the currently selected node - if(HItem = =NULL) { $AfxMessageBox (TEXT ("Please select a node")); $ return; - } - CString StrText; the GetDlgItemText (idc_edit1, strText); - if(strtext.getlength () = =0) {WuyiAfxMessageBox (TEXT ("Please enter text")); the return; - } Wu M_tree. Setitemtext (HItem, strText); - } About $ //Select New Node event - voidCmfcapplication8dlg::ontvnselchangedtree1 (NMHDR *pnmhdr, LRESULT *PResult) - { -Lpnmtreeview Pnmtreeview = reinterpret_cast<lpnmtreeview>(PNMHDR); A //gets the currently selected node +Htreeitem HItem = M_tree. GetSelectedItem ();//gets the currently selected node the if(HItem! =NULL) { -CString StrText =M_tree. GetItemText (HItem); $Setdlgitemtext (Idc_edit1, StrText);//Select the node text to add to the edit box the } the the*presult =0; the}
MFC Getting Started Sample tree control (Ctreecontrol)