Drag and Drop mit Dateien
[Windows 95/98/NT/2000]

03.11.1999


Das nachfolgende Beispiel demonstriert Drag and Drop mit Dateien.
    Beispielsweise, wenn aus dem Windows-Explorer eine oder mehrere Dateien
    auf Ihr Programm "gezogen" werden.

ERFORDERLICHE OBJEKTE
   1 Listbox (List1)
   1 CheckBox (Check1)
 MODUL-CODE
   Public Declare Sub DragAcceptFiles Lib "shell32.dll" _
      (ByVal hWnd As Long, ByVal fAccept As Long)
   Private 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
   Public Declare Function SetWindowLong Lib "user32" Alias _
      "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
      ByVal dwNewLong As Long) As Long
   Private Declare Function CallWindowProc Lib "user32" Alias _
      "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
      ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, 
      ByVal lParam As Long) As Long
   Public Const WM_DROPFILES = &H233
   Public Const GWL_WNDPROC = -4
   Public lProcOld As Long
   Public Function FileDropHandler(ByVal hWnd As Long, _
      ByVal iMsg As Long, ByVal wParam As Long, _
      ByVal lParam As Long) As Long
      Dim filename As String * 128
      If iMsg = WM_DROPFILES Then
         Anzahl = DragQueryFile(wParam, -1, filename, 127)
         Form1.List1.Clear
         For x = 1 To Anzahl
            DragQueryFile wParam, x - 1, filename, 127
            Form1.List1.AddItem (filename)
         Next x
         Exit Function
      End If
      FileDropHandler = CallWindowProc(lProcOld, hWnd, _
      iMsg, wParam, lParam)
   End Function

 FORM-CODE
   Private Declare Function SetWindowPos Lib "user32" _
      (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, 
      ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
      ByVal cy As Long, ByVal wFlags As Long) As Long
   Private Sub Check1_Click()
      If Check1.Value = 1 Then
         Call SetWindowPos(Me.hWnd, -1, 0, 0, 0, 0, 3)
      Else
         Call SetWindowPos(Me.hWnd, -2, 0, 0, 0, 0, 3)
      End If
   End Sub
   Private Sub Form_Load()
      DragAcceptFiles hWnd, True
      lProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf _
         FileDropHandler)
   End Sub
   Private Sub Form_Unload(Cancel As Integer)
      DragAcceptFiles hWnd, False
      SetWindowLong hWnd, GWL_WNDPROC, lProcOld
   End Sub

 

Download - 3 KB