如何为CTREECTRL的每一个ITEM指定自己的数据?
比如说每一个叶子ITEM除了用于显示的信息之外,还保存自己对应的数据库名。点击一个ITEM时,右边的列表控件显示相应数据库名中的数据。在用户选择ITEM时,我们可以得到ITEM的句柄。我想通过这个句柄,直接得到与相应ITEM对应的数据,如数据库名(字串)。不知道应该如何设置?SetItem与SetItemData可以吗?他们各自的用处是什么?如何为TVITEM结构赋值,可以在其中存放数据库名字串吗?这样可以直接使用GetItem???得到数据库名。
问题点数:36、回复次数:9Top
1 楼rushinger(阮祥哥)回复于 2005-06-03 18:29:36 得分 4
CTreeCtrl::GetItem
BOOL GetItem( TVITEM* pItem );
Return Value
Nonzero if successful; otherwise 0.
Parameters
pItem
A pointer to aTVITEM structure, as described in the Platform SDK.
CTreeCtrl::SetItem
BOOL SetItem( TVITEM* pItem );
BOOL SetItem( HTREEITEM hItem, UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam );
Return Value
Nonzero if successful; otherwise 0.
Parameters
pItem
A pointer to aTVITEM structure that contains the new item attributes, as described in the Platform SDK.
hItem
Handle of the item whose attributes are to be set.
nMask
Integer specifying which attributes to set.
lpszItem
Address of a string containing the item’s text.
nImage
Index of the item’s image in the tree view control’s image list.
nSelectedImage
Index of the item’s selected image in the tree view control’s image list.
nState
Specifies values for the item’s states.
nStateMask
Specifies which states are to be set.
lParam
A 32-bit application-specific value associated with the item.
Remarks
Call this function to set the attributes of the specified tree view item.
In the TVITEM structure, the hItem member identifies the item, and the mask member specifies which attributes to set.
If the mask member or the nMask parameter specifies the TVIF_TEXT value, the pszText member or the lpszItem is the address of a null-terminated string and the cchTextMax member is ignored. If mask (or nMask) specifies the TVIF_STATE value, the stateMask member or the nStateMask parameter specifies which item states to change and the state member or nState parameter contains the values for those states.
CTreeCtrl::GetItemData
DWORD GetItemData( HTREEITEM hItem ) const;
Return Value
A 32-bit application-specific value associated with the item specified by hItem.
Parameters
hItem
Handle of the item whose data is to be retrieved.
Remarks
Call this function to retrieve the 32-bit application-specific value associated with the specified item.
CTreeCtrl::SetItemData
BOOL SetItemData( HTREEITEM hItem, DWORD dwData );
Return Value
Nonzero if successful; otherwise 0.
Parameters
hItem
Handle of the item whose data is to be retrieved.
dwData
A 32-bit application-specific value associated with the item specified by hItem.
Remarks
Call this function to set the 32-bit application-specific value associated with the specified item.
每一个Item可以存储一个DWORD型数值,通过SetItemData和GetItemData来存取
Top
2 楼littlepig_2002(小朱)回复于 2005-06-03 18:38:14 得分 0
看来应该使用SetItemData, GetItemData
如果想存放字串数据,如数据库文件名,应该如何存放?Top
3 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2005-06-03 19:16:14 得分 2
一样的可以,Top
4 楼littlepig_2002(小朱)回复于 2005-06-03 20:38:17 得分 0
我想问一下,字串如何存放到SetItemData, GetItemData参数DWORD类型变量中?Top
5 楼missle(闪人)回复于 2005-06-03 21:06:07 得分 17
使用SetItemData可以设置指针进去,但是你要重载OnDeleteItem函数去释放他,例子如下:
void CCateTreeView::OnDeleteitem(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
HTREEITEM hItem = pNMTreeView->itemOld.hItem;
if(hItem)
{
DWORD data = GetTreeCtrl().GetItemData(hItem);
if(-1 != data)
{
KP_Data *p = (KP_Data*)data;
delete p;
}
}
*pResult = 0;
}
设置的时候是这个样子的
KP_Data *pData = new KP_Data;
pData->Id = cate.CateId;
pData->bCard = false;
m_treeCtrl.SetItemData(hSub, (DWORD)pData);
我的KP_Data结构如下
class KP_Data
{
public:
SDEGuid Id;
Boolean bCard;
KP_Data()
{
Id = 0;
bCard = false;
}
};
你可以把替换成你要的结构
比如
class KP_Data
{
public:
CString dbName;
};
Top
6 楼littlepig_2002(小朱)回复于 2005-06-03 22:30:35 得分 0
不好意思问一下楼上的前辈,上面的代码是不是实践过:) 不过我觉得是很好的办法。另外,没有可以直接存放字串的变量吗?
另外,SetItemData设置的是不是TVITEM中的lParam成员?
Top
7 楼fanqing(火影忍者+28%(准备学习进程/线程))回复于 2005-06-04 09:03:15 得分 8
1.SetItemData
2.直接存放字串的变量--自己子类化,构造一个链表存放.
3.BOOL SetItemData(
HTREEITEM hItem,
DWORD dwData );
Top
8 楼ShiGang(Sucess)回复于 2005-06-04 09:10:01 得分 5
SetItemData保存一個存儲你自己數據的指針.調用時用GetItemData,就可以了.Top
9 楼missle(闪人)回复于 2005-06-04 20:09:43 得分 0
当然是,是从我目前写的code中copy过来的,看来,楼主对SetItemData的用法不是很清楚阿,详细如下
树中的每个item都有个HTREEITEM, 即是item的handle,使用SetItemData,必须先取得每个item的handle,SetItemData是树控件的成员函数,没有现成的东西了, fanqing(火影忍者+28%(准备学习进程/线程)) 的方法也可以,这样你就可以不重写OnDeleteitem了,但是这个链表在应用程序结束的时候要手工释放,否则会内存泄漏.Top




