Build a Tree View Control Using VC ++

Source: Internet
Author: User
Tree View Controls are well-organized, structured, beautiful, and flexible. They are widely used in various operating systems and are the most familiar and commonly used controls.

From the appearance of Tree View Controls to the present, they have been considered very complex and difficult to program. To make them run normally, they must be compared with other controls such as edit boxes, radio buttons, and check boxes, developers need to do more work. However, the Tree View Control provides developers with more capabilities and space while using complex controls. Here I will introduce the programming and usage of Tree View Controls in VC ++.

The Tree View control ctreectrl class provided by MFC is used to encapsulate the function of the Tree View control. It is also a very thin package. It is applied in a dialog box or a view form. Like other controls, you can drag and drop the widget to change its position, size, and basic attributes.

Create a ctreectrl as follows:

1. Drag ctreectrl to the View window, adjust the position and size, and define its object ID as idc_tree.

2. Change its properties and select the check boxes of has buttons and has lines. In this way, the use of the Tree View control is the same as that in Windows Resource Manager.

3. Define a cnewtree class inherited from ctreelctrl. In MFC classwizard, set the member variable for the new definition class to m_mytree.ProgramThis member variable is used to control the widget. This is done to facilitate the addition of other user-defined functions.

After completing the above steps, we can start writingCode. First, initialize the Tree View Control and associate it with an image list. Then, use the insertitem function to add nodes. Add the following code to the oninitialupdate () event in the cmyformview in the View window:

Compared with ctreeview, ctreectrl is a "lightweight" version of ctreeview, and programming is relatively simple.

Void cmyformview: oninitialupdate ()
{
Hicon [7];
Cimagelist m_imagelist;
M_imagelist.create (16, 16, 7 );
// Create an image list
M_imagelist.setbkcolor (RGB (255,255,255); hicon [0] = afxgetapp ()-> loadicon (IDI _ BMP 0); hicon [1] = afxgetapp () -> loadicon (IDI _ BMP 1 );
......
Hicon [6] = afxgetapp ()-> loadicon (IDI _ BMP 6 );
For (INT I = 0; I <= 6; I ++)
{
M_imagelist.add (hicon [I]);
}
M_mytree.setimagelist (& m_imagelist, tvsil_normal)
// Set an image list for m_mytree to display different icons for different nodes of ctreectrl
Htreeitem m_item
M_item = m_mytree.insertitem ("root", 0, 0, 0 );
// The root node icon is idi_bmp 0
If (m_item! = NULL)
// The root node is successfully created.
{
M_mytree.insertitem ("subitem1", 1, 1, m_item)
// Create a subnode named subitem1 under the root node, and the displayed icon is idi_bmp 1. Similarly, you can create other nodes and the nodes at the same level display the same icons.
}
......
}

The ctreectrl class does not provide node search functions. Therefore, programmers are required to write search functions with specific conditions.

Generally, events triggered by clicking different nodes are different. In this case, you need to add the onselchangedtree event. In the classwiard window, select the cmyformview class. The object ID is idc_tree and the message is tvn_selchanged. Add a function and edit the code.

Void cmyformview: onselchangedtree (nmhdr pnmhdr, lresult presult)
{
Htreeitem selitem;
Mystructure itemdata; // mystructure is the structure type defined by the user.
Selitem = m_mytree.getselecteditem ();
Itemdata = getitemdata (selitem );
// Obtain the Data Pointer of the node
Switch (itemdata-> value1)
{
Case 0 :{......}
// The operation specified by the user
Case 1 :{......}
......
}
}

In actual programming, it may not only be for display. Each node on the Tree View control corresponds to a specific value, so the pointer to specific data should be assigned to the corresponding node. The specific method is to add a new setvalue (htreeitem) process in the Custom class cnewtree ). The Code is as follows:

Void cnewtree: setvalue (htreeitem item_parm, int value1, int value2 .....)
{
Mystructure itemdata
Itemdata = new mystructure ;;
Itemdata-> value1 = value1;
Itemdata-> value2 = value2;
......
Setitemdata (item_parm, (DWORD) itemdata );
}

You can assign values to a given node by passing in corresponding parameters. Of course, the dynamic allocation address new is used here. Therefore, do not forget to delete these spaces before the program ends.

Void cnewtree: deletedata (htreeitem item)
{
Mystructure itemdata;
Itemdata = getitemdata (item );
// Obtain the Data Pointer of the node
If (itemdata! = NULL) {Delete [] (char) itemdata ;}
// Delete the Occupied Space
......
}

Based on the structure characteristics of the Tree View, we use recursive traversal to find nodes. Of course, you can narrow the traversal range according to the conditions. Here, I write a custom function findnode () based on the node value matching and return the handle of the first qualified node. The Code is as follows:

Htreeitem cnewtree: findnode (htreeitem nodeitem, Int & nodevalue)
{
Mystructure itemdata;
Htreeitem nextitem;
If (nodeitem = NULL)
Return NULL;
// Recursive exit
Else
{
While (nodeitem! = NULL)
{
Itemdata = getitemdata (nodeitem );
If (itemdata-> value1 = nodevalue)
Return nodeitem;
Nodeitem = getchilditem (nodeitem );
// Obtain the handle of the first subnode of the current node.
If (findnode (nodeitem, nodevalue) = NULL );
// Recursive search
Nodeitem = getnextsiblingitem (nodeitem );
// Obtain the handle of the sibling node of the current node
}
}
}

So far, I have introduced some Tree View control programming methods, including the establishment of Tree View Controls, the granting and deletion of node values, and searching. Of course, it is widely used and used in many ways. The basic framework for building Tree View Controls is provided here. On this basis, the Tree View controls can be expanded to provide more powerful functions, such as combining List View Controls and adding pop-up menus to them. If you are interested, you may wish to extend the control by yourself.

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.