Page 1 of 1

Drag and Drop a file into another window

Posted: 16 Sep 2019, 01:23
by Enlprck
Hey everyone!

I have simple A.txt file and I would like to drag & drop it into another window. How can I do that ? Thanks for any help.


Let me explain some of my searches:
https://autohotkey.com/board/topic/41467-make-ahk-drop-files-into-other-applications/page-2
https://www.autohotkey.com/boards/viewtopic.php?f=5&t=38670


Code: Select all

DropFiles( FileList, wTitle="", Ctrl="", X=200, Y=200, NCA=0 ) 
{
 characterSize := A_IsUnicode ? 2 : 1
 StringReplace, FileList, FileList, `r`n, `n , All
 VarSetCapacity( DROPFILES,20,32 ),  DROPFILES.=FileList "`n`n",  nSize:=StrLen(DROPFILES)*characterSize
 StringReplace, DROPFILES,DROPFILES, `n,`n, UseErrorLevel
 Loop %ErrorLevel%
	NumPut( 0, DROPFILES, InStr(DROPFILES,"`n",0,0)*characterSize - characterSize, A_IsUnicode ? "Short" : "Char" )

 pDP := &DROPFILES
 NumPut(20, pDP+0)
 NumPut(X, pDP+4)
 NumPut(Y, pDP+8)
 NumPut(NCA, pDP+12)
 NumPut(A_IsUnicode ? 1 : 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
}
HDrop(fnames,x=0,y=0) 
{
 	characterSize := A_IsUnicode ? 2 : 1
   fns:=RegExReplace(fnames,"\n$")
   fns:=RegExReplace(fns,"^\n")
   hDrop:=DllCall("GlobalAlloc","UInt",0x42,"UInt",20+(StrLen(fns)*characterSize)+characterSize*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(A_IsUnicode ? 1 : 0,  p+16) ;fWide

   p2:=p+20
   Loop,Parse,fns,`n,`r
   {
      DllCall("RtlMoveMemory","UInt",p2,"Str",A_LoopField,"UInt",StrLen(A_LoopField)*characterSize)
      p2+=StrLen(A_LoopField)*characterSize + characterSize
   }
   DllCall("GlobalUnlock","UInt",hDrop)
   Return hDrop
}

PostMessage, 0x233, HDrop("C:\A.txt"), 0,, ahk_class Notepad

Code: Select all

SetTitleMatchMode, 2

list_files:={} ;define array name

data=
(
C:\A.txt
)


loop,parse,data,`n,`r
list_files[A_Index]:=A_LoopField


DropFiles("ahk_class Notepad", list_files)

DropFiles(window, files)
{
  for k,v in files
    memRequired+=StrLen(v)+1
  hGlobal := DllCall("GlobalAlloc", "uint", 0x42, "ptr", memRequired+21)
  dropfiles := DllCall("GlobalLock", "ptr", hGlobal)
  NumPut(offset := 20, dropfiles+0, 0, "uint")
  for k,v in files
    StrPut(v, dropfiles+offset, "utf-8"), offset+=StrLen(v)+1
  DllCall("GlobalUnlock", "ptr", hGlobal)
  PostMessage, 0x233, hGlobal, 0,, %window%
  if ErrorLevel
    DllCall("GlobalFree", "ptr", hGlobal)
}


Both code just work perfect but only for Notepad window. Whenever I am trying to change it to "ahk_class X ( X the window that I wanted to drop it )" code is not work for me. Most probably it's about to droping into specific coordinate ( like X=100 Y=100) arena into that Window. However when I also changed X,Y coordinate that include into these codes it still not work for me. Any solution to do it?

Please help me and it's really importand for me, I have been searching for it like two weeks. I have no idea how can I do it. I don't know how to write codes. Thank you all!

Re: Drag and Drop a file into another window

Posted: 16 Sep 2019, 02:30
by divanebaba
Hi.
The ahk_class must not be identical with window title, according to my knowledge.
Try to find ahk_class with Window Spy.
You can find WindowSpy in your AutoHotkey-install-folder.

Re: Drag and Drop a file into another window

Posted: 16 Sep 2019, 04:17
by Enlprck
divanebaba wrote:
16 Sep 2019, 02:30
Hi.
The ahk_class must not be identical with window title, according to my knowledge.
Try to find ahk_class with Window Spy.
You can find WindowSpy in your AutoHotkey-install-folder.

Thanks for respond,

Yes I have knowledge about windows spy. And I am finding my X windows from there. So It cannot be wrong in my opinion. Notepad is just brandnew blank notepad that's why it's ahk_class Notepad. There is something that we are missing but I am not sure what is it.

I changed DropFiles( FileList, wTitle="", Ctrl="", X=0, Y=0, NCA=0 ) to X=200, Y=200 as you can see but still doesn't work for me :/

Thank you!

Re: Drag and Drop a file into another window

Posted: 16 Sep 2019, 06:24
by divanebaba
Hi.
I've used second code and modified it for Notepad++ by changing only the ahk_class name from Notepad to Notepad++ and it worked as desired.

Code: Select all

DropFiles("ahk_class Notepad++", list_files)
Are you really sure, that your choosen ahk_class was accurate?

Re: Drag and Drop a file into another window

Posted: 16 Sep 2019, 09:05
by Enlprck
divanebaba wrote:
16 Sep 2019, 06:24
Hi.
I've used second code and modified it for Notepad++ by changing only the ahk_class name from Notepad to Notepad++ and it worked as desired.

Code: Select all

DropFiles("ahk_class Notepad++", list_files)
Are you really sure, that your choosen ahk_class was accurate?
Yes sir, I am 100% sure that It is correct ahk_class.

Save A.txt file (put some simple text into it and save it) to anywhere and try to use that code to let it appear into Chrome. You can try to this and you will see that it's not working for example for "ahk_class Chrome_WidgetWin_1".
It's work perfectly for Notepad or Notepad++ but not for something else. And I have not idea.

When you manually drag & drop a text file onto chrome it's work.
When you try it with code it's not work.
I have no idea what should I do.

Appreciate for your help.

Re: Drag and Drop a file into another window

Posted: 19 Sep 2019, 03:21
by divanebaba
Hi.
I've tested with Chrome and you're right, it is not working.
Sorry, I do not know how to fix :mrgreen:
Nice greetings from Germany :mrgreen:

Re: Drag and Drop a file into another window

Posted: 19 Sep 2019, 10:28
by Enlprck
divanebaba wrote:
19 Sep 2019, 03:21
Hi.
I've tested with Chrome and you're right, it is not working.
Sorry, I do not know how to fix :mrgreen:
Nice greetings from Germany :mrgreen:
Hey, thank you for respond.

Appreciate for your help again.

Does anyone know how can we fix it? It's really importand for me :/
Thanks!

Re: Drag and Drop a file into another window

Posted: 20 Sep 2019, 12:33
by Flipeador