VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 拖动时显示 ListView 项目
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:11307, 日期:2001-05-26
拖动时显示 ListView 项目

  这段代码演示了如何利用 ImageList 函数在拖动 ListView 项目的时候显示被拖动的项目图标(就像在资源管理器中所看到的那样)。

Option Explicit

注释: ==== Private members ====
Private m_lIL As Long
Private m_lWndPrev 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 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 LVM_CREATEDRAGIMAGE = &H1000& + 33

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 ListViewDragComplete()
   
   注释: Stop the timer
   KillTimer 0, m_lTimer

   注释: End the image dragging
   ImageList_EndDrag
   
   注释: Destroy the ImageList
   ImageList_Destroy m_lIL
   
End Sub

注释:
注释: ListViewStartDrag
注释:
注释: Starts the image dragging
注释:
注释: Parameters:
注释: hWndListView - Window handle of the ListView that
注释:                contains the item to drag
注释: ItemIndex - Index of the item to drag
注释: X, Y - Hot spot coordinates in the image
注释:
Public Sub ListViewStartDrag( _
   ByVal hWndListView As Long, _
   ByVal ItemIndex As Long, _
   Optional ByVal X As Long = 20, _
   Optional ByVal Y As Long = 20)
Dim tPoint As POINTL

   注释: Get a ImageList with
   注释: the drag image
   m_lIL = SendMessage(hWndListView, LVM_CREATEDRAGIMAGE, ItemIndex - 1, tPoint)
   
   注释: Start the image dragging
   ImageList_BeginDrag m_lIL, 0, X, Y
   
   注释: Start the timer
   m_lTimer = SetTimer(0, 0, 1, AddressOf TimerDragMove)

End Sub

Private Sub TimerDragMove( _
   ByVal hWnd As Long, _
   ByVal uMsg As Long, _
   ByVal idEvent As Long, _
   ByVal dwTime As Long)
Dim tPoint As POINTL
Dim tRect As RECT
Dim lWnd As Long
   
   注释: Get the cursor position
   GetCursorPos tPoint
   
   注释: Get the window under
   注释: the cursor
   lWnd = WindowFromPoint(tPoint.X, tPoint.Y)
   
   注释: Check if the cursor has moved
   If lWnd <>  m_lWndPrev Then
   
      注释: Remove the image from
      注释: the previous window
      ImageList_DragLeave m_lWndPrev
      
      注释: Add the image to
      注释: the new window
      ImageList_DragEnter lWnd, 0, 0
      
      注释: Save the current window
      m_lWndPrev = lWnd
      
   End If
   
   注释: Get the window rectangle
   GetWindowRect lWnd, tRect
   
   注释: Convert the screen coordinates
   注释: to window coordinates
   tPoint.X = tPoint.X - tRect.Left
   tPoint.Y = tPoint.Y - tRect.Top
   
   注释: Move the image
   ImageList_DragMove tPoint.X, tPoint.Y
   
End Sub
在 OLEStartDrag 事件代码中调用 ListViewStartDrag:


Private Sub ListView1_OLEStartDrag( _
   Data As ComctlLib.DataObject, _
   AllowedEffects As Long)
   
   ListViewStartDrag ListView1.hWnd, ListView1.SelectedItem.Index

End Sub
同时在 OLECompleteDrag 事件代码中调用 ListViewDragComplete:


Private Sub ListView1_OLECompleteDrag(Effect As Long)
   
   ListViewDragComplete
   
End Sub