Copy an item to new location | CodeGuru

Copy an item to new location

Copying an item is simple but Ive listed a function for copying anyway. This function is used by other functions discussed later and it also contains a component that we often forget – extensibility. At the end of the function, the OnItemCopied() function is called to give the derived class the oppurtunity to update any […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More




Copying an item is simple but Ive listed a function for copying anyway.


This function is used by other functions discussed later and it also contains


a component that we often forget

extensibility. At the end of the function,


the OnItemCopied() function is called to give the derived class the oppurtunity


to update any internal information.



 

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



CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.