I improved it a bit to handle binary data as well:
SetClipboardData(_format, ByRef @data, _dataSize=0, _bEmptyClipboard=true) { local res, mem, str, cfFormat, errorCode errorCode = 0 If (_dataSize = 0) { ; Assume it is a simple string; otherwise, should provide real length... _dataSize := StrLen(@data) If (_dataSize = 0) { errorCode = SIZE Goto SCD_End } } ; Open the clipboard, and empty it. res := DllCall("OpenClipboard", "UInt", 0) If (res = 0) { errorCode = OC Goto SCD_End } If (_bEmptyClipboard) { DllCall("EmptyClipboard") } ; Allocate a global memory object for the text mem := DllCall("GlobalAlloc" , "UInt", 2 ; GMEM_MOVEABLE , "UInt", _dataSize + 1) ; +1 in case it is a zero-terminated string If (mem = 0) { errorCode = GA Goto SCD_End } ; Lock the handle and copy the text to the buffer str := DllCall("GlobalLock", "UInt", mem) ; str is a pointer to a memory area, so is treated as UInt DllCall("RtlMoveMemory" , "UInt", str , "Str", @data , "UInt",_dataSize) ; In case it is a zero-terminated string, we put the final zero DllCall("RtlZeroMemory", "UInt", str + _dataSize, "UInt", 1) DllCall("GlobalUnlock", "UInt", mem) ; Handle format If _format is integer { cfFormat := _format } Else { cfFormat := DllCall("RegisterClipboardFormat", "Str", _format) If (cfFormat = 0) { errorCode = RCF Goto SCD_End } } ; Place the handle on the clipboard res := DllCall("SetClipboardData" , "UInt", cfFormat , "UInt", mem) If (res = 0) { errorCode = SCD Goto SCD_End } SCD_End: If errorCode != 0 { errorCode = %errorCode% (%A_LastError%) } ; Close the clipboard DllCall("CloseClipboard") ErrorLevel := errorCode }You may want to set _bEmptyClipboard to false, for example putting CF_TEXT while emptying the clipboard, then adding CF_RTF to give choice for the target software.
The example uses Hex2Bin from my BinaryEncodingDecoding.ahk file.
#Include BinaryEncodingDecoding.ahk CF_TEXT = 1 CF_UNICODETEXT = 13 CF_RTF = Rich Text Format CF_RTFNOOBJS = Rich Text Format Without Objects CF_DIB = 8 CF_DIBV5 = 17 ; Plain text string1 = The quick brown Fox jumps over the lazy Dog! ; string3 trimed down (just some words underlined/italics/bold) string2 = {\rtf1 The \ul quick\ulnone brown \i Fox\i0 jumps over the lazy \b Dog\b0 !} ; Raw as copied from WordPad (idem with font changed) string3 = {\rtf1\ansi\ansicpg1252\deff0\deflang1036{\fonttbl{\f0\fswiss\fcharset0 Arial`;}}`r`n\uc1\pard\f0\fs20 The \ul quick\ulnone brown \i Fox\i0 jumps over the lazy \b Dog\b0 !}`r`n ; Copied from IrfanView, got, as the other data, with ClipSpy imageHex = ( Join 280000001000000010000000010018000000000000030000130B0000130B0000 00000000000000006EAF6569AA6061A2575FA0545A9B4E5596494D8E404B8C3E 4687384182323C7D2C397A292F711F2D6F1D26681626681674B46A6EAF6569AA 6061A2575FA0545A9B4E5596495091434B8C3E438435418232397A2937782732 74222D6F1D26681676B66C71B1676EAF6569AA6091B3895091445A9B4E559649 5091434D8E406A9C5F6395583576253778273274222D6F1D7EBE7579B96F74B4 6AB7D8B2FCFEFC91B38961A2575A9B4E559649529346FCFEFCFCFEFC35762639 7A2937782734762480C07880C07879B96FB9DAB4FCFEFC91B38961A2575FA054 5C9D51559649FCFEFCFCFEFC3576264384353C7D2C37782789C98380C0787EBE 75BCDDB7FCFEFC91B3896BAC6264A55A5FA0545C9D51FCFEFCFCFEFC3A7B2B46 87384384353C7D2C8CCC8689C98383C37CBEDFBAFCFEFCC7D9C391B3896B9C60 47883A428334FCFEFCFCFEFC3C7E2E4B8C3E46873843843593D38E8CCC8686C6 7FC1E2BEFCFEFCFCFEFCFCFEFCFCFEFCFCFEFCFCFEFCFCFEFCFCFEFC3F803152 93464D8E4048893B9ADA9697D7928CCC86C4E5C1FCFEFCC7D9C3BCDDB7DBECD8 FCFEFCFCFEFCFCFEFCFCFEFC4183345596495596494B8C3E9ADA9697D79293D3 8EC8E9C5FCFEFC91B38980C0787BBB7276B66C71B167FCFEFCFCFEFC4485375C 9D5157984B529346A2E29F9DDD999ADA96C8E9C5FCFEFC91B38986C67F80C078 7BBB7274B46AFCFEFCFCFEFC46883A61A2575FA05457984BAAE9A7A2E29F9DDD 99CBECC9FCFEFC91B38989C98386C67F80C0787BBB72FCFEFCFCFEFC498A3C66 A75D61A2575FA054ADECAAAAE9A7A4E4A1CEEFCCFCFEFCAED0A990D08A89C983 86C67F80C078FCFEFCFCFEFC62A35769AA6069AA6061A257B3F1B1ADECAAA7E7 A4A4E4A1CEEFCC9ADA9693D38E90D08A8CCC8686C67F9FD0999DCD9676B66C71 B1676CAC6269AA60BAF7B7B6F4B4AFEEADAAE9A7A4E4A19FDF9C9ADA9697D792 90D08A89C98386C67F80C0787EBE7579B96F71B1676EAF65BDFABABAF7B7B3F1 B1AFEEADAAE9A7A4E4A19DDD999ADA9697D79290D08A8CCC8686C67F80C0787B BB7279B96F74B46A ) SetClipboardData(CF_TEXT, string1) MsgBox %ErrorLevel% SetClipboardData(CF_RTF, string2, 0, false) MsgBox %ErrorLevel% SetClipboardData(CF_RTF, string3) MsgBox %ErrorLevel% imageLen := Hex2Bin(image, imageHex) SetClipboardData(CF_DIB, image, imageLen) MsgBox %ErrorLevel%
[EDIT] In problem with MSword formatted clipboard result topic, I made a function to get formatted text too.
I just repaste it here for easy retrieval.
GetClipboardData(_format, ByRef @data) { local cfFormat, hData, pData, dataLen If _format is integer { cfFormat := _format } Else { cfFormat := DllCall("RegisterClipboardFormat", "Str", _format, "UInt") If (cfFormat = 0) { Return "BAD_FORMAT" } } If (DllCall("IsClipboardFormatAvailable", "UInt", cfFormat) = 0) { return "NO_DATA" } If (DllCall("OpenClipboard", "UInt", 0) != 0) { hData := DllCall("GetClipboardData", "UInt", cfFormat, "UInt") If (hData != 0) { dataLen := DllCall("GlobalSize", "UInt", hData) pData := DllCall("GlobalLock", "UInt", hData, "UInt") VarSetCapacity(@data, dataLen, 0) ; Might do a lstrcpyW (and lstrlenW) for Unicode format... ;~ r := DllCall("lstrcpy", "Str", @data, "UInt", pData, "UInt") DllCall("RtlMoveMemory" , "UInt", &@data ; Destination , "UInt", pData ; Source , "UInt", dataLen) ; Length DllCall("GlobalUnlock", "UInt", hData) } DllCall("CloseClipboard") } Return dataLen } GetClipboardData(CF_TEXT, str) MsgBox %str% GetClipboardData(CF_RTF, str) MsgBox %str%