drag and drop problem VB6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • albertw
    Contributor
    • Oct 2006
    • 267

    drag and drop problem VB6

    hi

    i've been snooping around on the internet trying to get something usefull for my drag and drop problem.
    i want to get the filename of a dropped file in a listbox.
    now, i have found a source but it does not always work.
    why ? or rather why not?

    the code for module1

    Code:
    Type POINTAPI
      x As Long
      y As Long
    End Type
    
    Type MSG
      hWnd As Long
      message As Long
      wParam As Long
      lParam As Long
      time As Long
      pt As POINTAPI
    End Type
    
    Declare Sub DragAcceptFiles Lib "shell32.dll" (ByVal hWnd As Long, ByVal fAccept As Long)
    Declare Sub DragFinish Lib "shell32.dll" (ByVal hDrop As Long)
    Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal hDrop As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long
    Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    
    Public Const PM_NOREMOVE = &H0
    Public Const PM_NOYIELD = &H2
    Public Const PM_REMOVE = &H1
    Public Const WM_DROPFILES = &H233
    1 Form: frmMain
    2 buttons: cmdClear and cmdExit
    1 listbox: lstFiles
    1 label: lblNumFiles
    1 label: lblInfo ["Number of dropped files"]

    Code:
    Dim FileDropMessage As MSG      ' Msg Type
    Dim FileDropped As Boolean      ' True if Files where dropped
    Dim hDrop As Long               ' Pointer to the dropped file structure
    Dim FileName As String * 128    ' the dropped filename
    Dim numOfDroppedFiles As Long   ' the amount of dropped files
    Dim curFile As Long             ' the current file number
    Dim ret As Long
    
    Private Sub cmdclear_Click()
    lstFiles.Clear
    lblNumFiles.Caption = "0"
    End Sub
    
    Private Sub cmdexit_Click()
    Unload Me
    End
    End Sub
    
    Private Sub Form_Load()
    
    frmmain.Show
    DragAcceptFiles frmmain.hWnd, True
    
    Do
      If PeekMessage(FileDropMessage, 0, WM_DROPFILES, WM_DROPFILES, PM_REMOVE Or PM_NOYIELD) = True Then
        hDrop = FileDropMessage.wParam
        numOfDroppedFiles = DragQueryFile(hDrop, True, FileName, 127)
        For curFile = 1 To numOfDroppedFiles
          ret = DragQueryFile(hDrop, curFile - 1, FileName, 127)
          lblNumFiles.Caption = LTrim(Str(numOfDroppedFiles))
          lstFiles.AddItem FileName
        Next curFile
        DragFinish hDrop
      End If
    DoEvents
    Loop
    
    End Sub
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Would it be acceptable to drop the file onto another control, say a textbox, and have your code transfer it to the listbox?

    Anyway, here's a sample I had lying around which may be some help. The files are dated 2001, I don't recall where it came from - picked it up on the web somewhere, I guess. It's just a project with a single form with the default name Form1. Here's the listing. If you want it, just create Form1, go to the code window, select all, then paste this in...
    Code:
    Option Explicit
    DefLng A-Z
    
    Private Sub Text1_OLEDragOver(Data As _
        DataObject, Effect As Long, Button As Integer, _
        Shift As Integer, X As Single, Y As Single, State _
        As Integer)
      If Data.GetFormat(vbCFFiles) Then
        'If the data is in the proper format, _
        inform the source of the action to be taken
        Effect = vbDropEffectCopy And Effect
        Exit Sub
      End If
      'If the data is not desired format, no drop
      Effect = vbDropEffectNone
    End Sub
    
    Private Sub Text1_OLEDragDrop(Data As _
          DataObject, Effect As Long, Button As Integer, _
          Shift As Integer, X As Single, Y As Single)
      If Data.GetFormat(vbCFFiles) Then
        Dim vFN As Variant
    
        For Each vFN In Data.Files
          'DropFile Text1, vFN
          Text1 = Text1 & vbCrLf & vFN
        Next vFN
      End If
    End Sub
    
    Sub DropFile(ByVal txt As TextBox, ByVal strFN$)
      Dim iFile As Integer
      iFile = FreeFile
    
      Open strFN For Input Access Read Lock Write As #iFile
      Dim Str$, strLine$
      While Not EOF(iFile) And Len(Str) <= 32000
        Line Input #iFile, strLine$
        If Str <> "" Then Str = Str & vbCrLf
        Str = Str & strLine
      Wend
      Close #iFile
    
      txt.SelStart = Len(txt)
      txt.SelLength = 0
      txt.SelText = Str
    
    End Sub

    Comment

    • albertw
      Contributor
      • Oct 2006
      • 267

      #3
      hi
      tnx a lot, this one works indeed.

      Comment

      • DonBurns
        New Member
        • Aug 2007
        • 1

        #4
        I just wanted to thank Killer42 for solving this same problem for me. I needed to have users drag files from Window explorer onto a form and attach them as documents based on their types. Your solution enabled me to do this.

        Sincerely,
        Don

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by DonBurns
          I just wanted to thank Killer42 for solving this same problem for me. I needed to have users drag files from Window explorer onto a form and attach them as documents based on their types. Your solution enabled me to do this.
          No problem, Don. Glad we could help. :)

          Comment

          Working...