I am trying to use the API function SetClipboardData to place a picture on the clipboard. I found a declare in the NUG with it, but it is for text. So I tried to adapt it to pictures. But so far I have not been successful. Nothing gets to the clipboard. Here is what I have.
I will strongly appreciate any pointer.
[code]Sub Action()
dim pic as picture
pic = Studebaker
Const LR_LOADFROMFILE = &H10
Const IMAGE_BITMAP = 0
Const IMAGE_ICON = 1
Const IMAGE_CURSOR = 2
Const IMAGE_ENHMETAFILE = 3
Const CF_BITMAP = 2
const GMEM_DDESHARE = &H2000
const GMEM_MOVEABLE = &H02
const CF_TEXT = 1
declare function OpenClipboard lib “user32.dll” (i as integer) as boolean
declare sub SetClipboardData lib “user32.dll” (i as integer, hdl as Integer)
declare sub CloseClipboard lib “user32.dll” ()
declare function GlobalAlloc lib “kernel32.dll” (flags as Integer,numBytes as Integer) as Integer
declare function GlobalLock Lib “kernel32.dll” (hdl as Integer) as Ptr
declare sub GlobalUnlock Lib “kernel32.dll” (hdl as Integer)
dim hdl as Integer, mb as MemoryBlock
mb = pic.GetData(Picture.FormatBMP, -1)
if not OpenClipboard(0) then
break
else
hdl = GlobalAlloc (GMEM_MOVEABLE + GMEM_DDESHARE, mb.Size+4)
mb = GlobalLock(hdl)
mb = pic.GetData(Picture.FormatBMP, -1)
GlobalUnlock(hdl)
SetClipboardData(CF_BITMAP, hdl)
CloseClipboard
end if
End Sub
[/code]
I have tried to adapt the code to the best I could understand. In the original, hdl is set through GlobalAlloc with the len of a string+1, then the memory block mb.stringvalue is set to that string.
hdl = GlobalAlloc (GMEM_MOVEABLE + GMEM_DDESHARE, LenB(txt) + 1)
mb = GlobalLock(hdl)
mb.CString(0) = txt
What I did was to load mb with the picture with pic.GetData, and use mb.size+2 for hdl (4 bytes given the length of the picture). Then after locking the resource with GlobalLock, I again load it with the picture, like it was set with the string in the NUG code.
And I use as first parameter of SetClipboardData CF_BITMAP to indicate it is a picture.