Display a tree structure in a drop-down list box

Source: Internet
Author: User
Tags data structures table name

Author: Star Track (ORBIT)
Reprint please indicate the original author, otherwise do not reprint

Displaying a tree structure drop-down list box (or Combo list box) in a drop-down list box is one of the most common controls for developing software under Windows systems, which is highly adaptable and easy to use, and is popular among developers. A typical user interface is to use a drop-down list box (or a combo list box) to display all the data for a related table name in a database or a field in a table, and the user selects an item with the mouse to do something about it. In this case, the use of a drop-down list box for data selection can simplify the operation, users prefer this way, but the simple drop-down list box cannot express the relationship between the data, such as the parent-child relationship between two data items or the containment relationship. Developers often want to make this information available to users while simplifying user operations, making it easier for users to decide what to do next. In view of this situation we use MFC to develop a support to display the tree structure of the CComboBox derived class Cwzcombobox, and the derived class has been successfully applied to our software, now we will open the code, we hope that the vast number of developers to help, but also hope that we can work together to improve it. (Demo program Run example shown in Figure 1)

Figure 1 Demo program Run instance

First, we make a brief description of the custom functions and related data structures of the Cwzcombobox class. To enable a list box to display a tree structure, you should first design a data structure that is used to record the relationships between items in the list. We define a structure that is used to store the relationships between nodes, which might be called information data structures, as follows:

struct members Wparantoriginidx and WORIGINIDX are used to store the parent of the item and the original index position of the item itself in the list, because adding or deleting a list item causes the order of the list items to change, that is, the index position changes. Therefore, the relationship between them can only be determined by the original index position of each item. Member CType is used to store the index position of the node corresponding to the icon in the icon list (ImageList), the member Clevel is used to store the level of the item, the item at the root node clevel is 0, and the clevel of the subkey is larger than the parent. This member is used primarily to determine the horizontal starting position of items when displaying list items, so that each item appears in a tree-like structure.

The design of class Cwzcombobox is explained in detail below. Base class CComboBox The original public Method AddString () and insertstring () do not support additional information can no longer be used, deletestring () can not properly release the memory occupied by the additional information is not used, To prevent developers from misusing them to cause code crashes, we overloaded these three functions:

Simply returning 1 indicates that the function failed to execute. Manually add addctstring () and deletectstring () two functions for adding and removing list items. Addctstring () Adds a subkey to the first parameter wparentidx the specified item, and the function first determines whether WPARENTIDX is Nrootindex (defines the constant Nrootindex equals (WORD)-1), If it is nrootindex the key is set to the root, even if Clevel is 0.

When you delete an item, you first traverse the entire list, delete all the subkeys, release the memory allocated for the child information structure, and finally delete the item and release the memory that the item information structure occupies. Depending on the nature of the tree structure, the index position of all the subkeys of an item is between the item and the next sibling of the item, so the task of deleting the function is to delete all items that are higher than the item after the item, until it encounters the same or smaller item with the first progression. The outer while loop is used to delete the subkey until the flag Bfind is false, and the inner for loop searches the current item's subkey from the index position of the current item to the end of the list, as long as it is found, assigns the bfind to True and removes the item, and then jumps out of the for loop. Continue the outer while loop.

  uses the Getchildcount () and CURRENTIDXFROMORIGINIDX () two functions in the Delete function, which are declared as protected members and do not allow direct access. Getchildcount () Returns all subkeys of the item at the specified index, including children of the subkey, and is implemented simply by using the attributes of the tree structure mentioned earlier to count the number of items after the item that are higher than the item until the first progression is encountered with the same or smaller item. The Currentidxfromoriginidx () function finds and returns the index position of its current position based on the original index position of an item, and implements the simple method of traversing the entire list, comparing the original index position of each item, and finding the same return its current index position in the list. The above is a description of the custom functions and related data structures of the Cwzcombobox class, and the functions related to MFC are now explained. First, a brief description of the self-drawn control, in fact all the controls are "self-painted", Windows system window when you need to redraw the control in the window will send a WM_DRAWITEM message to the control, the message has two parameters, the first parameter is the ID of the control, The second argument is a pointer to the DRAWITEMSTRUCT structure that contains the controls that draw their own display device context, location, control's state, and other properties that the control draws itself, using the DRAWITEMSTRUCT structure. MFC simplifies the complexity of programming by encapsulating the controls and mapping Wm_drawitem messages to virtual function DrawItem. Therefore, to implement a list box to display a custom tree structure, you can simply overload the DrawItem function. When the system needs to draw a list box, it calls DrawItem multiple times to draw each item of the list box. The DrawItem of Cwzcombobox class is simply to draw the item in the corresponding position according to the information structure, so there is no need to make too much explanation. Another function that needs to be overloaded is DeleteItem, which is also a virtual function of the base class, and the purpose of overloading this function is to allow the system to delete list items while freeing the storage space allocated for the information structure of the item. Our custom delete function does not directly release the storage space occupied by the information structure, but instead calls the base class's deletestring function, which is triggered by the deletestring function, which simplifies the developer's maintenance of the list item. The Cbn_dropdown event is triggered when the drop-down list box is opened, the Cwzcombobox class responds to this event, and the calculation causes all items in the list box to fully display the minimum width value that is required, and resets the width of the list box based on the minimum width value, making it look visually better.     

The use of the Cwzcombobox class is very simple, just look at the example code to know. This code was originally published in the www.codeproject.com site, so the project's resources are in English, and the comments are in English. Www.vchelp.net website is also reproduced, you can also use the following Url:http://www.winmsg.com/download/cwzcombox.zip download code and demo program.

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.