Results 1 to 2 of 2

Thread: VB6 - Drag & Drop ListView items to Explorer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    C:\Windows\SysWOW64\
    Posts
    229

    Post VB6 - Drag & Drop ListView items to Explorer

    This sample code provides a reliable, non-API way of dropping items from a ListView control to a physical folder on the disk, using a popup menu.


    The declarations:
    vb Code:
    1. Option Explicit
    2.  
    3. Private sExportedFile As String
    4.  
    5. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    6.  
    7. Private Const KEY_TOGGLED As Long = &H1


    The code:
    vb Code:
    1. Private Sub ListView1_OLEStartDrag(Data As MSComctlLib.DataObject, AllowedEffects As Long)
    2. AllowedEffects = vbDropEffectMove  'We want the file to be moved from its temporary location
    3. Data.SetData vFormat:=vbCFFiles
    4. sExportedFile = vbNullString  'Initialize the variable
    5. End Sub
    6.  
    7. Private Sub ListView1_OLESetData(Data As MSComctlLib.DataObject, DataFormat As Integer)
    8. If Not bDragOutside Then
    9.     'I noticed that this event fires several times and GetKeyState returns -127 or -128 4 times before going outside the form.
    10.     'When the mouse pointer goes outside the form, this event fires constantly and GetKeyState returns 0 or 1 (I don't know why).
    11.     If GetKeyState(KEY_TOGGLED) >= 0 Then
    12.         bDragOutside = True  'We're outside the form; don't let any code run in here again.
    13.         'The drop process is now interrupted...
    14.         Me.PopupMenu mnuFilesExport  '...until a choice is made or the user cancels (clicks somewhere else).
    15.         'Each choice creates a file in a temporary directory and returns the full path into sExportedFile.
    16.         Data.Files.Clear
    17.         If sExportedFile <> vbNullString Then
    18.             Data.Files.Add sExportedFile
    19.            
    20.             bDragOutside = False
    21.         End If
    22.     End If
    23. End If
    24. End Sub
    25.  
    26. Private Sub ListView1_OLECompleteDrag(Effect As Long)
    27. bDragOutside = False
    28.  
    29. If sExportedFile <> vbNullString Then
    30.     'FileExists is a routine I've written, which (obviously) checks if the file exists.
    31.     If FileExists(sExportedFile) Then Kill sExportedFile
    32.     'I "kill" the file because, if the drop operation is canceled, the file remains in its temporary location.
    33. End If
    34. End Sub

    The concept is that we don't know (and don't need) the actual path to which the user wants to drop the item(s).
    The Data.Files collection contains the files which are supposed to be copied (vbDropEffectCopy) or moved (vbDropEffectMove) to the drop location.

    A possible modification for exporting more than one files is to make sExportFile a string array, check if sExportFile(0) = vbNullString, add each member to the data.files collection, and "kill" any files left behind.
    Another modification could be removing the popupmenu (if there is only one option), and directing the program flow to a single routine of yours. The OLESetData event will continue as soon as that routine is finished.


    For the curious people, here is the FileExists routine:
    vb Code:
    1. Public Function FileExists(strPath As String) As Boolean
    2. On Error Resume Next
    3. FileExists = Not (Dir(strPath, vbNormal Or vbReadOnly) = vbNullString)
    4. If Err.Number <> 0 Then FileExists = False
    5. End Function
    Last edited by Cube8; Jul 7th, 2009 at 02:00 AM.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: VB6 - Drag & Drop ListView items to Explorer

    I've wondered how programs like FileZilla work when they do this...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width