Dynamic Calculation of DragImage Hotspot

Have you ever noticed how, when you drag an item from the left pane in the Explorer, the drag image is always created relative to the cursor position? It looks like you are grabbing the drag item exactly where the cursor is. Here is code that accomplishes this. The key is to dynamically calculate the offset between the cursor and the selected image, and use that as the hotspot.

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:00am

    Originally 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
    if (pMyTree->GetItemRect(htiHit, &itemRect, TRUE))
    {
    // Calculate the new offset
    offsetPt.y = dragPt.y - itemRect.top;
    offsetPt.x = dragPt.x - (itemRect.left - pMyTree->GetIndent());
    }

    Reply
  • Account for stateMask bitmap in x offset

    Posted by Legacy on 12/09/1999 12:00am

    Originally posted by: Joey Ting

    Add the following after the new offset calculation.
    
    

    UINT nState;
    if(nState=GetItemState( htiHit, LVIS_STATEIMAGEMASK ) ){
    (nState>>=12)--;
    IMAGEINFO ImageInfo;
    //State Image list
    m_ImageListState.GetImageInfo(nState,&ImageInfo);
    offsetPt.x -= (ImageInfo.rcImage.right-ImageInfo.rcImage.left);
    }

    Reply
  • Taking checkboxes into account

    Posted by Legacy on 05/27/1999 12:00am

    Originally posted by: P�l Kristian T�nder

    The following code also checks for state images
    
    (for instance check boxes)

    CImageList* pList = pMyTree->GetImageList(TVSIL_STATE);
    if(pList) {
    IMAGEINFO info;
    pList->GetImageInfo(1, &info);
    offsetPt.x -= info.rcImage.right-info.rcImage.left;
    }

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 13, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT If you are developing applications, you'll want to join us to learn how applications are changing as a result of gesture recognition. This technology will change how you and your users interact - not simply with your devices, but with the world around you. Your devices will be able to see and hear what your users are doing. Are your applications ready for this? Join us to learn about Intel® RealSense™ Technology, including never been …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds