Page 1 of 1

[V2] file2clip() - Adding files to the clipboard

Posted: 07 Apr 2024, 07:14
by arcticir
修改自论坛的 V1 脚本。

Code: Select all

file2clip(list, is_cut?) {
	if !list := Trim(list, "`n`r`t ")
		return

	while !DllCall("OpenClipboard", "UPtr", A_ScriptHwnd)
	{
		if A_Index > 5
			return
		Sleep 50
	}

	list := RegExReplace(list, '\s*\n\s*', '`n')

	; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)

	hPath := DllCall("GlobalAlloc", "uint", 0x42, "uint", 22 + StrPut(list, "UTF-16"), "UPtr")

	; DROPFILES.pFiles = offset of file list
	; fWide = 0 -->ANSI,fWide = 1 -->Unicode

	pPath := NumPut("UInt", 1, NumPut("UInt", 20, DllCall("GlobalLock", "UPtr", hPath, "UPtr")), 12)

	Loop Parse list, "`n"	; Rows are delimited by linefeeds (`r`n).
		pPath += StrPut(A_LoopField, pPath, StrLen(A_LoopField) + 1, "UTF-16")

	DllCall("GlobalUnlock", "UPtr", hPath)

	DllCall("EmptyClipboard")
	DllCall("SetClipboardData", "uint", 0xF, "UPtr", hPath) ; 0xF = CF_HDROP

	; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
	; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)

	mem := DllCall("GlobalAlloc", "uint", 0x42, "uint", 4, "UPtr")
	str := DllCall("GlobalLock", "UPtr", mem)

	DllCall("RtlFillMemory", "UPtr", str, "uint", 1, "UChar", IsSet(is_cut) ? 0x02 : 0x05)

	DllCall("GlobalUnlock", "UPtr", mem)

	static cfFormat := DllCall("RegisterClipboardFormat", "Str", "Preferred DropEffect")

	DllCall("SetClipboardData", "uint", cfFormat, "UPtr", mem)

	DllCall("CloseClipboard")
	return 1
}