Function: Create a tree control and double-click a node to respond to events.
Response Method: Add a double-click RESPONSE event on the tree control.
Code:
1. in oninitdialog: Counter * counter = (ctreectrl *) getdlgitem (idc_tree1); // call the modifystyle method to modify the sytle ctreectrl-> modifystyle (0, tvs_haslines | counter ); // set the tree control font color ctreectrl-> settextcolor (RGB (255, 0,255); // set the tree control background color ctreectrl-> setbkcolor (RGB (, 0 )); // Add the root node root htreeitem root = ctreectrl-> insertitem (_ T ("root"); ctreectrl-> setitemdata (root, 1 ); // Add parent nodes parent1 and parent2 htreeitem parent1 = ctreectrl-> insertitem (_ T ("parent1"); ctreectrl-> setitemdata (parent1, 2 ); htreeitem parent2 = ctreectrl-> insertitem (_ T ("parent2"); ctreectrl-> setitemdata (parent2, 3 ); // Add the root node rootchild htreeitem rootchild = ctreectrl-> insertitem (_ T ("rootchild"), root); ctreectrl-> setitemdata (rootchild, 4 ); // Add the child node htreeitem Child1 = ctreectrl-> insertitem (_ T ("Child1"), parent1); ctreectrl-> setitemdata (Child1, 5) of the parent node parent1 ); htreeitem child2 = ctreectrl-> insertitem (_ T ("child2"), parent1, Child1); ctreectrl-> setitemdata (child2, 6 ); htreeitem Child3 = ctreectrl-> insertitem (_ T ("Child3"), parent1, child2); ctreectrl-> setitemdata (Child3, 7 ); // Add the child node htreeitem child11 = ctreectrl-> insertitem (_ T ("child11"), parent2); ctreectrl-> setitemdata (child11, 8) of the parent node parent2 ); htreeitem child22 = ctreectrl-> insertitem (_ T ("child22"), parent2, child11); ctreectrl-> setitemdata (child22, 9 ); htreeitem child33 = ctreectrl-> insertitem (_ T ("child33"), parent2, child22); ctreectrl-> setitemdata (child33, 10 );
2. use classwizard to add a tree control and double-click the RESPONSE event: void cctreectrldemo2dlg: ondblclktree1 (nmhdr * pnmhdr, lresult * presult) {// todo: add your control notification handler code here ctreectrl * ctreectrl = (ctreectrl *) getdlgitem (idc_tree1); // Step 4: add the cursor to the ondclicktree function and click the message response code. // obtain the coordinate point cpoint Pt = getcurrentmessage ()-> pt of the current mouse click message. // set the mouse's screen coordinate, ctreectrl-> screentoclient (& pt); uint uflags = 0; htreeitem hitem = CT Reectrl-> hittest (PT, & uflags); // click to test the node, to obtain the tree node if (hitem! = NULL) & (tvht_onitem & uflags) // If the clicked position is in the node position {ctreectrl-> selectitem (hitem); // obtain the data set to start, note that this is why I want to set different data for each node. // Of course, if you have fewer data nodes, you can also obtain the node text, such as "root, parent1, parent2 ······", // then, the string comparison is used to determine which node is clicked: int ndat = ctreectrl-> getitemdata (hitem); // then, based on different nodes, after you complete different actions, you can switch (ndat) {Case 1: MessageBox ("root"); break; Case 2: MessageBox ("parent1"); break; Case 3: messageBox ("parent2"); break; Case 4: MessageBox ("rootchild"); break; Case 5: MessageBox ("Child1"); break; Case 6: messageBox ("child2"); break; Case 7: MessageBox ("Child3"); break; case 8: MessageBox ("child11"); break; Case 9: messageBox ("child22"); break; case 10: MessageBox ("child33"); break; default: MessageBox ("error");} * presult = 0 ;}}
This article is from the "whatever957" blog, please be sure to keep this source http://whatever957.blog.51cto.com/6835003/1565073
Tree control ctreectrl creation and double-click response