Solved

Drag and Drop

Posted on 1999-01-13
9
168 Views
Last Modified: 2008-03-06
How do i make so if i drop a file on a program i made, it automaticaly open it? The program open when i drop something on it, but of course, it doesn't open the file. in what way do i recive the "file parameter" so i can do something with it? i though it should be in the OLEDragDrop and such events of the form, but it doesn't seem to work. all i would like to do, would be to capture the path of the file name that have been droped on my program. Thanks...
0
Comment
Question by:OeilNoir
  • 4
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Check the variable Command$ - it might contain the full path of the file you dropped on your program; you might just have to write some straightforward code to extract it from there.


0
 
LVL 1

Author Comment

by:OeilNoir
Comment Utility
Give more details please, where is the variable command$ is passed? in what events can i get it?
0
 
LVL 4

Accepted Solution

by:
chabaud earned 50 total points
Comment Utility
Here is an sample to get filenames dropped from the explorer.
Create on form and one module.

In Form1:
---------

Option Explicit

Private Sub Form_Load()
    oldWndProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf wndProc)
    DragAcceptFiles Me.hWnd, True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    DragAcceptFiles Me.hWnd, False
    SetWindowLong Me.hWnd, GWL_WNDPROC, oldWndProc
End Sub

In Module1:
-----------

Option Explicit

Public Declare Sub DragAcceptFiles Lib "shell32.dll" (ByVal hWnd As Long, ByVal fAccept 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
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
Private Declare Sub DragFinish Lib "shell32.dll" (ByVal hDrop 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
Private Declare Function DragQueryPoint Lib "shell32.dll" (ByVal hDrop As Long, lpPoint As POINTAPI) As Long

Public Const GWL_WNDPROC = (-4)
Private Const WM_DROPFILES = &H233

Private Type POINTAPI
        x As Long
        y As Long
End Type

Public oldWndProc As Long

Private Function rtrim0(s As String) As String
    rtrim0 = Left(s, lstrlen(s))
End Function
Public Function wndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    If Msg = WM_DROPFILES Then
        Dim hDrop As Long
        Dim n As Long
        Dim i As Integer
        Dim s As String
       
        hDrop = wParam
       
        n = DragQueryFile(hDrop, &HFFFFFFFF, 0, 0)
        Debug.Print "Drop " & n & " files"
       
        For i = 0 To n - 1
            s = Space(144)
            DragQueryFile hDrop, i, s, Len(s)
            Debug.Print "Drop(" & i & ")=[" & rtrim0(s) & "]"
        Next
       
        DragFinish hDrop
        wndProc = 0
    Else
        wndProc = CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam)
    End If
   
End Function


0
 
LVL 1

Author Comment

by:OeilNoir
Comment Utility
isn't there an easier way? i can't belive you need so much code to be able to use the drag and drop. what are the OLEDragDrop events and other of that kind are for?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Hello BlackEye!

Command$ is always available to your VB program, you don't need to pass it.

To check what's in it, just put in a message box somewhere at the start of your program.

MsgBox Command$

Then re-compile, and drop something on your program. If the message box then shows you something that contains, amongst other things, the full path of the file you've just dropped onto your program you'll know that Command$ will work for you.
0
 
LVL 1

Author Comment

by:OeilNoir
Comment Utility
caraf_g hehe you know some french =)
Well.. the command is almost what i want... it could work great, but it's using 8.3 format for file name... :-/ is there a way to have it to use the long name format? Also.. i guess i would have to "parse" to get the path if someone ever call my program with some other parameters...
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Yes,

If it gives you the filename in 8.3 format, don't worry - as long as it gives you the full path. Let's say you extract this full path into a string variable named strFullPath.

If you then use strLongPath = Dir$(strFullPath, vbNormal), strLongPath will contain the long file name.
0
 
LVL 4

Expert Comment

by:chabaud
Comment Utility
There is a difference between
- drag&drop a file from the windows explorer to an icon program (shortcut or not) on the desktop or in an explorer folder

and

- drag&drop some files from the windows explorer to an OPENED program (that is to a running window)

In the first case, just use the command$ function. To test it within VB see menu Project - Properties - Make and fill the "Command line argument"

In the second case, use my previous code (with API calls)
0
 
LVL 1

Author Comment

by:OeilNoir
Comment Utility
Sorry i should have said i wanted to drag and drop on an icon.. and not in the program.. well.. i think you 2 have a good answer...  i'll remake a question to give the points to caraf_g too..

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

10 Experts available now in Live!

Get 1:1 Help Now