Drag and drop into forms

Posted on 2005-05-10
Last Modified: 2008-02-01
Programs like ms word allow u to drag and drop files into the app window then the app wud open the file u dragged into the app. How do u do this using VB6
Question by:schoi_ukuk
    LVL 17

    Expert Comment

    The drop files object is not very documented. Also there are some bugs with using it on Windows 95.  Which O/S will you be using?

    Author Comment

    windows xp pro

    Author Comment

    GRRR I wish they would document things properly make our job much easier
    LVL 8

    Accepted Solution

    I found this code on a long time ago

    into module

    Option Explicit

    ' These are the types that need to be declared for the program to work
      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
    ' These are the Windows API's. These are references to dll's
    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
    ' These are the Constants, which are made public for the whole application
    ' to use
    Public Const PM_NOREMOVE = &H0
    Public Const PM_NOYIELD = &H2
    Public Const PM_REMOVE = &H1
    Public Const WM_DROPFILES = &H233

    Option Explicit

    Private Sub cmdaccept_Click()
    ' You can turn the form's / controls  ability
    ' to accept the files by passing the hWnd as
    ' the first parameter and Ture/False as the
    ' Second
    If cmdaccept.Caption = "&Accept Files" Then
      ' allow the application to accept files
      DragAcceptFiles frmmain.hwnd, True
      cmdaccept.Caption = "&Do Not Accept"
      DragAcceptFiles frmmain.hwnd, False
      cmdaccept.Caption = "&Accept Files"
    End If
    End Sub

    Private Sub Form_Load()
    Visible = True
    lblemail = email
    lblurl = URL
    End Sub

    Public Sub WatchForFiles()
    ' This subrountine watchs for all of your WM_DROPFILES messages
    ' Dim Variables
    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
    ' loop to keep checking for files
    ' NOTE : Do any code you want to execute before this set
      ' check for Dropped file messages
      fileDropped = PeekMessage(FileDropMessage, 0, WM_DROPFILES, WM_DROPFILES, PM_REMOVE Or PM_NOYIELD)
      If fileDropped Then
        ' Get the pointer to the dropped file structure
        hDrop = FileDropMessage.wParam
        ' Get the toal number of files
        numOfDroppedFiles = DragQueryFile(hDrop, True, filename, 127)
        For curFile = 1 To numOfDroppedFiles
          ' Get the file name
          ret = DragQueryFile(hDrop, curFile - 1, filename, 127)
          ' at this pointer you can do what you want with the filename
          ' the filename will be a full qalified path
          lblnumfiles = LTrim$(Str$(numOfDroppedFiles))
          lstfiles.AddItem filename
        Next curFile
        ' We are now done with the structure, tell windows to discard it
        DragFinish hDrop
      End If
    End Sub


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now