MFC Tree Control: How to use 'SetItemData()' and 'GetItemData()'?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: MFC Tree Control: How to use 'SetItemData()' and 'GetItemData()'?

Hybrid View

  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    MFC Tree Control: How to use 'SetItemData()' and 'GetItemData()'?

    Q: How to use 'GetItemData()' and 'SetItemData()'?

    A: A tree control is just a visual representation of some hierarchical data structure. You use 'SetItemData()' and 'GetItemData()' to link each tree item to a node if this data structure.

    Firstly you have to define a class or a structure that holds the data for each node. For example if your tree is supposed to show a file system, the structure you define will reflect all the properties of a file, like name, size, timestamp, whether it is a directory or not, access rights and so on. The tree will display only the name. Each of the trees items will be linked to an instance of such a structure.

    Code:
    struct node_data
    {
      //...
    };
    'SetItemData()' allows you to attach a 'DWORD' to each item and 'GetItemData()' allows you to get that 'DWORD' back. A plain 'DWORD' isn't of much use, but luckily under Windows a 'DWORD' and a pointer have the same size, so you can cast forth and back between them.

    When you add an item to the tree, you attach a pointer to a 'node_data' structure to that item:

    Code:
    HTREEITEM hItem = m_tree.InsertItem(/*...*/);
    node_data *node = new node_data();
    
    // Fill up the new node
    node->member = value;    //...
    
    // Attach the node to the item
    m_tree.SetItemData(hItem, (DWORD) node);
    When you handle an operation on some item of the tree (for example selection) you retrieve the node using 'GetItemData()':

    Code:
    // 'hItem' is a valid iten handle
    node_data *node = (node_data *) m_tree.GetItemData(hItem);
    
    // Retrieve, set or take decisions according to the nodes 
    // members
    if(node->member == some_value)
      some_action();
    
    value = node->member;
    node->member = value;
    Last edited by Andreas Masur; July 25th, 2005 at 03:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center