1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| CMyTreeCtrl::CMyTreeCtrl()
{
m_pDragImage = NULL;
m_hitemDrag = NULL;
m_hitemDrop = NULL;
m_bLDragging = FALSE;
}
// 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 CMyTreeCtrl::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 CMyTreeCtrl::OnItemCopied(HTREEITEM /*hItem*/, HTREEITEM /*hNewItem*/ )
{
// Virtual function
}
// CopyBranch - Copies all items in a branch to a new location
// Returns - The new branch node
// htiBranch - The node that starts the branch
// htiNewParent - Handle of the parent for new branch
// htiAfter - Item after which the new branch should be created
HTREEITEM CMyTreeCtrl::CopyBranch( HTREEITEM htiBranch, HTREEITEM htiNewParent,
HTREEITEM htiAfter /*= TVI_LAST*/ )
{
HTREEITEM hChild;
HTREEITEM hNewItem = CopyItem( htiBranch, htiNewParent, htiAfter );
hChild = GetChildItem(htiBranch);
while( hChild != NULL)
{
// recursively transfer all the items
CopyBranch(hChild, hNewItem);
hChild = GetNextSiblingItem( hChild );
}
return hNewItem;
}
void CMyTreeCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
*pResult = 0;
m_hitemDrag = pNMTreeView->itemNew.hItem;
m_hitemDrop = NULL;
m_pDragImage = CreateDragImage(m_hitemDrag); // get the image list for dragging
// CreateDragImage() returns NULL if no image list
// associated with the tree view control
if( !m_pDragImage )
return;
m_bLDragging = TRUE;
m_pDragImage->BeginDrag(0, CPoint(-15,-15));
POINT pt = pNMTreeView->ptDrag;
ClientToScreen( &pt );
m_pDragImage->DragEnter(NULL, pt);
SetCapture();
}
void CMyTreeCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
HTREEITEM hitem;
UINT flags;
if (m_bLDragging)
{
POINT pt = point;
ClientToScreen( &pt );
CImageList::DragMove(pt);
if ((hitem = HitTest(point, &flags)) != NULL)
{
CImageList::DragShowNolock(FALSE);
SelectDropTarget(hitem);
m_hitemDrop = hitem;
CImageList::DragShowNolock(TRUE);
}
}
CTreeCtrl::OnMouseMove(nFlags, point);
}
void CMyTreeCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
CTreeCtrl::OnLButtonUp(nFlags, point);
if (m_bLDragging)
{
m_bLDragging = FALSE;
CImageList::DragLeave(this);
CImageList::EndDrag();
ReleaseCapture();
delete m_pDragImage;
// Remove drop target highlighting
SelectDropTarget(NULL);
if( m_hitemDrag == m_hitemDrop )
return;
// If Drag item is an ancestor of Drop item then return
HTREEITEM htiParent = m_hitemDrop;
while( (htiParent = GetParentItem( htiParent )) != NULL )
{
if( htiParent == m_hitemDrag ) return;
}
Expand( m_hitemDrop, TVE_EXPAND ) ;
HTREEITEM htiNew = CopyBranch( m_hitemDrag, m_hitemDrop, TVI_LAST );
DeleteItem(m_hitemDrag);
SelectItem( htiNew );
}
} |
Partager