Beginning the Tree-View Drag Operation
A tree-view control sends the parent window a TVN_BEGINDRAG (or TVN_BEGINRDRAG) notification message whenever the user starts to drag an item. The parent window receives the notification in the form of a WM_NOTIFY message whose lParam parameter is the address of an NM_TREEVIEW structure. The members of this structure include the screen coordinates of the mouse cursor and a TV_ITEM structure that contains information about the item to be dragged.
The following example shows how to process the WM_NOTIFY message to obtain TVN_BEGINDRAG.
case WM_NOTIFY:
switch (((LPNMHDR) lParam)->code) {
case TVN_BEGINDRAG:
Main_OnBeginDrag(hwndTV, // application-defined function
(NM_TREEVIEW *) lParam);
break;
.
. // Handle other notifications here.
.
}
break;
Beginning the drag operation involves using the ImageList_BeginDrag function. The function's parameters include the handle of the image list containing the image to use during the drag operation and the index of the image. You can either provide your own image list and image, or you can have the tree-view control create them for you by using the TVM_CREATEDRAGIMAGE message.
Because the drag image replaces the mouse cursor for the duration of the drag operation, ImageList_BeginDrag requires you to specify a hot spot within the image. The coordinates of the hot spot are relative to the upper left corner of the image. ImageList_BeginDrag also requires you to specify the initial location of the drag image. An application typically sets the initial location so that the hot spot of the drag image corresponds to that of the mouse cursor at the time the user began the drag operation.
The following function demonstrates how to begin dragging a tree-view item. It uses the drag image provided by the tree-view control and obtains the bounding rectangle of the item to determine the appropriate point for the hot spot. (The dimensions of the bounding rectangle are the same as those of the image.) Note that the bounding rectangle does not account for the indentation of child items. For this reason, the function adds the amount of indentation to the x-coordinate of the hot spot.
The function captures mouse input, causing mouse messages to be sent to the parent window. The parent window needs the subsequent WM_MOUSEMOVE messages to determine where to drag the image and the WM_LBUTTONUP message to determine when to end the drag operation.
// Main_OnBeginDrag - begins dragging an item in a tree-view control.
// hwndTV - handle of the image list
// lpnmtv - address of information about the item being dragged
void Main_OnBeginDrag(HWND hwndTV, NM_TREEVIEW *lpnmtv)
{
HIMAGELIST himl; // handle of image list
RECT rcItem; // bounding rectangle of item
DWORD dwLevel; // heading level of item
DWORD dwIndent; // amount that child items are indented
// Tell the tree-view control to create an image to use
// for dragging.
himl = TreeView_CreateDragImage(hwndTV, lpnmtv->itemNew.hItem);
// Get the bounding rectangle of the item being dragged.
TreeView_GetItemRect(hwndTV, lpnmtv->itemNew.hItem, &rcItem, TRUE);
// Get the heading level and the amount that the child items are
// indented.
dwLevel = lpnmtv->itemNew.lParam;
dwIndent = (DWORD) SendMessage(hwndTV, TVM_GETINDENT, 0, 0);
// Start the drag operation.
ImageList_BeginDrag(himl, 0, 0, 0);
// Hide the mouse cursor, and direct mouse input to the
// parent window.
ShowCursor(FALSE);
SetCapture(GetParent(hwndTV));
g_fDragging = TRUE;
return;
}