Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Make AHK drop files into other applications


  • Please log in to reply
29 replies to this topic
SalchichaPicante
  • Members
  • 1 posts
  • Last active: Jul 02 2009 12:30 PM
  • Joined: 05 Jun 2009
[ Moderator!: Topic split from Make AHK drop files into other applications ]

I found a working function on a Japanese page (Google translation is my friend :) ).
I edited it a little, so I think it's simpler to use.

Run, mspaint.exe

Sleep, 1000
; Show an error message because this isn't a picture file
PostMessage, 0x233, HDrop(A_MyDocuments), 0,, ahk_class MSPaintApp
ExitApp

/*
Return a handle to a structure describing files to be droped.
Use it with PostMessage to send WM_DROPFILES messages to windows.

fnames is a list of paths delimited by `n or `r`n
x and y are the coordinates where files are droped in the window.

Eg. :
; Open autoexec.bat in an existing Notepad window.
PostMessage, 0x233, HDrop("C:\autoexec.bat"), 0,, ahk_class Notepad
*/

HDrop(fnames,x=0,y=0) {
   fns:=RegExReplace(fnames,"\n$")
   fns:=RegExReplace(fns,"^\n")
   hDrop:=DllCall("GlobalAlloc","UInt",0x42,"UInt",20+StrLen(fns)+2)
   p:=DllCall("GlobalLock","UInt",hDrop)
   NumPut(20, p+0)  ;offset
   NumPut(x,  p+4)  ;pt.x
   NumPut(y,  p+8)  ;pt.y
   NumPut(0,  p+12) ;fNC
   NumPut(0,  p+16) ;fWide
   p2:=p+20
   Loop,Parse,fns,`n,`r
   {
      DllCall("RtlMoveMemory","UInt",p2,"Str",A_LoopField,"UInt",StrLen(A_LoopField))
      p2+=StrLen(A_LoopField)+1
   }
   DllCall("GlobalUnlock","UInt",hDrop)
   Return hDrop
}
Thanks to lukewarm, the script author.

The original function: http://lukewarm.s101...ts/DragDrop.zip
Scripts by lukewarm (Japanese): http://lukewarm.s101...a.com/myscripts

\o/
I will show you my signature "when it's done" :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I found a working function on a Japanese page (Google translation is my friend :) ).


Nice find.. Thanks. I guess lukewarm adapted the code from Code Project Article : How to Implement Drag and Drop Between Your Program and Explorer. WM_DROPFILES > DROPFILES Structure requires the list of files delimited with NULL and the string be double terminated with a pair of NULLS. To achieve this, the presented code loop parses and copies each file name into g-allocated memory calling RtlMoveMemory() as many times. Since I was uncomfortable with this method, I've written a variant that accepts linefeed-delimited file list and uses InStr() + Numput() combo to replace all LINEFEED with NULL character. ( Refer topic : Search and Replace a character with null )

DropFilesA( FileList, wTitle="", Ctrl="", X=0, Y=0, NCA=0 ) {
 StringReplace, FileList, FileList, `r`n, `n , All
 VarSetCapacity( DROPFILES,20,32 ),  DROPFILES.=FileList "`n`n",  nSize:=StrLen(DROPFILES)
 StringReplace, DROPFILES,DROPFILES, `n,`n, UseErrorLevel
 Loop %ErrorLevel%
   NumPut( 0, DROPFILES, InStr(DROPFILES,"`n",0,0)-1, "Char" )
 pDP:=&DROPFILES,  NumPut(20,pDP+0), NumPut(X,pDP+4), NumPut(Y,pDP+8), NumPut(NCA,pDP+12)
 NumPut(0,pDP+16), hDrop := DllCall( "GlobalAlloc", UInt,0x42, UInt,nSize )
 pData := DllCall( "GlobalLock", UInt,hDrop )
 DllCall( "RtlMoveMemory", UInt,pData, UInt,pDP, UInt,nSize )
 DllCall( "GlobalUnlock", UInt,hDrop )
 PostMessage, 0x233, hDrop, 0, %Ctrl%, %wTitle% ; WM_DROPFILES := 0x233
}
DropFilesA( "C:\SomeName.txt", "ahk_class Notepad" ) ; usage Example

BTW, WM_DROPFILES is Windows 3.1 implementation and does not work with Internet/Windows Explorer etc. For COM implementation, please refer Sean's code and links in his post: Drop files simulation

fizz
  • Guests
  • Last active:
  • Joined: --
This is great although I fear I will never fully understand it no matter how many times I read the documentation on MS.

Is there a way to also add dragging to the script or a way to drop directly to a control? I've been trying to get it to do so but post message does not seem to work that way.

Any help is appreciated.

fizz
  • Guests
  • Last active:
  • Joined: --
Perhaps I was wrong about not being able to understand this. I just specified the control HWND var in WinTitle and IT WORKED!

I still am in disbelief. Got to take some time to let it all sink in :D :D

fizz
  • Guests
  • Last active:
  • Joined: --
Sorry for so many posts but THANK YOU THANK YOU THANK YOU THANK YOU for this thread!


did I say THANK YOU?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
You are welcome. :)

fizz
  • Guests
  • Last active:
  • Joined: --
Now if I could just figure out the 1st part of the process, the click - file under mouse - hold until release, I would be able to simulate the whole process.

Posted Image

Any idea of how the filename would be determined at time of click?

Thanks for any advice.

art
  • Members
  • 57 posts
  • Last active: Mar 11 2019 07:48 AM
  • Joined: 23 Sep 2008
THANK YOU for this thread!

How can we simulate drag of a string or a path\filename so to allow user to manually choose (by mouse click) the window/ctrl to drop it?

thanks

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
Am I to understand that this will not work for all applications like a standard drag and drop? It seems to work for some but others fail.

Any info would be great...

Posted Image

don't duplicate, iterate!


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

BTW, WM_DROPFILES is Windows 3.1 implementation and does not work with Internet/Windows Explorer etc. For COM implementation, please refer Sean's code and links in his post: Drop files simulation



TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
Thanx skan missed that tid bit.

Posted Image

don't duplicate, iterate!


automaticman
  • Members
  • 658 posts
  • Last active: Nov 20 2012 06:10 PM
  • Joined: 27 Oct 2006

DropFilesA( "C:\SomeName.txt", "ahk_class Notepad" ) ; usage Example

Would this also work with Ctrl pressed at the same time? Meaning how would we write it if we want to simulate a ctrl+drag and drop of multiple files?

Or control+pasting would be also enough for my purposes. Ctrl+v I can not use for pasting, as I need also the additional Ctrl, any ideas? I need something like Ctrl+Ctrl+v.

wakewatcher
  • Members
  • 254 posts
  • Last active: Oct 04 2011 10:03 PM
  • Joined: 15 Jul 2006
I have been using this for some time now with eRightSoft's free SUPER video converter/joiner and it works great. I've now tried it with Koyotesoft.com "free Video Converter V2.5.0.0" and "Videos To DVD Converter V3.2" and can't get them to work. I want to make sure I'm doing it correctly. For setup I start one of those programs and then start the autoit window spy to get the ahk class for the free converter that shows an ahk class named "WindowsForms10.Window.8.app.0.378734a" (?) and the DVD converter is "ThunderRT6FormDC" (fyi for SUPER its "TffmpegW") Anyway replacing the SUPER ahk class with either of these doesn't work. Doesn't crash but no files are moved into the file area. (I can manually move the files a group at a time but they get rearranged so I use this utility (or are trying to) to order the drag.) Thanks in advance for any help.

Zizou
  • Members
  • 32 posts
  • Last active: Oct 31 2017 05:51 AM
  • Joined: 16 Nov 2008
@SKAN
If works just fabulous; wonderful!

Thanks, man.

AmourSpirit
  • Members
  • 93 posts
  • Last active: Mar 01 2016 04:28 PM
  • Joined: 01 Dec 2010

I found a working function on a Japanese page (Google translation is my friend :) ).
I edited it a little, so I think it's simpler to use...


I tried your script in the simplest of forms and I can not get it to work.
I created C:\test.txt and put a little sample text in it.
Notepad starts and then I just get an error with I run this simple test ( The system could not find the file specified ).
I tested other files with the same results as well. I can assure you the file does exist on the root of my C drive.
I tried this on both win 7 x86 and x64 machines with the same result.

I am using Version v1.1.05.04 of AutoHotkey L
Any suggestions?

Run, notepad.exe
Sleep, 1000
PostMessage, 0x233, HDrop("C:\test.txt"), 0,, ahk_class Notepad
ExitApp

HDrop(fnames,x=0,y=0) {
   fns:=RegExReplace(fnames,"\n$")
   fns:=RegExReplace(fns,"^\n")
   hDrop:=DllCall("GlobalAlloc","UInt",0x42,"UInt",20+StrLen(fns)+2)
   p:=DllCall("GlobalLock","UInt",hDrop)
   NumPut(20, p+0)  ;offset
   NumPut(x,  p+4)  ;pt.x
   NumPut(y,  p+8)  ;pt.y
   NumPut(0,  p+12) ;fNC
   NumPut(0,  p+16) ;fWide
   p2:=p+20
   Loop,Parse,fns,`n,`r
   {
      DllCall("RtlMoveMemory","UInt",p2,"Str",A_LoopField,"UInt",StrLen(A_LoopField))
      p2+=StrLen(A_LoopField)+1
   }
   DllCall("GlobalUnlock","UInt",hDrop)
   Return hDrop
}