Share via


Handling Drag-and-Drop Messages (Windows CE 5.0)

Send Feedback

A tree-view control notifies the parent window when a user starts to drag an item by using a mouse or stylus. The parent window receives a TVN_BEGINDRAG message when a user begins dragging an item by using the left mouse button and a TVN_BEGINRDRAG message when a user begins dragging by using the right button. You can prevent a tree-view control from sending these messages by giving the tree-view control the TVS_DISABLEDRAGDROP style.

You can obtain an image to display during a drag operation by using the TVM_CREATEDRAGIMAGE message. The tree-view control creates a dragging bitmap that is based on the label of the item that is being dragged. Then, the tree-view control creates an image list, adds the bitmap to it, and returns the handle to the image list.

You must provide the code that actually drags the item. Typically, this task involves using the dragging capabilities of the functions that manipulate image lists, and including code for processing the WM_MOUSEMOVE and WM_LBUTTONUP messages that are sent to the parent window after the drag operation begins.

To use an item in a tree-view control as the target of a drag-and-drop operation, use the SendMessage function to send a TVM_HITTEST message to determine when the mouse or stylus is on a target item. To do this, specify the address of a TVHITTESTINFO structure that contains the current coordinates of the mouse or stylus. When SendMessage returns, the structure contains a flag that indicates the location of the mouse or stylus relative to the tree-view control. If the mouse or stylus is over an item in the tree-view control, the structure contains the handle to the item, too.

You can indicate that an item is the target of a drag-and-drop operation by using the TVM_SETITEM message to set the state to TVIS_DROPHILITED. An item that has this state is drawn in the style that is used to indicate a target for a drag-and-drop operation.

The following code example shows how to handle drag-and-drop messages.

case WM_NOTIFY:
{
  LPNMHDR  pnmh = (LPNMHDR) lParam;
  
  switch (pnmh->code)
  {
    case TVN_BEGINDRAG:
      {  
         // Notifies the tree-view control's parent window that a 
         // drag-and-drop operation is being initiated.

         return 0;
      }
         
      case TVN_BEGINLABELEDIT:
      {  
         // Notifies the tree-view control's parent window about the 
         // start of label editing for an item.

         return 0;
      }
         
    case TVN_ITEMEXPANDED:
    {
      // Notifies a tree-view control's parent window that a parent
      // item's list of child items has expanded or collapsed. This 
      // message is sent in the form of a WM_NOTIFY 
      // message.

      return 0;
    }
      
    case TVN_ITEMEXPANDING:
    {
      // Notifies a tree-view control's parent window that a parent
      // item's list of child items is about to expand or collapse.

      return 0;  
    }

    default:
      return 0;    
  }
  break;
}

See Also

Creating a Tree View | Working with Common Controls

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.