Clipboard_En.au3 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #include-once
  2. #include "Memory.au3"
  3. ; #INDEX# =======================================================================================================================
  4. ; Title .........: Clipboard
  5. ; AutoIt Version : 3.3.14.5
  6. ; Language ......: English
  7. ; Description ...: Functions that assist with Clipboard management.
  8. ; The clipboard is a set of functions and messages that enable applications to transfer data.
  9. ; Because all applications have access to the clipboard, data can be easily transferred
  10. ; between applications or within an application.
  11. ; Author(s) .....: Paul Campbell (PaulIA)
  12. ; ===============================================================================================================================
  13. ; #CONSTANTS# ===================================================================================================================
  14. Global Const $CF_TEXT = 1 ; Text format
  15. Global Const $CF_BITMAP = 2 ; Handle to a bitmap (HBITMAP)
  16. Global Const $CF_METAFILEPICT = 3 ; Handle to a metafile picture (METAFILEPICT)
  17. Global Const $CF_SYLK = 4 ; Microsoft Symbolic Link (SYLK) format
  18. Global Const $CF_DIF = 5 ; Software Arts' Data Interchange Format
  19. Global Const $CF_TIFF = 6 ; Tagged image file format
  20. Global Const $CF_OEMTEXT = 7 ; Text format containing characters in the OEM character set
  21. Global Const $CF_DIB = 8 ; BITMAPINFO structure followed by the bitmap bits
  22. Global Const $CF_PALETTE = 9 ; Handle to a color palette
  23. Global Const $CF_PENDATA = 10 ; Data for the pen extensions to Pen Computing
  24. Global Const $CF_RIFF = 11 ; Represents audio data in RIFF format
  25. Global Const $CF_WAVE = 12 ; Represents audio data in WAVE format
  26. Global Const $CF_UNICODETEXT = 13 ; Unicode text format
  27. Global Const $CF_ENHMETAFILE = 14 ; Handle to an enhanced metafile (HENHMETAFILE)
  28. Global Const $CF_HDROP = 15 ; Handle to type HDROP that identifies a list of files
  29. Global Const $CF_LOCALE = 16 ; Handle to the locale identifier associated with text in the clipboard
  30. Global Const $CF_DIBV5 = 17 ; BITMAPV5HEADER structure followed by bitmap color and the bitmap bits
  31. Global Const $CF_OWNERDISPLAY = 0x0080 ; Owner display format
  32. Global Const $CF_DSPTEXT = 0x0081 ; Text display format associated with a private format
  33. Global Const $CF_DSPBITMAP = 0x0082 ; Bitmap display format associated with a private format
  34. Global Const $CF_DSPMETAFILEPICT = 0x0083 ; Metafile picture display format associated with a private format
  35. Global Const $CF_DSPENHMETAFILE = 0x008E ; Enhanced metafile display format associated with a private format
  36. Global Const $CF_PRIVATEFIRST = 0x0200 ; Range of integer values for private clipboard formats
  37. Global Const $CF_PRIVATELAST = 0x02FF ; Range of integer values for private clipboard formats
  38. Global Const $CF_GDIOBJFIRST = 0x0300 ; Range for (GDI) object clipboard formats
  39. Global Const $CF_GDIOBJLAST = 0x03FF ; Range for (GDI) object clipboard formats
  40. ; ===============================================================================================================================
  41. ; #CURRENT# =====================================================================================================================
  42. ; _ClipBoard_ChangeChain
  43. ; _ClipBoard_Close
  44. ; _ClipBoard_CountFormats
  45. ; _ClipBoard_Empty
  46. ; _ClipBoard_EnumFormats
  47. ; _ClipBoard_FormatStr
  48. ; _ClipBoard_GetData
  49. ; _ClipBoard_GetDataEx
  50. ; _ClipBoard_GetFormatName
  51. ; _ClipBoard_GetOpenWindow
  52. ; _ClipBoard_GetOwner
  53. ; _ClipBoard_GetPriorityFormat
  54. ; _ClipBoard_GetSequenceNumber
  55. ; _ClipBoard_GetViewer
  56. ; _ClipBoard_IsFormatAvailable
  57. ; _ClipBoard_Open
  58. ; _ClipBoard_RegisterFormat
  59. ; _ClipBoard_SetData
  60. ; _ClipBoard_SetDataEx
  61. ; _ClipBoard_SetViewer
  62. ; ===============================================================================================================================
  63. ; #FUNCTION# ====================================================================================================================
  64. ; Author ........: Paul Campbell (PaulIA)
  65. ; Modified.......:
  66. ; ===============================================================================================================================
  67. Func _ClipBoard_ChangeChain($hRemove, $hNewNext)
  68. DllCall("user32.dll", "bool", "ChangeClipboardChain", "hwnd", $hRemove, "hwnd", $hNewNext)
  69. If @error Then Return SetError(@error, @extended)
  70. EndFunc ;==>_ClipBoard_ChangeChain
  71. ; #FUNCTION# ====================================================================================================================
  72. ; Author ........: Paul Campbell (PaulIA)
  73. ; Modified.......:
  74. ; ===============================================================================================================================
  75. Func _ClipBoard_Close()
  76. Local $aResult = DllCall("user32.dll", "bool", "CloseClipboard")
  77. If @error Then Return SetError(@error, @extended, False)
  78. Return $aResult[0]
  79. EndFunc ;==>_ClipBoard_Close
  80. ; #FUNCTION# ====================================================================================================================
  81. ; Author ........: Paul Campbell (PaulIA)
  82. ; Modified.......:
  83. ; ===============================================================================================================================
  84. Func _ClipBoard_CountFormats()
  85. Local $aResult = DllCall("user32.dll", "int", "CountClipboardFormats")
  86. If @error Then Return SetError(@error, @extended, 0)
  87. Return $aResult[0]
  88. EndFunc ;==>_ClipBoard_CountFormats
  89. ; #FUNCTION# ====================================================================================================================
  90. ; Author ........: Paul Campbell (PaulIA)
  91. ; Modified.......:
  92. ; ===============================================================================================================================
  93. Func _ClipBoard_Empty()
  94. Local $aResult = DllCall("user32.dll", "bool", "EmptyClipboard")
  95. If @error Then Return SetError(@error, @extended, False)
  96. Return $aResult[0]
  97. EndFunc ;==>_ClipBoard_Empty
  98. ; #FUNCTION# ====================================================================================================================
  99. ; Author ........: Paul Campbell (PaulIA)
  100. ; Modified.......:
  101. ; ===============================================================================================================================
  102. Func _ClipBoard_EnumFormats($iFormat)
  103. Local $aResult = DllCall("user32.dll", "uint", "EnumClipboardFormats", "uint", $iFormat)
  104. If @error Then Return SetError(@error, @extended, 0)
  105. Return $aResult[0]
  106. EndFunc ;==>_ClipBoard_EnumFormats
  107. ; #FUNCTION# ====================================================================================================================
  108. ; Author ........: Paul Campbell (PaulIA)
  109. ; Modified.......: Gary Frost
  110. ; ===============================================================================================================================
  111. Func _ClipBoard_FormatStr($iFormat)
  112. Local $aFormat[18] = [17, "Text", "Bitmap", "Metafile Picture", "SYLK", "DIF", "TIFF", "OEM Text", "DIB", "Palette", _
  113. "Pen Data", "RIFF", "WAVE", "Unicode Text", "Enhanced Metafile", "HDROP", "Locale", "DIB V5"]
  114. If $iFormat >= 1 And $iFormat <= 17 Then Return $aFormat[$iFormat]
  115. Switch $iFormat
  116. Case $CF_OWNERDISPLAY
  117. Return "Owner Display"
  118. Case $CF_DSPTEXT
  119. Return "Private Text"
  120. Case $CF_DSPBITMAP
  121. Return "Private Bitmap"
  122. Case $CF_DSPMETAFILEPICT
  123. Return "Private Metafile Picture"
  124. Case $CF_DSPENHMETAFILE
  125. Return "Private Enhanced Metafile"
  126. Case Else
  127. Return _ClipBoard_GetFormatName($iFormat)
  128. EndSwitch
  129. EndFunc ;==>_ClipBoard_FormatStr
  130. ; #FUNCTION# ====================================================================================================================
  131. ; Author ........: Paul Campbell (PaulIA)
  132. ; Modified.......: Gary Frost,
  133. ; ===============================================================================================================================
  134. Func _ClipBoard_GetData($iFormat = 1)
  135. If Not _ClipBoard_IsFormatAvailable($iFormat) Then Return SetError(-1, 0, 0)
  136. If Not _ClipBoard_Open(0) Then Return SetError(-2, 0, 0)
  137. Local $hMemory = _ClipBoard_GetDataEx($iFormat)
  138. ;_ClipBoard_Close() ; moved to end: traditionally done *after* copying over the memory
  139. If $hMemory = 0 Then
  140. _ClipBoard_Close()
  141. Return SetError(-3, 0, 0)
  142. EndIf
  143. Local $pMemoryBlock = _MemGlobalLock($hMemory)
  144. If $pMemoryBlock = 0 Then
  145. _ClipBoard_Close()
  146. Return SetError(-4, 0, 0)
  147. EndIf
  148. ; Get the actual memory size of the ClipBoard memory object (in bytes)
  149. Local $iDataSize = _MemGlobalSize($hMemory)
  150. If $iDataSize = 0 Then
  151. _MemGlobalUnlock($hMemory)
  152. _ClipBoard_Close()
  153. Return SetError(-5, 0, "")
  154. EndIf
  155. Local $tData
  156. Switch $iFormat
  157. Case $CF_TEXT, $CF_OEMTEXT
  158. $tData = DllStructCreate("char[" & $iDataSize & "]", $pMemoryBlock)
  159. Case $CF_UNICODETEXT
  160. ; Round() shouldn't be necessary, as CF_UNICODETEXT should be 2-bytes wide & thus evenly-divisible
  161. $iDataSize = Round($iDataSize / 2)
  162. $tData = DllStructCreate("wchar[" & $iDataSize & "]", $pMemoryBlock)
  163. Case Else
  164. ; Binary data return for all other formats
  165. $tData = DllStructCreate("byte[" & $iDataSize & "]", $pMemoryBlock)
  166. EndSwitch
  167. ; Grab the data from the Structure so the Memory can be unlocked
  168. Local $vReturn = DllStructGetData($tData, 1)
  169. ; Unlock the memory & Close the clipboard now that we have grabbed what we needed
  170. _MemGlobalUnlock($hMemory)
  171. _ClipBoard_Close()
  172. ; Return the size of the string or binary object in @extended
  173. Return SetExtended($iDataSize, $vReturn)
  174. EndFunc ;==>_ClipBoard_GetData
  175. ; #FUNCTION# ====================================================================================================================
  176. ; Author ........: Paul Campbell (PaulIA)
  177. ; Modified.......:
  178. ; ===============================================================================================================================
  179. Func _ClipBoard_GetDataEx($iFormat = 1)
  180. Local $aResult = DllCall("user32.dll", "handle", "GetClipboardData", "uint", $iFormat)
  181. If @error Then Return SetError(@error, @extended, 0)
  182. Return $aResult[0]
  183. EndFunc ;==>_ClipBoard_GetDataEx
  184. ; #FUNCTION# ====================================================================================================================
  185. ; Author ........: Paul Campbell (PaulIA)
  186. ; Modified.......: Ascend4nt
  187. ; ===============================================================================================================================
  188. Func _ClipBoard_GetFormatName($iFormat)
  189. Local $aResult = DllCall("user32.dll", "int", "GetClipboardFormatNameW", "uint", $iFormat, "wstr", "", "int", 4096)
  190. If @error Then Return SetError(@error, @extended, "")
  191. Return $aResult[2]
  192. EndFunc ;==>_ClipBoard_GetFormatName
  193. ; #FUNCTION# ====================================================================================================================
  194. ; Author ........: Paul Campbell (PaulIA)
  195. ; Modified.......:
  196. ; ===============================================================================================================================
  197. Func _ClipBoard_GetOpenWindow()
  198. Local $aResult = DllCall("user32.dll", "hwnd", "GetOpenClipboardWindow")
  199. If @error Then Return SetError(@error, @extended, 0)
  200. Return $aResult[0]
  201. EndFunc ;==>_ClipBoard_GetOpenWindow
  202. ; #FUNCTION# ====================================================================================================================
  203. ; Author ........: Paul Campbell (PaulIA)
  204. ; Modified.......:
  205. ; ===============================================================================================================================
  206. Func _ClipBoard_GetOwner()
  207. Local $aResult = DllCall("user32.dll", "hwnd", "GetClipboardOwner")
  208. If @error Then Return SetError(@error, @extended, 0)
  209. Return $aResult[0]
  210. EndFunc ;==>_ClipBoard_GetOwner
  211. ; #FUNCTION# ====================================================================================================================
  212. ; Author ........: Paul Campbell (PaulIA)
  213. ; Modified.......:
  214. ; ===============================================================================================================================
  215. Func _ClipBoard_GetPriorityFormat($aFormats)
  216. If Not IsArray($aFormats) Then Return SetError(-1, 0, 0)
  217. If $aFormats[0] <= 0 Then Return SetError(-2, 0, 0)
  218. Local $tData = DllStructCreate("uint[" & $aFormats[0] & "]")
  219. For $iI = 1 To $aFormats[0]
  220. DllStructSetData($tData, 1, $aFormats[$iI], $iI)
  221. Next
  222. Local $aResult = DllCall("user32.dll", "int", "GetPriorityClipboardFormat", "struct*", $tData, "int", $aFormats[0])
  223. If @error Then Return SetError(@error, @extended, 0)
  224. Return $aResult[0]
  225. EndFunc ;==>_ClipBoard_GetPriorityFormat
  226. ; #FUNCTION# ====================================================================================================================
  227. ; Author ........: Paul Campbell (PaulIA)
  228. ; Modified.......:
  229. ; ===============================================================================================================================
  230. Func _ClipBoard_GetSequenceNumber()
  231. Local $aResult = DllCall("user32.dll", "dword", "GetClipboardSequenceNumber")
  232. If @error Then Return SetError(@error, @extended, 0)
  233. Return $aResult[0]
  234. EndFunc ;==>_ClipBoard_GetSequenceNumber
  235. ; #FUNCTION# ====================================================================================================================
  236. ; Author ........: Paul Campbell (PaulIA)
  237. ; Modified.......:
  238. ; ===============================================================================================================================
  239. Func _ClipBoard_GetViewer()
  240. Local $aResult = DllCall("user32.dll", "hwnd", "GetClipboardViewer")
  241. If @error Then Return SetError(@error, @extended, 0)
  242. Return $aResult[0]
  243. EndFunc ;==>_ClipBoard_GetViewer
  244. ; #FUNCTION# ====================================================================================================================
  245. ; Author ........: Paul Campbell (PaulIA)
  246. ; Modified.......:
  247. ; ===============================================================================================================================
  248. Func _ClipBoard_IsFormatAvailable($iFormat)
  249. Local $aResult = DllCall("user32.dll", "bool", "IsClipboardFormatAvailable", "uint", $iFormat)
  250. If @error Then Return SetError(@error, @extended, False)
  251. Return $aResult[0]
  252. EndFunc ;==>_ClipBoard_IsFormatAvailable
  253. ; #FUNCTION# ====================================================================================================================
  254. ; Author ........: Paul Campbell (PaulIA)
  255. ; Modified.......:
  256. ; ===============================================================================================================================
  257. Func _ClipBoard_Open($hOwner)
  258. Local $aResult = DllCall("user32.dll", "bool", "OpenClipboard", "hwnd", $hOwner)
  259. If @error Then Return SetError(@error, @extended, False)
  260. Return $aResult[0]
  261. EndFunc ;==>_ClipBoard_Open
  262. ; #FUNCTION# ====================================================================================================================
  263. ; Author ........: Paul Campbell (PaulIA)
  264. ; Modified.......:
  265. ; ===============================================================================================================================
  266. Func _ClipBoard_RegisterFormat($sFormat)
  267. Local $aResult = DllCall("user32.dll", "uint", "RegisterClipboardFormatW", "wstr", $sFormat)
  268. If @error Then Return SetError(@error, @extended, 0)
  269. Return $aResult[0]
  270. EndFunc ;==>_ClipBoard_RegisterFormat
  271. ; #FUNCTION# ====================================================================================================================
  272. ; Author ........: Paul Campbell (PaulIA)
  273. ; Modified.......: Ascend4nt
  274. ; ===============================================================================================================================
  275. Func _ClipBoard_SetData($vData, $iFormat = 1)
  276. Local $tData, $hLock, $hMemory, $iSize
  277. ; Special NULL case? (the option to provide clipboard formats upon request)
  278. If IsNumber($vData) And $vData = 0 Then
  279. ; No need to allocate/set memory
  280. $hMemory = $vData
  281. Else
  282. ; Test if the format is Binary or String format (only supported formats)
  283. If IsBinary($vData) Then
  284. $iSize = BinaryLen($vData)
  285. ElseIf IsString($vData) Then
  286. $iSize = StringLen($vData)
  287. Else
  288. ; Unsupported data type
  289. Return SetError(2, 0, 0)
  290. EndIf
  291. $iSize += 1
  292. ; Memory allocation is in bytes, yet Unicode text is 2-bytes wide
  293. If $iFormat = $CF_UNICODETEXT Then
  294. ; Multiply $iSize (Character length for Unicode text) by 2 for Unicode
  295. $hMemory = _MemGlobalAlloc($iSize * 2, $GHND)
  296. Else
  297. $hMemory = _MemGlobalAlloc($iSize, $GHND)
  298. EndIf
  299. If $hMemory = 0 Then Return SetError(-1, 0, 0)
  300. $hLock = _MemGlobalLock($hMemory)
  301. If $hLock = 0 Then Return SetError(-2, 0, 0)
  302. Switch $iFormat
  303. Case $CF_TEXT, $CF_OEMTEXT
  304. $tData = DllStructCreate("char[" & $iSize & "]", $hLock)
  305. Case $CF_UNICODETEXT
  306. $tData = DllStructCreate("wchar[" & $iSize & "]", $hLock)
  307. Case Else
  308. ; Every other type is treated as Binary, or ASCII Strings
  309. $tData = DllStructCreate("byte[" & $iSize & "]", $hLock)
  310. EndSwitch
  311. DllStructSetData($tData, 1, $vData)
  312. _MemGlobalUnlock($hMemory)
  313. EndIf
  314. If Not _ClipBoard_Open(0) Then Return SetError(-5, 0, 0)
  315. If Not _ClipBoard_Empty() Then
  316. _ClipBoard_Close()
  317. Return SetError(-6, 0, 0)
  318. EndIf
  319. If Not _ClipBoard_SetDataEx($hMemory, $iFormat) Then
  320. _ClipBoard_Close()
  321. Return SetError(-7, 0, 0)
  322. EndIf
  323. _ClipBoard_Close()
  324. Return $hMemory
  325. EndFunc ;==>_ClipBoard_SetData
  326. ; #FUNCTION# ====================================================================================================================
  327. ; Author ........: Paul Campbell (PaulIA)
  328. ; Modified.......:
  329. ; ===============================================================================================================================
  330. Func _ClipBoard_SetDataEx(ByRef $hMemory, $iFormat = 1)
  331. Local $aResult = DllCall("user32.dll", "handle", "SetClipboardData", "uint", $iFormat, "handle", $hMemory)
  332. If @error Then Return SetError(@error, @extended, 0)
  333. Return $aResult[0]
  334. EndFunc ;==>_ClipBoard_SetDataEx
  335. ; #FUNCTION# ====================================================================================================================
  336. ; Author ........: Paul Campbell (PaulIA)
  337. ; Modified.......:
  338. ; ===============================================================================================================================
  339. Func _ClipBoard_SetViewer($hViewer)
  340. Local $aResult = DllCall("user32.dll", "hwnd", "SetClipboardViewer", "hwnd", $hViewer)
  341. If @error Then Return SetError(@error, @extended, 0)
  342. Return $aResult[0]
  343. EndFunc ;==>_ClipBoard_SetViewer