Dynamic Calculation of DragImage Hotspot
Posted
by Chris Protopapas
on August 6th, 1998
This code is added to the OnBeginDrag method, immediately before the call to CImageList::BeginDrag. Change variable names as needed to make it work in your code.
// 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 = pMyTree->HitTest(dragPt, &nHitFlags);
if (NULL != htiHit)
{
// The drag point has Hit an item in the tree
CRect itemRect;
if (pMyTree->GetItemRect(htiHit, &itemRect, FALSE))
{
// Count indent levels
HTREEITEM htiParent = htiHit;
int nIndentCnt = 0;
while (htiParent != NULL)
{
htiParent = pMyTree->GetParentItem(htiParent);
nIndentCnt++;
}
// Calculate the new offset
offsetPt.y = dragPt.y - itemRect.top;
offsetPt.x = dragPt.x - (nIndentCnt * pMyTree->GetIndent()) + GetScrollPos(SB_HORZ);
}
}
// Begin the Drag operation using the Drag image and the calculated hotspot offset
m_pDragImage->BeginDrag(0, offsetPt);

Comments
Why to count indent levels?
Posted by Legacy on 04/25/2000 12:00amOriginally posted by: Tomas Brotz
Simply get the text bounding rectangle and substract one indent. You save the while cycle and the GetScrollPos() call.
// Get the text bounding rectangle
Replyif (pMyTree->GetItemRect(htiHit, &itemRect, TRUE))
{
// Calculate the new offset
offsetPt.y = dragPt.y - itemRect.top;
offsetPt.x = dragPt.x - (itemRect.left - pMyTree->GetIndent());
}
Account for stateMask bitmap in x offset
Posted by Legacy on 12/09/1999 12:00amOriginally posted by: Joey Ting
ReplyTaking checkboxes into account
Posted by Legacy on 05/27/1999 12:00amOriginally posted by: P�l Kristian T�nder
Reply