- ListView_StartDrag: Initializes the drag image for a ListView
- TreeView_StartDrag: Initializes the drag image for a TreeView
- DragComplete: Ends the image dragging
Use ListView_StartDrag and TreeView_StartDrag in the OLEStartDrag event and DragComplete in the OLECompleteDrag event.
Option Explicit ' ==== Private members ==== Private m_lIL As Long Private m_lTimer As Long ' ==== API declarations ==== Private Declare Function ImageList_BeginDrag Lib "comctl32" ( _ ByVal himlTrack As Long, _ ByVal iTrack As Long, _ ByVal dxHotspot As Long, _ ByVal dyHotspot As Long) As Long Private Declare Sub ImageList_EndDrag Lib "comctl32" () Private Declare Function ImageList_DragEnter Lib "comctl32" ( _ ByVal hwndLock As Long, _ ByVal x As Long, _ ByVal y As Long) As Long Private Declare Function ImageList_DragLeave Lib "comctl32" ( _ ByVal hwndLock As Long) As Long Private Declare Function ImageList_DragMove Lib "comctl32" ( _ ByVal x As Long, _ ByVal y As Long) As Long Private Declare Function ImageList_Destroy Lib "comctl32" ( _ ByVal himl As Long) As Long Private Declare Function ImageList_GetImageCount Lib "comctl32" ( _ ByVal himl As Long) As Long Private Type POINTL x As Long y As Long End Type Private Declare Function GetCursorPos Lib "user32" ( _ lpPoint As POINTL) As Long Private Declare Function WindowFromPoint Lib "user32" ( _ ByVal xPoint As Long, _ ByVal yPoint As Long) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _ ByVal hWnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Const LVNI_SELECTED = &H2 Const LVNI_DROPHILITED = &H8 Const LVM_CREATEDRAGIMAGE = &H1000& + 33 Const LVM_GETNEXTITEM = &H1000& + 12 Const TVM_CREATEDRAGIMAGE = &H1100& + 18 Const TVM_GETNEXTITEM = &H1100& + 10 Const TVGN_DROPHILITE = &H8 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function GetWindowRect Lib "user32" ( _ ByVal hWnd As Long, _ lpRect As RECT) As Long Private Declare Function KillTimer Lib "user32" ( _ ByVal hWnd As Long, _ ByVal nIDEvent As Long) As Long Private Declare Function SetTimer Lib "user32" ( _ ByVal hWnd As Long, _ ByVal nIDEvent As Long, _ ByVal uElapse As Long, _ ByVal lpTimerFunc As Long) As Long ' ' DragComplete ' ' Removes the drag image ' Public Sub DragComplete() ' Stop the timer KillTimer 0, m_lTimer ' End the image dragging ImageList_EndDrag ' Destroy the ImageList ImageList_Destroy m_lIL End Sub ' ' ListView_StartDrag ' ' Starts the image dragging ' ' Parameters: ' hWndListView - Window handle of the ListView that ' contains the item to drag ' X, Y - Hot spot coordinates in the image ' Public Sub ListView_StartDrag( _ ByVal hWndListView As Long, _ Optional ByVal x As Long = 20, _ Optional ByVal y As Long = 20) Dim tPoint As POINTL Dim lItem As Long ' Get the selected item lItem = SendMessage(hWndListView, LVM_GETNEXTITEM, -1, ByVal LVNI_SELECTED) ' Get a ImageList with ' the drag image m_lIL = SendMessage(hWndListView, LVM_CREATEDRAGIMAGE, lItem, tPoint) ' Start the image dragging ImageList_BeginDrag m_lIL, 0, x, y ImageList_DragEnter 0, 0, 0 ' Start the timer m_lTimer = SetTimer(0, 0, 1, AddressOf pvTimerDragMove) End Sub Public Sub TreeView_StartDrag( _ ByVal hWndTreeView As Long, _ Optional ByVal x As Long = 20, _ Optional ByVal y As Long = 20) Dim tPoint As POINTL Dim hItem As Long ' Get the selected node hItem = SendMessage(hWndTreeView, TVM_GETNEXTITEM, TVGN_DROPHILITE, ByVal 0&) ' Get a ImageList with the drag image m_lIL = SendMessage(hWndTreeView, TVM_CREATEDRAGIMAGE, 0, ByVal hItem) ' Start the image dragging ImageList_BeginDrag m_lIL, 0, x, y ImageList_DragEnter 0, 0, 0 ' Start the timer m_lTimer = SetTimer(0, 0, 1, AddressOf pvTimerDragMove) End Sub Private Sub pvTimerDragMove( _ ByVal hWnd As Long, _ ByVal uMsg As Long, _ ByVal idEvent As Long, _ ByVal dwTime As Long) Dim tPoint As POINTL ' Get the cursor position GetCursorPos tPoint ' Move the image to the new cursor position ImageList_DragMove tPoint.x, tPoint.y End Sub