模拟鼠标托文件到程序的程序,名字含有中文应该怎么处理 Topic is solved

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

dirtyacc
Posts: 10
Joined: 16 Oct 2019, 01:19

模拟鼠标托文件到程序的程序,名字含有中文应该怎么处理

20 Oct 2019, 03:11

Code: Select all

/*

DropFiles:
combined from two functions - ideally they should be merged into one "smarter" 
function but this will do for now.
 
- SKAN - http://www.autohotkey.com/board/topic/41467-make-ahk-drop-files-into-other-applications/#entry258810
- nimda - http://www.autohotkey.com/board/topic/79145-help-converting-ahk-ahk-l/#entry502676

Example:

DropFiles( "C:\SomeName.txt", "ahk_class Notepad" ) 

*/

DropFiles( FileList, wTitle="", Ctrl="", X=0, Y=0, NCA=0 ) {
	If (A_IsUnicode = 1)
		{ ; from nimda - http://www.autohotkey.com/board/topic/79145-help-converting-ahk-ahk-l/#entry502676
		   fns:=RegExReplace(FileList,"\n$") 
		   fns:=RegExReplace(fns,"^\n") 
		   hDrop:=DllCall("GlobalAlloc","UInt",0x42,"UPtr",20+StrLen(fns)+2) 
		   p:=DllCall("GlobalLock","UPtr",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","UPtr",p2,"AStr",A_LoopField,"UPtr",StrLen(A_LoopField)) 
			  p2+=StrLen(A_LoopField)+1 
		   } 
		   DllCall("GlobalUnlock","UPtr",hDrop) 
		   PostMessage, 0x233, hDrop, 0, %Ctrl%, %wTitle%
		   }
	 Else If (A_IsUnicode <> 1)
		{ ; from DropFilesA - SKAN - http://www.autohotkey.com/board/topic/41467-make-ahk-drop-files-into-other-applications/#entry258810
		 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%
		} 
	}
DropFiles( "D:\test.txt",  "ahk_class Notepad"  ) 
如果文件名是test.txt这样的纯ASCII名字没问题,但名字里面带中文就不行了,应该怎么处理?
dirtyacc
Posts: 10
Joined: 16 Oct 2019, 01:19

Re: 模拟鼠标托文件到程序的程序,名字含有中文应该怎么处理

20 Oct 2019, 09:18

我测试一下,应该是strlen长度的问题。但我不会改,英文字母那个DLLCall长度用Strlen得出来的,中文每个字长度多一倍,所以就不正确了。请前辈指教一下
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: 模拟鼠标托文件到程序的程序,名字含有中文应该怎么处理  Topic is solved

21 Oct 2019, 03:33

@dirtyacc

btw 標題應該改成拖入文件

Code: Select all

; F1 將指定文件模擬拖入打開的 notepad++ 中
; 依照你的代碼加上參考下面這個網址 RHCP 的 post 改了幾行代碼
; https://autohotkey.com/board/topic/41467-make-ahk-drop-files-into-other-applications/?p=673038

DropFiles( FileList, wTitle="", Ctrl="", X=0, Y=0, NCA=0 ) {
    characterSize := A_IsUnicode ? 2 : 1
    fns:=RegExReplace(FileList,"\n$")
    fns:=RegExReplace(fns,"^\n")
    hDrop:=DllCall("GlobalAlloc","UInt",0x42,"UInt",20+(StrLen(fns)*characterSize)+characterSize*2)

    p:=DllCall("GlobalLock","UPtr",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
    NumPut(A_IsUnicode ? 1 : 0,  p+16) ;fWide

    p2:=p+20
    Loop,Parse,fns,`n,`r
    {
        DllCall("RtlMoveMemory","UPtr",p2,"Str",A_LoopField,"UPtr",StrLen(A_LoopField)*characterSize)
            p2+=StrLen(A_LoopField)*characterSize + characterSize
        }
    DllCall("GlobalUnlock","UPtr",hDrop)
    PostMessage, 0x233, hDrop, 0, %Ctrl%, %wTitle%
}

; 拖入多個文件
filepath = C:\Users\%A_Username%\Downloads\drop中文檔名測試文件.txt `nC:\Users\%A_Username%\Downloads\test2.txt

F1::DropFiles( filepath,  "ahk_class Notepad++"  )

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 45 guests