void CGroupEditTree::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; *pResult = 0; SetFocus(); // Receive WM_KEYDOWN m_hitemDrag = pNMTreeView->itemNew.hItem; m_hitemDrop = NULL; SelectItem(m_hitemDrag); if (!IsDropSource(m_hitemDrag)) return; // get the image list for dragging m_pDragImage = CreateDragImage(m_hitemDrag); // CreateDragImage() returns NULL if no image list // associated with the tree view control if (!m_pDragImage) return; m_bDragging = TRUE; m_pDragImage->BeginDrag(0, CPoint(-15,-15)); m_dropCursor = LoadCursor(NULL, IDC_ARROW); m_noDropCursor = LoadCursor(NULL, IDC_NO); POINT pt = pNMTreeView->ptDrag; ClientToScreen(&pt); m_pDragImage->DragEnter(NULL, pt); SetCapture(); *pResult = 0; }
void CTreeFileCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; *pResult = 0; SelectItem(pNMTreeView->itemNew.hItem); if (!IsDropSource(pNMTreeView->itemNew.hItem) || !m_bAllowDragDrop) return; m_pilDrag = CreateDragImage(pNMTreeView->itemNew.hItem); if (!m_pilDrag) return; m_hItemDrag = pNMTreeView->itemNew.hItem; m_hItemDrop = NULL; // Calculate the offset to the hotspot CPoint offsetPt(8,8); // Initialize a default offset CPoint dragPt = pNMTreeView->ptDrag; // Get the Drag point UINT nHitFlags = 0; HTREEITEM htiHit = HitTest(dragPt, &nHitFlags); if (htiHit != NULL) { // The drag point has Hit an item in the tree CRect itemRect; if (GetItemRect(htiHit, &itemRect, FALSE)) { // Count indent levels HTREEITEM htiParent = htiHit; int nIndentCnt = 0; while (htiParent != NULL) { htiParent = GetParentItem(htiParent); nIndentCnt++; } if (!(GetStyle() & TVS_LINESATROOT)) nIndentCnt--; // Calculate the new offset offsetPt.y = dragPt.y - itemRect.top; offsetPt.x = dragPt.x - (nIndentCnt * GetIndent()) + GetScrollPos(SB_HORZ); } } //Begin the dragging m_pilDrag->BeginDrag(0, offsetPt); POINT pt = pNMTreeView->ptDrag; ClientToScreen(&pt); m_pilDrag->DragEnter(NULL, pt); SetCapture(); m_nTimerID = SetTimer(1, 300, NULL); }