(1) Tree control main feature control and Tree Control & View are mainly used to display data items with a hierarchical structure, such as disk directory in the resource manager, etc., for users to make various options. Each data item in the tree control includes a text string of the data item name and an image for representing the data item, and each data item can contain each subkey, and the entire structure is like a directory tree. For data items containing various sub-items, you can deliver or closely through the mouse, which can achieve different display forms of tree control by controlling the different styles of the tree. These styles include: tvs_haslines indicates that connects the parent and its subtries below, which can make the display hierarchy of the tree clearer, but there is no connection between the individual subtries without parents; TVS_LinesTroot Indicates that there is a connection between the subtries under the root without the parent; TVS_HASBUTTONS indicates a button with " " or "-" in front of the parent with children, which makes the user You can implement the subkey's expansion and closing by clicking this small button. When there is a child, the initial state of the button is " ". When the child is expanded, press the small button by " " to "-" Number, when the child is closed, the small button is "-" becomes " ", which is also invalid for roots. If you need to be implemented by combining the TVS_LinesTroot style; TVS_EDITLABELS means allowing users to click on input focus Data items to modify their name. For tree control, the MFC is also encapsulated in two forms, namely tree control (ctreectrl), and ctreeView, to meet the different needs of users. For general requirements, users are applied in the dialog box, using tree control comparison Convenient, for users with high requirements, there is also a variety of convenience characteristics of the view window when using the tree, can better meet the requirements of the document / view structure. When the tree is used in the window, the tree will be adjusted by the customer area full of two windows and automatically adjusts the adjustment of the frame structure of the window, and can have a variety of menus, accelerator keys, and toolbars. Command message. When using the tree, you can use its member function ctreeView to get one reference, you can apply as soon as tree control: CTreeCtrl & TreeCtrl = getTreeCtrl (). (2) Tree control Object structure 1, Tree control Establishment method CTreeCtrl & Treectr Create Tree Control Object Structure Create Creating Tree Control and Binding Object Tree Control CTREECTRL :: CREATE Call format as follows: Bool Create (DWord DWStyle, Const Rect, CWND * PPARENTWND, UINT NID); where parameter dWStyle is used to determine the type of tree control; RECT is used to determine the size and position of tree control; PParentWnd is used to determine the parent window controlled by the tree, and general is a dialog and cannot be null; NID is used to determine the identity of tree control.
The style of the tree control can be a combination of the following value: TVS_haslines indicates that the tree control is connected between the subtries; TVS_LINESATROOT indicates that the tree control is connected between the roots; TVS_HASBUTTONS indicates that the tree control is expanded on the left side of the parent. Close the control button; TVS_EDitlabels represents the name of the mouse click to modify the tree item; TVS_SHOWSELALWAYS indicates that the selected item remains selected even when the window lost the input focus; tvs_disabledragdrop indicates that the tree control sends a TVN_BEGINDRAG message 2, the property tree control of tree control The attribute class includes obtaining the number of items in the tree control, acquire the deviation value of the tree control relative to the parent, acquire the tree control image list control handle GetImageList, set the tree control image list control handle setImageList, get the next tree item GetNextItem, determined whether the given tree item contains children ItemHaschildren, gets the tree item getchildIndItem, get the next same tree item GetNextSiblingItem, get the previous same tree item getPrevsiblingItem, get the parent tree getParentItem, get the first visual tree item GetFirstvisibleItem, get the next visible tree item GetNextVisible Item, get the previous visual tree item getPrevvvisibleItem, get the selected tree item GetSelectedItem, get the root tree item GetrootItem, get the property GetItem, set the property setItem, set the property of the tree item, Take the status of the tree item, setting the status of the tree item, set the status of the tree item, get the GetItemImage associated with the tree item, set the image of the tree Items to set the image setItemImage, get the tree item text GetItemText, set the tree item text setItemtext and get the tree item editing control handle getEditControl, etc. . 3, the operation method of tree control, the operation method of tree control includes inserting a tree term INSERTITEM, deleting a tree item DeleteItem, deleting a child item expand, or closing a tree item, select a specific tree item SelectItem, select a tree As the first visible tree item SelectSetFirstvisible, edit a visual tree item EDitLabel and sort the sub-tree item for the parent tree item SortChildren, etc. (3) Tree control data structure needs to know two very important data structural TV_Item and TV_INSERTSTRUCT when using tree control, the previous data structure is used to represent tree-controlled tree item information, and the latter data structure is used to define Add the tree item to the data content required in the data control.
In addition, the three data structures of NM_TREEVIEW, TV_DISPINFO and TV_HITTESTINFO have been needed. The definition methods of these data structures are as follows: 1 Basic data item structure typef struct _tv_item {uint mask; // Structural member valid mask htreeItem HITEM; // data Item control handle uint state; // data item status uint statemask; // state valid mask bit LPSTM psztext; // data item name string int cchtextMax; // Data item name max IImage; // data item icon Index number int zelectedImage; // selection data item icon index INT cchildren; // child identifier LParam lparam; // program defined 32-bit data} TV_Item, FAR * LPTV_ITEM; 2 Insert Tree Item TYPEDEF STRUCT _TV_INSER TSTRUCT {HtreeItem HParent; // Parental Control Handle HtreeItem HinsetAfter; // Insert the position of the tree item TV_Item item; // Data item structure} TV_INSERTSTRUCT, FAR * LPTV_INSERTSTRUCT; where the insertion is inserted, it is inserted into tree control. The front or last, if it is TVi_sort, the inserted tree is automatically inserted into the appropriate location.
3 Tree Control Notification Message TypeDef struct _nm_treeView {nmHDR HDR; // Notification message handle uint action; // Notification message flag TV_Item item ItemOLD; / / The original data structure TV_Item itemnew; // New data structure Point PTDRAG; // Drag } 指} nm_treeview; 4 acquisition or set data structure typef struct _tv_dispinfo {tvdi nmHDR HDR; / / Notification message control handle TV_Item item; // data item structure} TV_DISPINFO; 5 pointer test data structure typedef struct _tvhittestinfo {TVHTST POINT PT; / / Customer area screen coordinate pointer uint flags; // stores the test results HTREEITEM HITEM; // Test data item structure} TV_HitTestinfo, FAR * lptv_hittestinfo; where the Flags test result can be as follows: TVHT_ABOVE in the customer area tvht_below at the customer The area below TVHT_NowHere in the customer area and at the last item below TVHT_ONITEM in the bitmap or tag of TVHT_ONITEMBUTTON in the bitmap or tag of tree items on the button associated with tree items on the bitmap associated with the tree item. Tv HT_ONITEMindent is associated with tree Online TVHT_ONITEMLABEL on the TVHT_ONITEMRIGHT on the right side of the tree item on the right side of the tree item on the right side of the TVHT_ONITEMSTATECI on the user-defined status icon on the right side of the customer area on the right side of the customer area (4) Tree control Application Skills Samples are still specifically presented by dialog-based demo instances to specifically introduce application skills of tree control and image list: through "File-> New-> Projects-> MFC AppWizard (EXE)" established a project called VCTree During the establishment process, choose the Dialog Based application; delete the default control in the dialog, set the Language Domain in all dialog properties to Chinese (PRC) to support the application to support Chinese; establish Two icons IDI_PM and IDI_CJ, used to represent the icon The neutralization is not selected, and the 32x32 and 16x16 sizes should be established for each icon to ensure the needs of the program; add a tree control object in the dialog window, and set five buttons "Add | Delete | View | Sort | Off ", its corresponding identifier is as follows: Control Name Title Name Logo Tree Control IDC_TREECTRL button Add Idc_ADD Delete IDC_DEL View iDOK 5, Select Tree Control Control, Select" View-> ClassWizard-> Memory Variables . 骺刂 艻 DC_TREECTRL introduces member variables, whose variable type is: Variable Name Variable Type M_TreeCtrl Control CTreeCtrl Simultaneously Use "Messages Map" to add control function functions for each command button.
6, then add the following control code in the code file VCTREEDLG.CPP: (1) Add image list to define CIMAGELIST CIL1, CIL2; // Size Map Number List (2) Add Code Bool CvcTreedlg in the initialization file :: OnNitDialog () {cdialog :: OnInitdialog (); ... // Other code // Todo: add extra initialization here // Started Add Code CVCTreeApp * PAPP = (CVCTreeApp *) AFXGetApp () ; // Create an image list Cil1.create (16, 16, ILC_COLOR, 2, 2); Cil1.Add (PAPP-> Loadicon); cil1.add (PAPP-> Loadicon); M_TreeCtrl. SetImageList (& Cil1, TVSIL_NORMAL); // set the image list DWORD dwStyles = GetWindowLong (m_TreeCtrl.m_hWnd, GWL_STYLE); // Get tree style original control dwStyles | = TVS_EDITLABELS | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT; SetWindowLong (m_TreeCtrl.m_hWnd, GWL_STYLE , DWSTYLES); // Setting style char * CJ [4] = {"Yuxi Cigarette Factory", "Yunnan Cigarette Factory", "Shenyang Cigarette Factory", "Chengdu Cigarette Factory"}; // Root Data Name Char * PM [ 4] [5] = {{"Hongmei Yi", "Red Mei Second", "Hongmei Three", "Hongmei Four", "Red Mei 5"}, // Product Data Item {"Bai Mei Yi", "Bai Mei Second", "Bai Mei Three", "Bai Mei Four", "Bai Mei 5"}, {"Green Mei Yi", "Green Mei 2", "Green Mei Three", "Green Mei Four", "Green Mei 5" "}, {" 青 梅 一 "," Qingmei Second "," Qingmei Three "," Qingmei Four "," Green Mei 5 "}}; INT I, J; HTREEITEM HROOT, HCUR; / / Tree Control Project Handle TV_INSERTSTRUCT TCITEM; // Insert data item data structure TCITEM.HPARENT = TVi_root; // Added root TCITEM.HINSERTAFTER = TVi_last; // After the last item TCITEM.Item.item .mask = tvif_text | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE; // Set mask TCITEM.Item.psztext = "Data Selection"; tcitem.Item.lparam = 0; // Sequence tcitem.Iimage = 0; // Normal Icon TCITEM .item.iselectedimage = 1; // Select the icon hroot = m_treectrl.insertitem (& tcitem); // Return the root clause for (i = 0; i <4; i ) {// increase each manufacturer tcitem.hparent = HROOT TCITEM.ITEM.PSZTEXT = CJ [I]; TCITEM.Item.lParam = (i 1) * 10; // child sequence hcur = m_treectrl.insertitem (& tcitem); for (j = 0; j <5; J
) {// increase each product tcitem.hparent = hcur; tcitem.Item.psztext = pm [i] [j]; tcitem.item.lparam = (i 1) * 10 (j 1); // Number M_TreeCtrl.insertitem (& Tcitem);} m_treectrl.expand (HCUR, TVE_EXPAND); // Expand Tree} m_treectrl.expand (Hroot, TVE_EXPAND); // Expand the previous tree Return True; // Return True Unless YOU SET The Focus to A Control} (3) Increase the implementation of tree features In addition to the tree item function, it is necessary to pay attention to the name of the new tree item, in addition to the data structure of the insertion tree item. For "new data", the user is increased to set the custom name to the data item. Pay special attention to m_treectrl.editlabel (Hinsert) when programming; there is no other program command, otherwise this editing instruction is invalid. Void cvctreedlg :: onadd () {// Add sub-function function htreeItem HSEL = m_treeCtrl.getSelectedItem (); // Get the selection of the option handle if (hsel == null) return; // None options Return static int NADDNO = 100; // Number greater than 100 for new data TV_INSERTSTRUCT TCITEM; // Define Insert Item TCITEM.HPARENT = HSEL; // Set the parent handle TCITEM.HINSERTAFTER = TVi_last; // In the final increase tcitem.tem.mask = TVIF_Text | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE; // Set mask Tcitem.Item.psztext = "Add data"; tcitem.Item.lparam = NADDNO ; // Index number increase tcitem.Iimage = 0; // Normal Icon TCITEM .Item.iselectedImage = 1; // Select the icon hinsert = m_treectrl.insertitem (& tcitem); // increase m_treectrl.expand (HSEL, TVE_EXPAND); m_treectrl.editlabel (Hinsert); // Modify Add Data} (4 Implementation of the deletion tree feature When the delete function is implemented, the tree item with the subkey is prompted to warn whether the user is deleted along with its child. Void cvctreedlg :: Ondel () {// Delete subkey function function htreeItem HSEL = m_treeCtrl.getSelectedItem (); // acquire the option handle; if (hsel == null) return; // None options Returns IF (M_TreeCtrl. ItemHaschildren (HSEL)) // Decision whether there is sub-IF (MessageBox ("manufacturer's name, delete it together?", "Warning", MB_YESNO) == IDNO) Return; m_treectrl.deleteItem (HSEL);} (5) The sorting function of the sorting function is to sort all the subtries of the selected tree items in characters. If you want to sort by other rules, you should use the sortchildrenitembc () function to make self-developing sorting programs, this self-developed function The function implemented in the list control is basically the same, and the read can be tested.