The three tree views, tvinsertstruct, tvitem, and nmtreeviewtvinsertstruct, contain the information used to add new items to the Tree View control. This structure is used by tvm_insertitem messages. This structure corresponds
TV _insertstructThe structure is the same, but it has been renamed according to the current naming conventions.
typedef struct tagTVINSERTSTRUCT { HTREEITEM hParent; HTREEITEM hInsertAfter;#if (_WIN32_IE >= 0x0400) union { TVITEMEX itemex; TVITEM item; } DUMMYUNIONNAME;#else TVITEM item;#endif} TVINSERTSTRUCT, FAR *LPTVINSERTSTRUCT;Member
-
Hparent
-
The handle of the parent item. If the value of this Member is tvi_root or null, it will be inserted as the root of the tree control.
-
Hinsertafter
-
The handle of the item after the new item is inserted. Or one of the following values:
| Value |
Meaning |
| Tvi_first |
Insert items at the beginning of the List |
| Tvi_last |
Insert an entry at the end of the list |
| Tvi_root |
Add as a root item |
| Tvi_sort |
Insert entries in alphabetical order |
-
Itemex
-
Version 4.71. Tvitemex contains information about the item addition.
-
Item
-
Tvitem contains information about the item addition.
Requirement
Windows NT/2000:Windows NT 3.51 or later is required.
Windows 95/98:Windows 95 or later is required.
Header:Defined in commctrl. h. Tvitem specifies or receives the attributes of the Tree View. This structure corresponds
TV _itemThe structure is the same, but it has been renamed by the current naming protocol. The new application should use this structure.
typedef struct tagTVITEM{ UINT mask; HTREEITEM hItem; UINT state; UINT stateMask; LPTSTR pszText; int cchTextMax; int iImage; int iSelectedImage; int cChildren; LPARAM lParam;} TVITEM, FAR *LPTVITEM;Member
-
Mask
-
Identifies the tag arrays containing valid data of other structure members. When this structure is used by tvm_getitem messages,
MaskThe Member indicates that the property of the item is retrieved. This member can be one or more of the following values.
| Tvif_children |
CChildrenThe Member is valid. |
| Tvif_di_setitem |
The Tree View control retains the supported information and does not request it again. This flag is valid when you process the tvn_getdispinf notification. |
| Tvif_handle |
HitemValid members. |
| Tvif_image |
IimageValid members. |
| Tvif_param |
LparamValid members. |
| Tvif_selectedimage |
IselectedimageValid members. |
| Tvif_state |
StateAndStatemaskValid members. |
| Tvif_text |
PsztextAndCchtextmaxValid members. |
-
Hitem
-
The item referenced by this function.
-
State
-
Position tag and image List Index settings, indicating the item status. When the status of an item is set,
StatemaskThe Member States that the bit of this Member is valid. When the status of an item is added, this member returns
StatemaskThe current status of the bit pointed by the Member.
The 0 to 7 digits of this member contain the status tag of the item. For more information about possible item status tags, see Tree View Control item states. overwrite the image on the item's icon image. The 8-11 bits of this Member specify a 1-based covered image index. If these bits are 0, this item does not overwrite the image. To isolate these bits, use the tvis_overlaymask mask. To overwrite the image index in this Member, use the indextooverlaymask macro. The overwritten image in the image list is set by the imagelist_setoverlayimage function. A status chart is displayed as an icon next only to an item indicating the state defined by the application. Sends a tvm_setimagelist message to specify a list of status images. To set the status image of an item
TvitemStructure
StatemaskThe member contains the tvis_stateimagemask value. Structure
StateIndex of the image to be drawn in the member's 12-15-bit specified state image list. To set the status image index, use indextostateimagemask. This macro sets an index to 12 to 15 characters. To specify that the item has no status image, set the index to 0. This means that image 0 in the status image list cannot be used as a status image. To isolate
StateUse the tvis_stateimagemask mask.
-
Statemask
-
StateThe member bit is valid. If you retrieve the status of an item, Set
StatemaskIndicates the position of a Member.
StateThe bit in the member is returned. If you set the status of an item, Set
StatemaskIndicates the position of a Member.
StateThe bit of the member you want to set. To set or retrieve the index of the overwrite image of an item, set the tvis_overlaymask bit. To set and retrieve the status image index of an item, set the tvis_stateimagemask bit.
-
Psztext
-
If this structure specifies the item attribute, the member points to a string ending with an empty character and contains the text of the item. If the value of this Member is lpstr_textcallback, the parent window is responsible for saving the name. In this case, when the Tree View control needs to display, save, or edit the item text, it sends a tvn_getdispinfo message to the parent window. When the item text changes, it sends a tvn_setdispinfo notification message.
If the structure is the property of the retrieved item, this member is the address of the retrieved item text buffer.
-
Cchtextmax
-
PsztextSpecifies the buffer size, in characters. If this structure is used to set item attributes, this member is ignored.
-
Iimage
-
When an item is not selected, it is the index of the image list of the tree control.
If the member is I _imagecallback, the parent window is responsible for saving the index. In this case, when the Tree View control needs to display this image, it sends a tvn_getdispinfo notification message to the parent window for indexing.
-
Iselectedimage
-
When an item is selected, it is the index of the tree control image list.
If the member is I _imagecallback, the parent window is responsible for saving the index. In this case, when the Tree View control needs to display this image, it sends a tvn_getdispinfo notification message to the parent window for indexing.
-
CChildren
-
Indicates which item has an associated subitem. This member can be one of the following values.
| Zero |
This item has no subitem. |
| One |
This item has one or more subitems. |
| I _childrencallback |
The parent window keeps track of whether the item has child items. In this case, when the Tree View control needs to display the item, the control sends the parent Tvn_getdispinfo Notification Message to determine whether the item has child items.If the Tree View control has Tvs_hasbuttons style, it Uses this member to determine whether to display the button indicating the presence of child items. you can use this member to force the control to display the button even though the item does not have any child items inserted. this allows you to display Button while minimizing the control's memory usage by inserting child items only when the item is visible or expanded. |
-
Lparam
-
The 32-bit value related to this parameter.
Yes
Windows NT/2000:Windows NT 3.51 or later is required.
Windows 95/98:Windows 95 or later is required.
Header:Defined in commctrl. h. Nmtreeview
Contains information about tree-based notification messages. This structure corresponds
Nm_treeviewThe structure is the same, but it has been renamed with the current naming rule.
typedef struct tagNMTREEVIEW { NMHDR hdr; UINT action; TVITEM itemOld; TVITEM itemNew; POINT ptDrag; } NMTREEVIEW, FAR *LPNMTREEVIEW; Member
-
HDR
-
The nmhdr structure contains information about the notification message.
-
Action
-
The action tag specified by the notification.
-
Itemold
-
The tvitem structure that contains information about the old item status. When a notification message is not used, the Member is 0.
-
Itemnew
-
The tvitem structure that contains new item status information. When a notification message is not used, the Member is 0.
-
Ptdrag
-
The point structure that contains the event information that causes the notification message to be sent.
For more information, see wm_notify ************************************** **************************************** **************************************** * *********** the tree control is used to construct a tree structure, there is a root node and there are many sub-nodes, and each sub-node can have one or more or no sub-nodes. In MFC, The ctreectrl class is used to encapsulate various operations of the tree control. By calling
Bool create (DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint NID );
Create a window. In dwstyle, you can use the following special style tree controls: tvs_haslines draw lines between parent and child nodes.
Tvs_linesatroot draw a line between the root and sub-nodes
Tvs_hasbuttons adds a button before each node to indicate whether the current node has been expanded.
The display characters of the tvs_editlabels node can be edited.
When tvs_showselalways loses focus, the selected node is also displayed.
Tvs_disabledragdrop does not allow drag/drop
Tvs_notooltips does not use tooltip to display node display characters. In the tree control, each node has a handle (htreeitem). When adding a node, the parameter must be the parent node handle of the node, (there is only one root node, which cannot be added or deleted)
Htreeitem insertitem (lpctstr lpszitem, htreeitem hparent = tvi_root, htreeitem hinsertafter = tvi_last );
You can add a node. pszitem is the display character. hparent represents the handle of the parent node. The currently added node is placed behind the node indicated by hinsertafter, And the return value is the handle of the currently created node. The following code creates a tree structure as follows: + --- parent1
+ --- Child1_1
+ --- Child1_2
+ --- Child1_3
+ --- Parent2
+ --- Parent3/* assume that m_tree is a ctreectrl object, and the window has been created */
Htreeitem hitem, hsubitem;
Hitem = m_tree.insertitem ("parent1", tvi_root); add parent1 to the root node
Hsubitem = m_tree.insertitem ("child1_1", hitem); // Add a subnode on parent1
Hsubitem = m_tree.insertitem ("child1_2", hitem, hsubitem); // Add a subnode on parent1, after child1_1
Hsubitem = m_tree.insertitem ("child1_3", hitem, hsubitem); hitem = m_tree.insertitem ("parent2", tvi_root, hitem );
Hitem = m_tree.insertitem ("parent3", tvi_root, hitem); if you want to add a small icon before each node, you must call
Cimagelist * setimagelist (cimagelist * pimagelist, int nimagelisttype );
Specifies the currently used imagelist. The nimagelisttype is tvsil_normal. After the call is complete, use the image in the control based on the image in the Set imagelist. Then call
Htreeitem insertitem (lpctstr lpszitem, int nimage, int nselectedimage, htreeitem hparent = tvi_root, htreeitem hinsertafter = tvi_last );
Add a node. nimage indicates the image sequence number used when the node is not selected, and nselectedimage indicates the image sequence number used when the node is selected. The following code demonstrates the settings of imagelist. /* M_list is a cimagelist object
Idb_tree is a 16 * (16*4) bitmap. Each image has 16*16 icons */
M_list.create (idb_tree, RGB (, 0 ));
M_tree.setimagelist (& m_list, tvsil_normal );
M_tree.insertitem ("parent1",); // Add. When selected, icon 1 is displayed. If not selected, icon 0 is displayed. In addition, ctreectrl provides some functions for obtaining/modifying the control status.
Htreeitem getselecteditem (); returns the handle of the currently selected node.
Bool selectitem (htreeitem hitem); the specified node is selected.
Bool getitemimage (htreeitem hitem, Int & nimage, Int & nselectedimage)/bool setitemimage.
Cstring getitemtext (htreeitem hitem)/bool setitemtext (htreeitem hitem, lpctstr lpszitem); used to get/modify the display characters of a node.
Bool deleteitem (htreeitem hitem); used to delete a node,
Bool deleteallitems (); All nodes are deleted. In addition, you can use the following functions to traverse the tree:
Htreeitem getrootitem (); get the root node.
Htreeitem getchilditem (htreeitem hitem); obtain the subnode.
Htreeitem getprevsiblingitem/getnextsiblingitem (htreeitem hitem); obtain the upper/next sibling node of the specified node.
Htreeitem getparentitem (htreeitem hitem); obtain the parent node. The message ing of the tree control uses the on_notify macro in the form of on_notify (wnotifycode, ID, memberfxn). wnotifycode is the notification code, ID is the window ID that generates the message, and memberfxn is the processing function, the function prototype is like void onxxxtree (nmhdr * pnmhdr, lresult * presult). Among them, pnmhdr is a data structure, which needs to be converted to other types of structures during use. The possible values and corresponding data structure of the tree control are as follows: tvn_selchanged is sent after the selected node changes. The structure used is nmtreeview.
Tvn_itemexpanded is sent after a node is expanded. Structure: nmtreeview
Tvn_beginlabeledit is sent when you start to edit node characters. Structure: nmtvdispinfo
Tvn_endlabeledit is sent when the Node Character is edited. The structure is nmtvdispinfo.
Tvn_getdispinfo is sent when you need to obtain information about a node. For example, the structure used is nmtvdispinfo.
There are many content about on_notify, which will be explained in detail in future. About dynamically providing the characters displayed by the node: You must specify the lpszitem parameter: lpstr_textcallback when adding the node. When the control displays the knot, the required characters are obtained by sending tvn_getdispinfo. when processing the message, the pnmhdr parameter is converted to lpnmtvdispinfo and then filled with item. psztext. But what do we know the information corresponding to this node? My approach is to set its lparam parameter after adding a node, then, when providing information, you can use this parameter to find the corresponding information. The following code illustrates this method: Char szout [8] [3] = {"No.1", "No. 2", "No. 3"}; // Add a node
Htreeitem hitem = m_tree.insertitem (lpstr_textcallback ,...)
M_tree.setitemdata (hitem, 0 );
Hitem = m_tree.insertitem (lpstr_textcallback ,...)
M_tree.setitemdata (hitem, 1 );
// Process the message
Void cparentwnd: ongetdispinfotree (nmhdr * pnmhdr, lresult * presult)
{
TV _dispinfo * ptvdi = (TV _dispinfo *) pnmhdr;
Ptvdi-> item. psztext = szout [ptvdi-> item. lparam]; // you can use lparam to obtain the position of the expected characters in the array.
* Presult = 0;
} For the display characters of the editing node, you must first set the tvs_editlabels style of the tree control. When editing, the control will send tvn_beginlabeledit, you can cancel the next edit by returning true to the processing function. After the edit, tvn_endlabeledit is sent. when processing the message, you need to convert the pnmhdr parameter to lpnmtvdispinfo, then use the item. psztext gets the edited character and resets the display character. If this variable is canceled during editing, It is null. The following code describes how to process these messages: // process the message tvn_beginlabeledit
Void cparentwnd: onbeginedittree (nmhdr * pnmhdr, lresult * presult)
{
TV _dispinfo * ptvdi = (TV _dispinfo *) pnmhdr;
If (ptvdi-> item. lparam = 0); // determines whether to cancel the operation.
* Presult = 0;
Else
* Presult = 1;
}
// Process the message tvn_beginlabeledit
Void cparentwnd: onbeginedittree (nmhdr * pnmhdr, lresult * presult)
{
TV _dispinfo * ptvdi = (TV _dispinfo *) pnmhdr;
If (ptvdi-> item. psztext! = NULL); // determines whether the edit has been canceled.
M_tree.setitemtext (ptvdi-> item. hitem, ptvdi-> psztext); // reset the display character
* Presult = 0;
} The message ing of the method described above must be performed in the parent window (all messages of wm_notify must be processed in the parent window ).