Copy an item to new location
// CopyItem - Copies an item to a new location
// Returns - Handle of the new item
// hItem - Item to be copied
// htiNewParent - Handle of the parent for new item
// htiAfter - Item after which the new item should be created
HTREEITEM CTreeCtrlX::CopyItem( HTREEITEM hItem, HTREEITEM htiNewParent,
HTREEITEM htiAfter /*= TVI_LAST*/ )
{
TV_INSERTSTRUCT tvstruct;
HTREEITEM hNewItem;
CString sText;
// get information of the source item
tvstruct.item.hItem = hItem;
tvstruct.item.mask = TVIF_CHILDREN | TVIF_HANDLE |
TVIF_IMAGE | TVIF_SELECTEDIMAGE;
GetItem(&tvstruct.item);
sText = GetItemText( hItem );
tvstruct.item.cchTextMax = sText.GetLength();
tvstruct.item.pszText = sText.LockBuffer();
// Insert the item at proper location
tvstruct.hParent = htiNewParent;
tvstruct.hInsertAfter = htiAfter;
tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
hNewItem = InsertItem(&tvstruct);
sText.ReleaseBuffer();
// Now copy item data and item state.
SetItemData( hNewItem, GetItemData( hItem ));
SetItemState( hNewItem, GetItemState( hItem, TVIS_STATEIMAGEMASK ),
TVIS_STATEIMAGEMASK );
// Call virtual function to allow further processing in derived class
OnItemCopied( hItem, hNewItem );
return hNewItem;
}
void CTreeCtrlX::OnItemCopied(HTREEITEM /*hItem*/, HTREEITEM /*hNewItem*/ )
{
// Virtual function
}
In the class declaration add the following.
public:
HTREEITEM CopyItem( HTREEITEM hItem, HTREEITEM htiNewParent,
HTREEITEM htiAfter = TVI_LAST );
protected:
virtual void OnItemCopied( HTREEITEM hItem, HTREEITEM hNewItem );
More for Developers
Top Authors
- Voted: 13 times.
- Voted: 11 times.
- Voted: 11 times.
- Voted: 10 times.
- Voted: 8 times.
- Paul Kimmel 214 articles
- Zafir Anjum 120 articles
- Tom Archer - MSFT 83 articles
- Mark Strawmyer 76 articles
- Jeffrey Juday 65 articles