Jump to content

File(s) Drag & Drop


Ascend4nt
 Share

Recommended Posts

Send "Drag & Drop File(s)" Message

I was looking for this functionality before, and couldn't figure out the correct method for using the WM_DROPFILES message. Thankfully today I came across the thread ' and found working, but flawed, code for doing exactly this. Specifically, (his 'DoDragDrop' function).

I cleaned up the code, added error checking, Unicode & x64 support, and fixed the Global memory management issues. The result is down below.

Also, see ProgAndy's COM implementation of Drag&Drop: '

Another good reference, besides those in the thread above, is this article at CodeProject: 'How to Implement Drag and Drop Between Your Program and Explorer'

Examples of usage:

TeraCopy

_FileDragDrop(WinGetHandle("[REGEXPTITLE:TeraCopy.*]"),@ScriptFullPath&'|'&@AutoItExe)

Notepad++:

$hNPPlus=WinGetHandle("[CLASS:Notepad++]")
$aNPPPos=WinGetPos($hNPPlus)
;~ WinActivate($hNotePad)
$sProgramFiles=@ProgramFilesDir
If @AutoItX64 Then $sProgramFiles&=' (x86)'
$iRet=_FileDragDrop($hNPPlus,@ScriptFullPath&'|'&$sProgramFiles&"\AutoIt3\Include\Memory.au3",$aNPPPos[2]/2,$aNPPPos[3]/2)
ConsoleWrite("Return: "&$iRet&" @error="&@error&", @extended="&@extended&@CRLF)

Be sure to credit the original authors, or leave the headers intact, if you use the code :graduated:

*edit: for some friggin reason I can't post the code in the post - I get a 'You must enter a post' error message - wtf. Attaching it for now..

UPDATES:

2010-12-4 (yup, today!):

  • Fixed ANSI implementation
  • Changed Memory allocation type to 'Fixed' (thanks trancexx)
  • Added X, Y position parameters - actually needed for some programs (Notepad++ for example)

_FileDragDrop.au3

Edited by Ascend4nt
Link to comment
Share on other sites

Both ways, KaFu. Maybe there's some formatting in the script that the forum doesn't recognize? *shrug*

Link to comment
Share on other sites

Thanks. As far as I can recall, TeraCopy was the only program I needed this for a while back. I think there were other types of Drag-and-Drop messages I wanted to use also, but can't recall for what.

Oh, and for anyone that is curious - no, this doesn't seem to work for Explorer windows, but then that's more of a job for FileCopy().

Link to comment
Share on other sites

trancexx,

For some odd reason I thought it needed a handle to a Global Memory object, but after some tests, it seems it works fine with a pointer instead. Thanks for the help. Updated the post with the new changes, plus ANSI fixes.

UPDATES:

2010-12-4:

  • Fixed ANSI implementation
  • Changed Memory allocation type to 'Fixed' (thanks trancexx)
  • Added X, Y position parameters - actually needed for some programs (Notepad++ for example)
[*edit: added X,Y..] Edited by Ascend4nt
Link to comment
Share on other sites

  • 5 years later...

I'm sorry but I need to push this thread ..
The UDF works fine on most of the programs. But I'm having a hard time getting it to work with Internet Explorer. There are certain websites which accept files to be dragged and dropped into them. For example http://www.mapshaper.org/
I want to add a file using this UDF but I just can't get it to work .. I tried with and without coordinates etc. but still no luck. Googling and using the search function won't help me either ..

Do you guys know how to get this to run? :)

Link to comment
Share on other sites

  • 9 months later...

I'm trying to use this UDF for transferring files from my GUI and its work if target is Notepad (notepad displays text from file), but if target is windows explorer nothing happens - there are no errors, file just wont get copied to folder in the explorer right pane. Is there a something wrong with the code?

Global $b___DraggingInProcess = False, $sFullpathToSomeFile = @TempDir "\qqq.txt"

;---------------------------------------------------------------------------
;GUI Loop :

    Case $GUI_EVENT_PRIMARYUP
        If $b___DraggingInProcess Then
        $b___DraggingInProcess = False
        If FileExists($sFullpathToSomeFile) Then
            Local $tPointStruct = _WinAPI_GetMousePos ()
            Local $hWndDragDropTarget = _WinAPI_WindowFromPoint($tPointStruct)

            Local $aWindows = _WinAPI_EnumWindows()
            For $i = 1 To $aWindows[0][0]
                If _WinAPI_IsChild ($hWndDragDropTarget, $aWindows[$i][0] ) Then
                    $hWndDragDropTarget = $aWindows[$i][0]
                    ConsoleWrite(WinGetTitle($hWndDragDropTarget) & " --> "&$hWndDragDropTarget & @CRLF)
                EndIf
            Next

            $tPointStruct = _WinAPI_GetMousePos (True, $hWndDragDropTarget)
            _FileDragDrop($hWndDragDropTarget,$sFullpathToSomeFile,DllStructGetData($tPointStruct, "x"),DllStructGetData($tPointStruct, "y"))
            ConsoleWrite("@error = "&@error & @CRLF)
        EndIf
    EndIf
;---------------------------------------------------------------------------
;WM_NOTIFY function :

    Case $LVN_BEGINDRAG
        If FileExists($sFullpathToSomeFile) Then
            $b___DraggingInProcess = True
        EndIF
;---------------------------------------------------------------------------

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...