Ticket #1031: _ClipBoard_SetData.[Fix].au3

File _ClipBoard_SetData.[Fix].au3, 6.1 KB (added by Ascendant, 15 years ago)

_ClipBoard_SetData new version

Line 
1; Fixed _ClipBoard_SetData  (to be consistent with new _ClipBoard_GetData function changes for v3.3.1.2)
2
3; #FUNCTION# ====================================================================================================================
4; Name...........: _ClipBoard_SetData
5; Description ...: Places data on the clipboard in a specified clipboard format
6; Syntax.........: _ClipBoard_SetData($vData[, $iFormat = 1])
7; Parameters ....: $vData     - Data in Binary or String format, or optionally NULL (0) (owner must render, see below).
8;                  +IMPORTANT: If a String is passed, and it is not of type $CF_TEXT, $CF_OEMTEXT, or $CF_UNICODETEXT,
9;                  + it will be treated as an ANSI string. To force Unicode strings for other types, you must pass the data
10;                  + in Binary format. Also, do NOT pass $CF_UNICODETEXT in Binary format (causes garbled data).
11;                  +When $vData is NULL, it indicates that the window provides data in the specified clipboard format upon request.
12;                  +If a window delays rendering, it must process the $WM_RENDERFORMAT and $WM_RENDERALLFORMATS messages.  If this
13;                  +function succeeds, the system owns the object identified by the $hMemory parameter.  The application may not
14;                  +write to or free the data once ownership has been transferred to the system, but it can lock and read from the
15;                  +data until the _ClipBoard_Close function is called.  The memory must be unlocked before the clipboard is
16;                  +closed.  If the $hMemory parameter identifies a memory object, the object must have been allocated using the
17;                  +function with the $GMEM_MOVEABLE
18;                  +flag.
19;                  $iFormat     - Specifies a clipboard format:
20;                  |$CF_TEXT            - Text format
21;                  |$CF_BITMAP          - Handle to a bitmap (HBITMAP)
22;                  |$CF_METAFILEPICT    - Handle to a metafile picture (METAFILEPICT)
23;                  |$CF_SYLK            - Microsoft Symbolic Link (SYLK) format
24;                  |$CF_DIF             - Software Arts' Data Interchange Format
25;                  |$CF_TIFF            - Tagged image file format
26;                  |$CF_OEMTEXT         - Text format containing characters in the OEM character set
27;                  |$CF_DIB             - BITMAPINFO structure followed by the bitmap bits
28;                  |$CF_PALETTE         - Handle to a color palette
29;                  |$CF_PENDATA         - Data for the pen extensions to Pen Computing
30;                  |$CF_RIFF            - Represents audio data in RIFF format
31;                  |$CF_WAVE            - Represents audio data in WAVE format
32;                  |$CF_UNICODETEXT     - Unicode text format
33;                  |$CF_ENHMETAFILE     - Handle to an enhanced metafile (HENHMETAFILE)
34;                  |$CF_HDROP           - Handle to type HDROP that identifies a list of files
35;                  |$CF_LOCALE          - Handle to the locale identifier associated with text in the clipboard
36;                  |$CF_DIBV5           - BITMAPV5HEADER structure followed by bitmap color and the bitmap bits
37;                  |$CF_OWNERDISPLAY    - Owner display format
38;                  |$CF_DSPTEXT         - Text display format associated with a private format
39;                  |$CF_DSPBITMAP       - Bitmap display format associated with a private format
40;                  |$CF_DSPMETAFILEPICT - Metafile picture display format associated with a private format
41;                  |$CF_DSPENHMETAFILE  - Enhanced metafile display format associated with a private format
42; Return values .: Success      - The handle to the data
43;                  Failure      - 0
44; Author ........: Paul Campbell (PaulIA)
45; Modified.......:
46; Remarks .......: This function performs all of the steps neccesary to put data on the clipboard.  It will allocate the global
47;                  memory object, open the clipboard, place the data on the clipboard and close the clipboard.  If you need more
48;                  control over putting data on the clipboard, you may want to use the _ClipBoard_SetDataEx function.
49; Related .......: _ClipBoard_GetData, _ClipBoard_SetDataEx
50; Link ..........;
51; Example .......; Yes
52; ===============================================================================================================================
53Func _ClipBoard_SetData($vData, $iFormat = 1)
54        Local $tData, $hLock, $hMemory, $iSize
55
56        ; Special NULL case? (the option to provide clipboard formats upon request)
57        If IsNumber($vData) And $vData = 0 Then
58                ; No need to allocate/set memory
59                $hMemory = $vData
60        Else           
61                ; Test if the format is Binary or String format (only supported formats)
62                If IsBinary($vData) Then
63                        $iSize = BinaryLen($vData)     
64                ElseIf IsString($vData) Then
65                        $iSize = StringLen($vData)
66                Else
67                        ; Unsupported data type
68                        Return SetError(2,0,0)
69                Endif
70                $iSize+=1
71               
72                ; Memory allocation is in bytes, yet Unicode text is 2-bytes wide
73                If $iFormat = $CF_UNICODETEXT Then
74                        ; Multiply $iSize (Character length for Unicode text) by 2 for Unicode
75                        $hMemory = _MemGlobalAlloc($iSize * 2, $GHND)
76                Else
77                        $hMemory = _MemGlobalAlloc($iSize, $GHND)
78                EndIf
79               
80                If $hMemory = 0 Then Return SetError(-1, 0, 0)
81                $hLock = _MemGlobalLock($hMemory)
82                If $hLock = 0 Then Return SetError(-2, 0, 0)
83       
84                Switch $iFormat
85                        Case $CF_TEXT, $CF_OEMTEXT
86                                $tData = DllStructCreate("char[" & $iSize & "]", $hLock)
87                        Case $CF_UNICODETEXT
88                                $tData = DllStructCreate("wchar[" & $iSize & "]", $hLock)
89                        Case Else
90                                ; Every other type is treated as Binary, or ASCII Strings
91                                $tData = DllStructCreate("ubyte[" & $iSize & "]", $hLock)
92                EndSwitch
93
94                DllStructSetData($tData, 1, $vData)
95                _MemGlobalUnlock($hMemory)
96        EndIf
97       
98        If Not _ClipBoard_Open(0) Then Return SetError(-5, 0, 0)
99        If Not _ClipBoard_Empty() Then Return SetError(-6, 0, 0)
100        If Not _ClipBoard_SetDataEx($hMemory, $iFormat) Then
101                _ClipBoard_Close()
102                Return SetError(-7, 0, 0)
103        EndIf
104
105        _ClipBoard_Close()
106        Return $hMemory
107EndFunc   ;==>_ClipBoard_SetData