VS2010/MFC Programming (Common Controls: Tree controls Create h and instances)

Source: Internet
Author: User
The previous section describes the tree control's introduction, notification messages, and related data structures, and this section continues with the lower half, including the creation of tree controls, the main member functions of the CTreeCtrl class, and application examples.

Creating a Tree control

MFC provides a CTreeCtrl class for a tree control that encapsulates all the operations of a tree control.

There are two ways to create a tree control, either by dragging it directly into a tree control in a dialog template or by creating a CTreeCtrl class's Create member function. The following is mainly about the latter.

The CTreeCtrl class's create member function is prototyped as follows:

Virtual BOOL Create (   DWORD dwstyle,   const rect& RECT,   cwnd* pparentwnd,   UINT NID);

The prototype of this function is similar to the CREATE function of all the control classes mentioned earlier. DWSTYLE specifies a combination of tree control styles, rect specifies the position and size of the tree control window, pParentWnd a pointer to the parent window of the tree control, and NID specifies the ID of the tree control. The following is mainly about the main style and meaning of the tree control.

Tvs_disabledragdrop: Disable the tree control from sending the TVN_BEGINDRAG notification message, that is, the drag operation is not supported

Tvs_editlabels: User can edit the label text of a node

Tvs_hasbuttons: Displays a small box with a "+" or "-" to indicate whether an item can be expanded or expanded

Tvs_haslines: Lines between parent and child nodes to show the structure of the tree more clearly

Tvs_linesatroot: Connecting at the root node

Tvs_showselalways: Displays the selection status of items even if the control loses input focus

Similarly, when you dynamically create a tree control, you typically specify Ws_child and ws_visible styles in addition to the combination of the styles described above.

When you drag a tree control directly into a dialog box template, you can set its style in the tree control's property page, which corresponds to the style above, for example, the property has lines corresponds to the tvs_haslines style.

Main member functions of the CTreeCtrl class

cimagelist* setimagelist (CImageList * pimagelist,int nimagelisttype);

If the tree node needs to display an icon, you must first create an object of the CImageList class and add multiple images to it to form an image sequence, and then call the SetImageList function to set the image sequence for the tree control. When inserting a node with InsertItem, the index of the desired image in the image sequence is passed in. This is demonstrated in the following example. The parameter pimagelist is a pointer to an object cimagelist the image sequence class and, if NULL, removes all images of the tree control. The parameter nimagelisttype specifies the type of image sequence, either tvsil_normal (normal image sequence) or tvsil_state (state image sequence, which represents the state of the node with an image).

UINT GetCount () const;

Gets the number of nodes in the tree control.

Dword_ptr GetItemData (Htreeitem hItem) const;

Gets the additional 32-bit data for a specified node in a tree control. The Hitem parameter is a handle to the specified tree node.

BOOL setitemdata (Htreeitem hitem,dword_ptr dwdata);

Sets the additional 32-bit data for a specified node in a tree control. Parameter Hitem, Dwdata is the 32-bit data to be set.

CString GetItemText (Htreeitem hItem) const;

Gets the label text for a specified node in a tree control. Parameter Hitem Ibid. The return value is a string that contains the text of the label.

BOOL Setitemtext (Htreeitem hitem,lpctstr lpszitem);

Sets the label text for a specified node in a tree control. Parameter Hitem, Lpszitem is a pointer to a string containing the label text.

Htreeitem GetNextSiblingItem (Htreeitem hItem) const;

Gets the next sibling node of a specified node in a tree control. Parameter Hitem Ibid. The return value is the handle to the next sibling node.

Htreeitem GetPrevSiblingItem (Htreeitem hItem) const;

Gets the previous sibling node of a specified node in a tree control. Parameter Hitem Ibid. The return value is a handle to the previous sibling node.

Htreeitem GetParentItem (Htreeitem hItem) const;

Gets the parent node of a specified node in a tree control. Parameter Hitem Ibid. The return value is the handle to the parent node.

Htreeitem GetRootItem () const;

Gets the handle to the root node of the tree control.

Htreeitem GetSelectedItem () const;

Gets the handle of the currently selected node of the tree control.

BOOL Deleteallitems ();

Deletes all nodes in the tree control. The delete succeeds returns true, otherwise false is returned.

BOOL DeleteItem (Htreeitem hItem);

Deletes a node in a tree control. The parameter hitem is the handle to the node to be deleted. The delete succeeds returns true, otherwise false is returned.

Htreeitem InsertItem (lpctstr lpszitem,int nimage,int nselectedimage,htreeitem hparent = TVI_ROOT,HTREEITEM Hinsertafter = Tvi_last);

Inserts a new node in the tree control. The parameter lpszitem is a pointer to the new node's label text string, the parameter nimage the index of the new node's icon in the tree control image sequence, and the parameter nselectedimage the index of the icon in the image sequence when the new node is selected, The parameter hparent is the handle to the parent node of the insertion node, and the parameter hinsertafter the handle of the previous node of the new node, that is, the new node is inserted after the Hinsertafter node.

BOOL SelectItem (Htreeitem hItem);

Selects the specified tree node. The parameter hitem is the handle to the node to select. Returns true if successful, otherwise false.

Application examples of tree controls

Finally, we will write a simple example to illustrate the use of several member functions and tree control notification messages for the CListCtrl class.

This instance implements the ability to display a simple hierarchical hierarchy of sites in a tree control, with a total of three layers, each of which is a Web site, individual categories, and articles. When you change the selected node with the left mouse button, the text of the selected node is displayed in the edit box. In addition, to achieve a common effect, is the mouse across the root node outside of a tree node, display the corresponding tip tip information. Here are the specific implementation steps:

1. Create a dialog-based MFC project with the name set to "Example31".

2. In the automatically generated dialog template Idd_example31_dialog, delete "Todo:place dialog controls here." The static text box, the OK button, and the Cancel button. Adding a TREE control control with ID set to Idc_web_tree, property has Buttons, have lines, and lines at root are set to true, in order to display a hint when the mouse is over a node, the info Set the Tip property to True. Add a static text box and an edit box, the Caption property of the static text box is set to the node you selected:, and the edit box has the ID set to Idc_item_sel_edit,read only property set to True. The dialog template at this point is as follows:

3. Import the icon that you want to add to the node for the tree control. Here you find three 32x32 icon icons, saved to the project Res directory. Then in the Resource View Explorer, right-click on the Icon node, select "Add Resource ..." In the right-click menu, Pop Up the "Add Resource" dialog and select "icon" from the "Resource type" list on the left. Click on the "Import ..." button on the right to select the three icon files to import. After the import succeeds, modify their IDs to Idi_web_icon, Idi_catalog_icon, and Idi_article_icon, respectively.

4. Add the CTreeCtrl type control variable m_webtree for the tree control Idc_web_tree. and add member objects to the Cexample31dlg class in the Example31Dlg.h file: CImageList m_imagelist;.

5. When the dialog is initialized, we add the tree structure of the site to the tree control, then we need to modify the Cexample31dlg::oninitdialog () function to:

BOOL Cexample31dlg::oninitdialog () {cdialogex::oninitdialog (); ......   //Todo:add extra initialization here Hicon hicon[3];   An array of icon handles Htreeitem hroot; The handle of the root node of the tree Htreeitem Hcataitem; Can represent the handle of any classification node Htreeitem Hartitem;   Can represent a handle to any article node//load three icons and save their handles to the array hicon[0] = Theapp.loadicon (Idi_web_icon);   HICON[1] = Theapp.loadicon (Idi_catalog_icon);     HICON[2] = Theapp.loadicon (Idi_article_icon);   Create an image Sequence CImageList object M_imagelist.create (3, 3), Ilc_color32;   Add three icons to an image sequence for (int i=0; i<3; i++) {m_imagelist.add (hicon[i]);     }//Set the image sequence for the tree control M_webtree.setimagelist (&m_imagelist, tvsil_normal);   Insert root Node Hroot = M_webtree.insertitem (_t ("Chicken peck Rice"), 0, 0);   Insert child nodes under the root node Hcataitem = M_webtree.insertitem (_t ("It Internet"), 1, 1, hroot, tvi_last);   Add additional numbered data for the IT Internet node, showing M_webtree.setitemdata (Hcataitem, 1) when the mouse is over the node;   Insert sub-nodes under the IT internet node Hartitem = M_webtree.insertitem ("Baidu article 1"), 2, 2, Hcataitem, tvi_last); As "Baidu WenChapter 1 "Node adds additional numbered data that displays M_webtree.setitemdata (Hartitem, 2) when the mouse is over the node;   Insert another child node under the IT internet node Hartitem = M_webtree.insertitem (_t ("Google article 2"), 2, 2, Hcataitem, tvi_last);   Add additional numbered data for the "Google Article 2" node, showing M_webtree.setitemdata (Hartitem, 3) when the mouse is over the node;   Insert a second child node under the root node Hcataitem = M_webtree.insertitem (_t ("Digital Life"), 1, 1, hroot, tvi_last);   Add additional numbered data for the "Digital Life" node, showing M_webtree.setitemdata (Hcataitem, 4) when the mouse is over the node;   Insert child nodes under the "Digital Life" node Hartitem = M_webtree.insertitem (_t ("smart phone article 1"), 2, 2, Hcataitem, tvi_last);   Add additional numbered data for the smart Phone article 1 node, displaying M_webtree.setitemdata (Hartitem, 5) when the mouse crosses the node;   Insert another child node under the "Digital Life" node Hartitem = M_webtree.insertitem (_t ("Tablet PC Article 2"), 2, 2, Hcataitem, tvi_last);   Add additional numbered data for the Tablet Article 2 node, showing M_webtree.setitemdata (Hartitem, 6) when the mouse is over the node;   Insert a third child node under the root node Hcataitem = M_webtree.insertitem (_t ("Software Development"), 1, 1, hroot, tvi_last);   Add additional numbered data for the Software development node, showing M_webtree.setitemdata (Hcataitem, 7) when the mouse is over the node; Inserting child nodes under the software development node Hartitem = M_webtree.insertitem (_t ("Getting Started with C + + programming Series 1"), 2, 2,Hcataitem, Tvi_last);   Add additional numbered data for the C + + programming Starter Series 1 node, showing M_webtree.setitemdata (Hartitem, 8) when the mouse is over the node;   Insert another child node under the software development node Hartitem = M_webtree.insertitem (_t ("Getting Started with VS2010/MFC Programming 2"), 2, 2, Hcataitem, tvi_last);   Add additional numbered data for the VS2010/MFC Programming starter 2 node, showing M_webtree.setitemdata (Hartitem, 9) when the mouse is over the node;   Insert a fourth child node under the root node Hcataitem = M_webtree.insertitem (_t ("Entertainment"), 1, 1, hroot, tvi_last);   Add additional numbered data for the "Entertainment" node, showing M_webtree.setitemdata (Hcataitem, 10) when the mouse is over the node;   Insert sub-nodes under "Entertainment" Node Hartitem = M_webtree.insertitem (_t ("Maya civilization Article 1"), 2, 2, Hcataitem, tvi_last);   Add additional numbered data for the Maya Civilization article 1 node, showing M_webtree.setitemdata (Hartitem, 11) when the mouse is over the node;   Insert another child node under the "Entertainment" node Hartitem = M_webtree.insertitem (_t ("It joke 2"), 2, 2, Hcataitem, tvi_last);     Add additional numbered data for the IT joke 2 node, showing M_webtree.setitemdata (Hartitem, 12) when the mouse is over the node; return TRUE; Return TRUE unless you set the focus to a control}

6. We want to be able to respond to tvn_selchanged notification messages by displaying the latest selections in real Time to the edit box when the selected node changes. Add a message handler function Cexample31dlg::ontvnselchangedwebtree for the notification message tvn_selchanged the tree control Idc_web_tree, and modify the function body as follows:

void Cexample31dlg::ontvnselchangedwebtree (NMHDR *pnmhdr, LRESULT *presult) {   Lpnmtreeview Pnmtreeview = Reinterpret_cast<lpnmtreeview> (PNMHDR);   Todo:add your control notification handler code here   *presult = 0;     CString StrText; The label text string for the tree node        //Gets the handle of the currently selected node   htreeitem hItem = M_webtree.getselecteditem ();   Gets the label text string for the selected node   strText = M_webtree.getitemtext (HItem);   Displays the string in the edit box   setdlgitemtext (Idc_item_sel_edit, strText);}

7. There is also a feature that needs to be implemented, that is, when the mouse across a tree node outside the root node, the corresponding tip information is displayed, the information in this instance is the number of the node. This requires a response to the TVN_GETINFOTIP notification message. Add a message handler function Cexample31dlg::ontvngetinfotipwebtree for the notification message tvn_getinfotip the tree control Idc_web_tree, and modify the function body as follows:

void Cexample31dlg::ontvngetinfotipwebtree (NMHDR *pnmhdr, LRESULT *presult) {   Lpnmtvgetinfotip Pgetinfotip = Reinterpret_cast<lpnmtvgetinfotip> (PNMHDR);   Todo:add your control notification handler code here   *presult = 0;   nmtvgetinfotip* Ptvtipinfo = (nmtvgetinfotip*) pnmhdr;  Convert the incoming PNMHDR to the Nmtvgetinfotip pointer type   htreeitem hroot = M_webtree.getrootitem ();   Gets the root node of the tree   CString strText;   The hint information for each tree node if     (Ptvtipinfo->hitem = = hroot)   {     //If the mouse across the node is the root node, the prompt message is null     StrText = _t ("");   }   Else  {     //If the mouse across the node is not the root node, the additional 32-bit data for that node is formatted as a string     Strtext.format (_t ("%d"), Ptvtipinfo->lparam);   }     Copy the StrText string to the PszText member of the ptvtipinfo struct variable so that it shows the message   wcscpy (Ptvtipinfo->psztext, StrText) of the content as StrText;}

8. Run the program and pop Up the results dialog box. Effects such as:

The knowledge of the tree controls is here, which can be slightly more complex than the previous controls. But with a lot of use, you will feel handy.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.