15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Drag&Drop in a CTreeCtrl

dobrin@itls.com
Monday, June 24, 1996

Hello Ladies & Gentlemen !

My application is written in VC++ 4.1 and runs on NT3.51.

Drag & Drop in the CTreeCtrl shows the "dragged" image, but also a 
"copy" of that image at the root level of the tree, which moves 
together with the dragged item.

Please give me an answer on this problem .

Thank you in advance,

Dan Dobrin
dan.dobrin@itls.com

Routines included :
void CTreeCtrlEx::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult) 
{
 TV_ITEM tvI;
 UINT nImageId;

 HTREEITEM hItem;

 NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

 tvI = pNMTreeView->itemNew;

 // Get a handle to the drag object.
 m_hDragItem = tvI.hItem;

 item.mask = TVIF_HANDLE | TVIF_IMAGE;
 item.hItem= m_hDragItem;
 GetItem(&item); 
 nImageId = item.iImage;

  BeginDrag((NM_TREEVIEW *)pNMTreeView);

 *pResult = 0;
}

void CTreeCtrlEx::OnMouseMove(UINT nFlags, CPoint point) 
{
 HTREEITEM hTarget;
 UINT flags;

 if (m_fDragging)
 {
  // Drag the item to the current mouse position.
  pImageList->DragMove(point);

  flags = TVHT_ONITEM | TVHT_ONITEMRIGHT;
  // if the cursor is on an item, highlight it as the drop target.
  if ((hTarget = HitTest(point, &flags)) != NULL)
  {
   pImageList->DragLeave(this);
   SelectDropTarget(hTarget);
   pImageList->DragEnter(this, point);
  }
 }

 CTreeCtrl::OnMouseMove(nFlags, point);
}



void CTreeCtrlEx::OnLButtonUp(UINT nFlags, CPoint point) 
{
 // If dragging, stop it.
 if (m_fDragging)
 {
  pImageList->DragLeave(this);
  pImageList->EndDrag();

  // Process the item drop.
  DropItem(m_hDragItem);

  // Release the mouse.
  ReleaseCapture();

  // Show the normal cursor.
  ShowCursor(TRUE);

  // Reset boolean.
  m_fDragging = FALSE;
 }

 CTreeCtrl::OnLButtonUp(nFlags, point);
}

/*********************************************************************
****** * *    FUNCTION: BeginDrag(NM_TREEVIEW *) * *    PURPOSE: This
function proceeses the beginning of a drag operation. *
**********************************************************************
*****/ void CTreeCtrlEx::BeginDrag(NM_TREEVIEW *lItem) {
 // Create an image to use for dragging.

 pImageList = CreateDragImage(lItem->itemOld.hItem);

 // show image
 pImageList->DragShowNolock(TRUE);

 //create drag cursor image
 pImageList->SetDragCursorImage(0, lItem->ptDrag);
 ShowCursor(FALSE);

 // Start dragging the image.
 pImageList->BeginDrag(0, lItem->ptDrag);

 pImageList->DragMove(lItem->ptDrag);
 pImageList->DragEnter(this, lItem->ptDrag);

 // Hide the cursor
 SetCapture();
 m_fDragging = TRUE;
}

/*********************************************************************
****** * *    FUNCTION: DropItem(HTREEITEM) * *    PURPOSE: This
function proceeses the drop consequences. *
**********************************************************************
*****/ void CTreeCtrlEx::DropItem(HTREEITEM hDragItem) {
 HTREEITEM hParent, hTarget;

 // Get the handle to the drop target.
 hTarget = GetDropHilightItem();

 // Get the parent of the drop target.
 hParent = GetParentItem(hTarget);

 TV_INSERTSTRUCT tvIns;
 TCHAR			szBuffer[100];
 tvIns.item.hItem = hDragItem;
 tvIns.item.cchTextMax = 99;
 tvIns.item.pszText = szBuffer;
 tvIns.item.mask = TVIF_CHILDREN | TVIF_HANDLE | TVIF_IMAGE |
 TVIF_SELECTEDIMAGE | TVIF_TEXT | TVIF_PARAM; GetItem(&tvIns.item);

 tvIns.hInsertAfter = hTarget;
 tvIns.hParent = hParent;
 InsertItem(&tvIns);

 // Delete the "dragged" item.
 DeleteItem(hDragItem);

 // Reset the drop target to NULL.
 SelectDropTarget((HTREEITEM)NULL);
}



Roland Pasternack -- rep@jupiter.Legato.COM
Friday, June 28, 1996

At 01:13 PM 6/24/96 -0500, you wrote:
>Hello Ladies & Gentlemen !
>
>My application is written in VC++ 4.1 and runs on NT3.51.
>
>Drag & Drop in the CTreeCtrl shows the "dragged" image, but also a 
>"copy" of that image at the root level of the tree, which moves 
>together with the dragged item.
>
>Please give me an answer on this problem .
>
>Thank you in advance,
>
>Dan Dobrin
>dan.dobrin@itls.com


Dan,

I tried doing drag and drop in a tree control a while ago and ran into
several problems.  When I started to research the problems I discovered that
drag and drop in a tree control was not well documented, nor could I find
any samples.  I ended up calling Microsoft and speaking to a support person
who wrote some sample code to do this.  I do not have the time to review
your code and try to figure out exactly what is causing your problem, but if
you e-mail me I will forward the sample code to you.

Roland
rpasternack@legato.com





| Вернуться в корень Архива |