Code: Select all
;-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;EXAMPLE (AUTOHOTKEY UNICODE!)
;Tested on: WIN_7 PRO 32-Bits
;-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Clipboard := "" ;empty clipboard
;set clipboard text
OpenClipboard()
r := SetClipboardText("hello")
CloseClipboard()
MsgBox % r
;get clipboard text
OpenClipboard()
Text := GetClipboardText()
CloseClipboard()
MsgBox % Text
;copy two files to clipboard (try paste it from explorer)
OpenClipboard()
r := SetClipboardText( [ComSpec, A_WinDir "\explorer.exe"],, "Copy")
CloseClipboard()
MsgBox % r
;get clipboard text (the copied files)
OpenClipboard()
Text := GetClipboardText()
CloseClipboard()
MsgBox % Text
;cut file to clipboard
OpenClipboard()
r := SetClipboardText("C:\My_File.txt",, "Cut")
CloseClipboard()
MsgBox % r
;empty clipboard & close
OpenClipboard()
EmptyClipboard()
CloseClipboard()
ExitApp
;-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;FUNCTION
;-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SetClipboardText(Text, Encoding := "CP0", Mode := "Str") {
EmptyClipboard()
if (Mode="Str") { ;Text
hGlobal := GlobalAlloc(Bytes:=StrLen(Text) + 1), hLockGlob := GlobalLock(hGlobal)
, StrPut(Text, hLockGlob+0, Bytes, Encoding), GlobalUnlock(hGlobal)
return r := SetClipboardData(0x1, hGlobal), ErrorLevel := !r
} else if (Mode="Copy") || (Cut:=(Mode="Cut")) { ;Copy | Cut
Files := GetValidArrayFiles(Text,,, Length)
, hGlobal := GlobalAlloc(20 + (Length + Files.MaxIndex() + 1) * 2, 0x42)
, hLockGlob := GlobalLock(hGlobal), NumPut(20, hLockGlob+0, 0, "UInt") ;DROPFILES
, NumPut(1, hLockGlob+0, 16, "UInt"), Offset := 0 ;0 = ANSI | 1 = UNICODE
for k, v in Files
Offset += StrPut(v, (hLockGlob+20)+Offset, StrLen(v)+1, "UTF-16") * 2
GlobalUnlock(hGlobal), r := SetClipboardData(15, hGlobal)
, hGlobal := GlobalAlloc(4, 0x42), hLockGlob := GlobalLock(hGlobal)
, RtlFillMemory(hLockGlob, 1, Cut?0x02:0x05), GlobalUnlock(hGlobal) ;0x02 = cut | 0x05 = copy
, f := SetClipboardData(RegisterClipboardFormat("Preferred DropEffect"), hGlobal)
return f&&r, ErrorLevel := !f||!r
} return false, ErrorLevel := true
}
GetClipboardText() {
Format := GetClipboardFormat()
if (Format.HasKey(15)) { ;Files (CF_HDROP)
Text := GetClipboardData(15), hLockGlob := GlobalLock(Text)
if !(Count:=DllCall("Shell32.dll\DragQueryFileW", "Ptr", hLockGlob, "UInt", 0xFFFFFFFF, "Ptr", 0, "UInt", 0))
return "", ErrorLevel := true
Loop, %Count%
Size := DllCall("Shell32.dll\DragQueryFileW", "Ptr", hLockGlob, "UInt", A_Index-1, "Ptr", 0, "UInt", 0)
, VarSetCapacity(Name, (Size+1) * 2, 0), DllCall("Shell32.dll\DragQueryFileW", "Ptr", hLockGlob
, "UInt", A_Index-1, "Str", Name, "UInt", Size + 1), OutputVar .= Name=""?"":Name "`n"
return RTrim(OutputVar, "`n"), GlobalUnLock(Text), ErrorLevel := OutputVar=""
} else if (Unicode:=Format.HasKey(13)) || (Format.HasKey(1)) || (OEM:=Format.HasKey(7)) ;Text
return StrGet(GetClipboardData(Unicode?13:OEM?7:1), Unicode?"UTF-16":"CP0"), ErrorLevel := false
return "", ErrorLevel := 2
}
OpenClipboard(Wait := 1000, Delay := 50) {
if !DllCall("User32.dll\OpenClipboard", "Ptr", A_ScriptHwnd, "Int") {
Loop
Sleep, %Delay%
until ((r:=DllCall("User32.dll\OpenClipboard", "Ptr", A_ScriptHwnd, "Int"))||(A_Index=Round(Wait/Delay)))
return r
} else return true
}
CloseClipboard() {
return DllCall("User32.dll\CloseClipboard", "Int")
}
EmptyClipboard() {
return DllCall("User32.dll\EmptyClipboard", "Int")
}
SetClipboardData(Format, hMem) {
return DllCall("User32.dll\SetClipboardData", "UInt", Format, "Ptr", hMem, "Ptr")
} ;https://msdn.microsoft.com/en-us/library/windows/desktop/ms649051(v=vs.85).aspx
GetClipboardData(Format) {
return DllCall("User32.dll\GetClipboardData", "UInt", Format, "Ptr")
} ;https://msdn.microsoft.com/en-us/library/windows/desktop/ms649039(v=vs.85).aspx
GetClipboardFormat() {
Format := {}
Loop, Parse, % "2|8|17|5|0x0082|0x008E|0x0083|0x0081|14|0x0300|0x03FF|15|16|3|7|0x0080|9|10|0x0200|0x02FF|11|41|6|13|12", |
if DllCall("User32.dll\IsClipboardFormatAvailable", "UInt", A_LoopField, "Int")
Format[A_LoopField] := A_LoopField
return Format
}
RegisterClipboardFormat(Format) {
return DllCall("User32.dll\RegisterClipboardFormatW", "Ptr", &Format, "UInt")
}
GetValidArrayFiles(Files, Exist := true, Path := "", ByRef Length := "", RemoveDuplicates := true) {
Items := [], Files := IsObject(Files)?GVAF_GetFiles(Files):StrSplit(Files, "`n", "`r"), Length := 0
for k, v in Files
{ if ((Exist && IsFile(Path v)) || !(Exist)) && !((v:=GetFullPathName(GetLongPathName(Path v)))="") && (Ok:=true) {
if (RemoveDuplicates)
for k2, v2 in Items
if (v=v2) && !(Ok:=false)
break
if (Ok)
Items.Push(v), Length += StrLen(v)
}} return Items.MaxIndex()?Items:0, ErrorLevel := Items.MaxIndex()?0:1
} GVAF_GetFiles(Array) {
Items := []
for k, v in Array {
for k2, v2 in StrSplit(v, "`n", "`r")
Items.Push(v2)
} return Items
}
IsFile(Filename) {
return (!InStr(i:=FileExist(Filename),"D")&&i)
}
GetFullPathName(Filename) {
Size := DllCall("Kernel32.dll\GetFullPathNameW", "Str", Filename, "UInt", 0, "Ptr", 0, "PtrP", 0), VarSetCapacity(OutputVar, Size * 2, 0)
, r := DllCall("Kernel32.dll\GetFullPathNameW", "Str", Filename, "UInt", Size, "Str", OutputVar, "PtrP", 0)
return r?RTrim(OutputVar, "\"):Filename, ErrorLevel := !r
}
GetLongPathName(Filename) {
Size := DllCall("Kernel32.dll\GetLongPathNameW", "Str", "\\?\" Filename, "Ptr", 0, "UInt", 0), VarSetCapacity(OutputVar, Size * 2, 0)
, r := DllCall("Kernel32.dll\GetLongPathNameW", "Str", "\\?\" Filename, "Str", OutputVar, "UInt", Size)
return r?LTrim(OutputVar, "\\?\"):Filename, ErrorLevel := !r
}
GlobalLock(hMem) {
return DllCall("Kernel32.dll\GlobalLock", "Ptr", hMem, "Ptr")
} GlobalAlloc(Bytes, Flags := 0x0002) {
return DllCall("Kernel32.dll\GlobalAlloc", "UInt", Flags, "UInt", Bytes, "Ptr")
} GlobalUnlock(hMem) {
return DllCall("Kernel32.dll\GlobalUnlock", "Ptr", hMem, "UInt")
}
RtlFillMemory(ByRef Destination, Bytes, Fill) {
DllCall("ntdll.dll\RtlFillMemory", "Ptr", Destination, "UInt", Bytes, "UChar", Fill)
} ;https://msdn.microsoft.com/en-us/library/windows/hardware/ff561870(v=vs.85).aspx