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.

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:

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()':

// '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;

 MFC Tree Control: How to disable an item in Tree Control?

Q: How to disable an item?

A: Tree items do not have enabled/disabled states. That means that you can stop searching for a function called 'SetItemEnabled()', because it does not exist. You have to simulate this by your own. Let's outline what you need to do in order to 'disable' an item:

  • You need to mark that item as 'disabled'. As the tree control itself doesn't help here, you need to reserve a flag in the underlaying structure of each item. You will use 'SetItemData()' and 'GetItemData()' to set and retrieve this structure for each item.

  • You need to give some visual feedback to your user that the item is disabled. The easiest solution is to use a special (grayed) icon for the item. You can refine this approach up to changing the items color or font.

  • You need to prevent some operations on that item, like expanding, selecting, dragging it or dropping on it. We assume that for each item you have correctly set an underlaying data structure called 'CItemStruct', and this has a boolean member 'm_bDisabled'.

    //Preventing selection: (handle TVN_SELCHANGING)
    void CYourDialog::OnSelchangingTree(NMHDR* pNMHDR, LRESULT* pResult)
    {
    NM_TREEVIEW* pNMTreeView
    = (NM_TREEVIEW*) pNMHDR;
    if
    (((CItemStruct *) m_tree.GetItemData(pNMTreeView->iNewItem))->m_bDisabled)
    {
    *pResult
    = 1;
    return;
    }
    *pResult
    = 0;
    }

MFC List Control: How to correctly delete items from a CListCtrl?

Q: How to correctly delete items from a CListCtrl?

A: Unlike the 'HTREEITEM' values returned from 'CTreeCtrl::InsertItem()', the integer values returned from 'CListCtrl::InsertItem()' aren't stable. In short, this means that you shouldn't store them anywhere. If you need to find an entry again, it's probably best to simply put something useful in 'LVITEM::lParam', and search for it using 'CListCtrl::GetNextItem()'.

Having said that, one can use the following piece of code to correctly delete items from a CListCtrl:

for (int nItem = 0; nItem < m_List.GetItemCount(); )
{
if (m_List.GetItemState(nItem, LVIS_SELECTED) == LVIS_SELECTED)
m_List.DeleteItem(nItem)
;
else
++nItem;
}

 

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值